Pseudocode from define.xml derivation text, tested on the data
Paste derivation comments from define.xml, get numbered pseudocode and Python; 10 of 14 columns matched the dataset.
About 30 minWordWorked example, recomputed
What you get
| A | B | C | D | E | F | |
|---|---|---|---|---|---|---|
| 1 | Variable | Records matching (as written) | Records | Matches all records | Records matching (SOC spelling fixed) | Cause of mismatch |
| 2 | ASTDT | 1191 | 1191 | yes | 1191 | |
| 3 | ASTDTF | 1191 | 1191 | yes | 1191 | |
| 4 | AENDT | 1191 | 1191 | yes | 1191 | |
| 5 | ASTDY | 1191 | 1191 | yes | 1191 | |
| 6 | AENDY | 1191 | 1191 | yes | 1191 | |
| 7 | ADURN | 1187 | 1191 | no | 1187 | production program computes duration before imputing the start day; not stated in the define text |
| 8 | ADURU | 1187 | 1191 | no | 1187 | same 4 records as ADURN |
| 9 | TRTEMFL | 1191 | 1191 | yes | 1191 | |
| 10 | AOCCFL | 1191 | 1191 | yes | 1191 | |
| 11 | AOCCSFL | 1191 | 1191 | yes | 1191 | |
| 12 | AOCCPFL | 1191 | 1191 | yes | 1191 | |
| 13 | AOCC02FL | 1191 | 1191 | yes | 1191 | |
| 14 | CQ01NAM | 1010 | 1191 | no | 1191 | SOC string in the define text has a stray space ('SUBC UTANEOUS') |
| 15 | AOCC01FL | 1107 | 1191 | no | 1191 | knock-on of CQ01NAM |
Swipe sideways to see every column.
Each derived column compared with adam_adae.xpt, record by record (1,191 records). Amber = does not match on every record.
Draft for review. Public data only.
The prompt
For each ADAE derivation comment below, write numbered pseudocode a programmer could implement without
the define: inputs, order of steps, handling of missing and partial values.
Then write Python (pandas) that implements it on sdtm_ae.xpt and adam_adsl.xpt, run it, and print counts.
If the text is ambiguous, contradictory or looks like a typo, say so and state your choice.
Do not invent rules the text does not give.
[paste the derivation comments and the input file notes here]Works in any chat assistant that writes and runs code; not tested in Microsoft 365 Copilot
Sample input used here
ADAE derivation comments, adam_define.xml, CDISCPILOT01
TRTSDT (Date of First Exposure to Treatment): ADSL.TRTSDT
ASTDT (Analysis Start Date): AE.AESTDTC, converted to a numeric SAS date. Some events with partial dates are imputed in a conservative manner. If the day component is missing, a value of '01' is used. If both the month and day are
ASTDTF (Analysis Start Date Imputation Flag): ASTDTF='D' if the day value within the character date is imputed. Note that only day values needed to be imputed for this study.
AENDT (Analysis End Date): AE.AEENDTC, converted to a numeric SAS date
ASTDY (Analysis Start Relative Day): IF ASTDT>=TRTSDT>MISSING then ASTDY=ASTDT-TRTSDT+1 Else if TRTSDT>ASTDT>MISSING then ASTDY=ASTDT-TRTSDT
AENDY (Analysis End Relative Day): IF AENDT>=TRTSDT>MISSING then AENDY=AENDT-TRTSDT+1 Else if TRTSDT>AENDT>MISSING then AENDY=AENDT-TRTSDT
ADURN (AE Duration (N)): ADURN=AENDT-ASTDT+1
ADURU (AE Duration Units): If ADURN is not missing then ADURU='DAY'
TRTEMFL (Treatment Emergent Analysis Flag): If ASTDT >= TRTSDT > . then TRTEMFL='Y'. Otherwise TRTEMFL='N'
AOCCFL (1st Occurrence of Any AE Flag): Subset to TRTEMFL='Y' and sort by Subject (USUBJID), Start Date (ASTDT), and Sequence Number (AESEQ) and flag the first record (set AOCCFL='Y') within each Subject
AOCCSFL (1st Occurrence of SOC Flag): Subset to TRTEMFL='Y' and sort by Subject (USUBJID), System Organ Class (AEBODSYS), Start Date (ASTDT), and Sequence Number (AESEQ) and flag the first record (set AOCCSFL='Y') within each Subj
AOCCPFL (1st Occurrence of Preferred Term Flag): Subset to TRTEMFL='Y' and sort by Subject (USUBJID), System Organ Class (AEBODSYS), Preferred Term (AEDECOD), Start Date (ASTDT), and Sequence Number (AESEQ) and flag the first reco
AOCC02FL (1st Occurrence 02 Flag for Serious): Subset to TRTEMFL='Y' and AESER='Y' and sort by Subject (USUBJID), Start Date (ASTDT), and Sequence Number (AESEQ) and flag the first record (set AOCC02FL='Y') within each Subject
CQ01NAM (Customized Query 01 Name): If AEDECOD contains any of the character strings of ('APPLICATION', 'DERMATITIS', 'ERYTHEMA', 'BLISTER') OR if AEBODSYS='SKIN AND SUBC UTANEOUS TISSUE DISORDERS' but AEDECOD is not in ('COLD SWECDISC pilot define.xml, credited to CDISC. Quotes normalised; the 'SUBC UTANEOUS' spelling is in the source.
How this was checked
Result
Generated by Claude on 2026-10-20; Python re-run 2026-10-24.
Worked example, recomputed. A script recomputed the values from the data and they matched or were flagged. Statements a script cannot test were checked by a second Claude agent.
Prompt shortened for this page. The output shown came from the earlier run of the longer prompt (2026-10-20).
Re-check on 2026-10-24. The AI's Python and the comparison script re-run with Python 3.14 and pandas 3.0: the same counts (CQ01NAM 181, AOCC01FL 84, ADURN and ADURU 4 each). Code only: the AI was not re-run. No R script exists for this workflow.
The longer prompt that was checked
Below are the ADAE derivation comments from define.xml (CDISC pilot) and the two input files. For each variable write numbered pseudocode a programmer could implement without seeing the define: inputs, order of steps, handling of missing and partial values. Then write Python (pandas) that implements it, reading sdtm_ae.xpt and adam_adsl.xpt (pd.read_sas with format='xport', encoding='latin1'), and run it. Where the define text is ambiguous, contradictory, or looks like a typo, say so and state the choice you made instead of silently picking one. Print counts per derived variable so I can check them. Do not invent rules the text does not give. [derivation text and input file notes pasted here]
A one-line prompt for comparison
Prompt: Write pseudocode for the derivations in the define file
I don't have the define file in front of me, so here is a generic pattern for turning define.xml derivations into pseudocode. For each derived variable: 1. Identify the source dataset and variables (for example ADSL, ADAE, SDTM AE). 2. Write the derivation in plain steps. 3. Handle missing values and flags. Example pseudocode (AE analysis dataset): READ AE, ADSL MERGE AE with ADSL by USUBJID ASTDT = convert AESTDTC to a date AENDT = convert AEENDTC to a date ASTDY = ASTDT - TRTSDT + 1 ...
All 5 checks
- Ask which step runs first. The production program computes duration before imputing the start day; the define does not say so.
- Do not treat define.xml as the specification of record when it disagrees with the program or the data.
Sources
- CDISC SDTM/ADaM pilot project (CDISC terms of use; used unmodified, not redistributed)
Scripts
- Derivation and comparison (Python)
- Licence (MIT)
- Expected console output: derive_adae_and_compare_as_written.txt
- Expected console output: derive_adae_and_compare_soc_typo_fixed.txt
- Expected console output: derive_adae_output.txt
- Full input used here
- Record of the earlier run (JSON; the page above reflects the newer check)
Related workflows
All workflows. How we check. Public data only. Drafts for review, not validated output.
Get one workflow a week
Unsubscribe anytime. Privacy.