Skip to main content

Improved handling of `all_diagnoses` and `all_procedures` fields

17 December 2024

We have added better handling for the all_diagnoses and all_procedures fields in the apcs table. These fields contain every diagnosis (ICD10 codes), and each procedure undertaken (OPCS4 codes), during a hospital spell. The field is a list of clinical codes, joined together with ,s and ||s (see the above links for more details on why). Previously ehrql considered these fields to be strings, and so people would need to take a codelist, split it up, convert each code to a string, and then search the field for each one in turn. Now you can simply do:

all_diagnoses.contains_any_of(icd10_codelist)

all_diagnoses.contains(icd10_code) also works if you just want to check for the presence of a single code. Also, both methods are happy to be passed ICD10Codes, or ICD10 string prefixes eg. “N17” - or a combination of the two.

The same is true for all_procedures, except replace ICD10, with OPCS4 in the above explanation.