PURPOSE: To validate an algorithm that uses delivery date and diagnosis codes to define gestational age at birth in electronic health plan databases. METHODS: Using data from 225,384 live born deliveries to women aged 15-45 years in 2001-2007 within eight of the 11 health plans participating in the Medication Exposure in Pregnancy Risk Evaluation Program, we compared (1) the algorithm-derived gestational age versus the "gold-standard" gestational age obtained from the infant birth certificate file and (2) the prenatal exposure status of two antidepressants (fluoxetine and sertraline) and two antibiotics (amoxicillin and azithromycin) as determined by the algorithm-derived versus the gold-standard gestational age. RESULTS: The mean algorithm-derived gestational age at birth was lower than the mean obtained from the birth certificate file among singleton deliveries (267.9 vs 273.5 days) but not among multiple-gestation deliveries (253.9 vs 252.6 days). The algorithm-derived prenatal exposure to the antidepressants had a sensitivity and a positive predictive value of >/=95%, and a specificity and a negative predictive value of almost 100%. Sensitivity and positive predictive value were both >/=90%, and specificity and negative predictive value were both >99% for the antibiotics. CONCLUSIONS: A gestational age algorithm based upon electronic health plan data correctly classified medication exposure status in most live born deliveries, but trimester-specific misclassification may be higher for drugs typically used for short durations.