Using a simple pregnancy algorithm to analyse Electronic Health Records (EHRs) data in OpenSAFELY
- Posted:
- Written by:
-
- Helen Curtis, Vicki Palin, Paolo Mazzone, Victoria Burns, Rose Higgins, Em Prestige
- Categories:
In a previous blog post, we described the general challenges in identifying pregnancy in electronic healthcare records (EHRs), with particular reference to OpenSAFELY. In this blog post, we’ve described how to make a simple pregnancy algorithm for an individual project, identify anomalies, and the importance of codelist checks.
Making a simple pregnancy algorithm for an individual project
As part of the Pharmacy First evaluation, we needed to identify the patients who were eligible to use the service at any point in time, specifically those who were not pregnant. We used ‘months’ as the window for analysis. While ‘months’ as a timescale is an approximation in itself, as a pregnancy might have started and/or become known or ended during the middle of the month, this approach could be adapted for more specific dates as required.
We applied the following algorithm where the criteria were applied sequentially (i.e. if one criteria is not met, proceed to the next):
| Criteria | Time period | Result | Codelist |
|---|---|---|---|
| Recent “end of pregnancy” code | 0-12 weeks prior to the start of present month | Not pregnant | Custom codelist (1,184 codes) |
| Future “end of pregnancy” code | In present month or next 2 months | Pregnant | Custom codelist (1,184 codes) |
| Future “estimated delivery date” (EDD) code | Within month or next 8 months (and not preceded by an [early] end-of-pregnancy prior to present month) | Pregnant | Custom codelist (3 codes) |
| Recent “pregnancy” code | Within current month or up to 12 weeks prior. | Pregnant | NHS Primary Care Domain Refset (53 codes) |
See the full code here
Notes on codelists, assumptions, and limitations of our pregnancy algorithm
The “end of pregnancy” codelist was designed to capture events happening within days of delivery, excluding events such as delivery-related infections or breastfeeding, which may be recorded weeks or months later.
To keep the algorithm as simple as possible and to avoid complications caused by the same event having different recorded dates in different care settings, we did not use secondary care codes.
The algorithm could be made more precise by using different groups of “end of pregnancy” codes to distinguish shorter and longer pregnancies, but as this is not straightforward (and may require complex rules), we initially used a combined codelist.
One limitation is that we do not know when the patient found out that they were pregnant. In practice, eligibility would be determined by the service provider asking the patient whether they were pregnant. Even if later-recorded codes show that the patient was pregnant at the time, it’s possible that the patient may not have known this themselves.
Initially, we calculated some counts and custom sense checks to ensure that each element of the algorithm was working reasonably well and that there were minimal conflicts in the information obtained. These checks gave reassuring results.
In addition to other variables, we also incidentally assessed the rates of pregnancy found in under 16s and older adults, comparing males and females. This produced some interesting findings which needed further investigation, which are described below.
Exploring anomalies
While codes for EDD and recent pregnancy were almost exclusively found in females of child bearing age, the “end of pregnancy” codelists found a small rate of matches in males and older females, causing their classification as having a recent pregnancy. We looked further at which specific end-of-pregnancy codes were recorded, focussing on the over 65s and under 16s.
65+
For over 65s, a small number of wrongly recorded pregnancies were identified in both sexes. The most common single code was “knee presentation”, which may be inadvertently used to refer to knee complaints rather than a birth situation as intended (see our recent report on this here). See also Figure 1 for national code usage.
Figure 1 Knee presentation (purple) was recorded over 9000 times in primary care in England in 2024/25. The lines with near-zero usage represent “Double knee presentation” and “Single knee presentation”. Source: OpenCodeCounts.
The next most common codes contained the words “delivery” or “cervical”, e.g. “Delivery Normal” in their descriptions. It is possible that codes related to “delivery” are autocomplete errors, where a clinician types a reference to the delivery (administration) of a drug, for example. Similarly, “cervical” codes – intended for cervical observations during delivery – may sometimes be entered in error when reference to the cervical spine was intended.
Under 16s
For people under 16, we found almost as many males as females (based on sex determined at birth) had a pregnancy identified by the algorithm. Although the numbers were relatively small, it was important to check whether pregnant people under-16 could be excluded accurately, because this was a specific exclusion criteria for some pathways.
At first, we found a range of codes. Some were likely related to the child’s own birth, as the codelist includes codes possibly intended for the baby rather than for the mother, and may have been used mainly in the youngest children. When we limited the analysis to ages 11-15 and removed ‘knee presentation’, fewer pregnancies were found, but some still remained in males.
Two codes in particular: “Delivery Normal” and “Mother Delivered” occurred in males. In the total population, these codes are commonly used (see Figure 2), so they appear to be used for genuine deliveries, rather than being simply problematic codes that can be removed. For comparison, in 16-19 year olds, we found these codes occurred at generally lower counts in males but much more commonly in females.
Figure 2 Delivery Normal (yellow) is recorded around 200k times per year in primary care in England, and Mother Delivered (purple) increased from around 7k uses in 2011/12 to nearly 20k in 2024/25. Source: OpenCodeCounts.
We therefore cannot tell which females have delivery- or birth-related codes recorded erroneously without further analysis. If we assume the pregnancies in under 16s of male sex are all errors, and a similar number of females have had the same codes recorded erroneously, then the rate of false positive pregnancies in females under 16 is somewhere around 10%. Across 20-40 year olds, where pregnancy rates are highest, the false positive rate should be very small. It is harder to be certain about pregnancy status in under 16s because conception rates are very low and termination rates are high.
Conclusions
We found that a relatively simple algorithm could be created to capture pregnancies, however, false positives could be problematic in groups with low rates of pregnancy-related events.
Importance of codelist checks
We strongly recommend carrying out lots of checks on your code and codelists! This applies whether you’re looking at pregnancy or any other events.
Using very large and broad codelists that capture rare events related to your outcome of interest may lead to erroneous results in some patient groups.
Be sure to test codelists in a range of population subgroups to check that rates of matches meet expectations, including where no matches would be expected, and validate results against published data where possible.
What’s next: plans to develop a more comprehensive pregnancy algorithm
There is a clear need for a systematic, open and flexible pregnancy algorithm to make it easier for everyone using OpenSAFELY to identify pregnancies and their outcomes where relevant for research - this in progress.
OpenPREGnosis will soon allow researchers to quickly create their required cohort of patients with valid pregnancy episodes, meaning they can get started on the main task of answering their research questions quickly. OpenPREGnosis could save researchers weeks (or even months) to get their starting cohort ready.
OpenPREGnosis will be an open-source and reproducible algorithm piecing together fragmented records across primary and secondary care. The algorithm systematically collates information using hierarchical, rule-based logic to address conflicting outcome codes, overlapping pregnancy episodes, and missing start date information.
To ensure that the information produced by the algorithm is reliable and robust, OpenPREGnosis will also enforce extensive quality assurance and data checks throughout the pipeline, and integrate clinical stakeholder input to ensure that methods, rules, and parameters are clinically reasonable.