R code from ADaM spec rows, compared with the real ADAE
Paste ADaM spec rows, get R code and a test plan, then compare each variable with an existing dataset.
About 30 minRWorked example, recomputed
What you get
| A | B | C | D | E | F | |
|---|---|---|---|---|---|---|
| 1 | Variable | Matching records | Records | Percent | After two fixes | Cause of mismatch |
| 2 | AESEQ | 1191 | 1191 | 100.0% | 1191 | |
| 3 | ASTDT | 1191 | 1191 | 100.0% | 1191 | |
| 4 | ASTDTF | 1191 | 1191 | 100.0% | 1191 | |
| 5 | AENDT | 1191 | 1191 | 100.0% | 1191 | |
| 6 | ASTDY | 1191 | 1191 | 100.0% | 1191 | |
| 7 | AENDY | 1191 | 1191 | 100.0% | 1191 | |
| 8 | ADURN | 1187 | 1191 | 99.7% | 1191 | ADAE leaves duration missing when the start day was imputed; not in the define text |
| 9 | ADURU | 1187 | 1191 | 99.7% | 1191 | same 4 records as ADURN |
| 10 | TRTEMFL | 1191 | 1191 | 100.0% | 1191 | |
| 11 | AOCCFL | 1191 | 1191 | 100.0% | 1191 | |
| 12 | AOCCSFL | 1191 | 1191 | 100.0% | 1191 | |
| 13 | AOCCPFL | 1191 | 1191 | 100.0% | 1191 | |
| 14 | AOCC02FL | 1191 | 1191 | 100.0% | 1191 | |
| 15 | CQ01NAM | 1010 | 1191 | 84.8% | 1191 | define text has 'SUBC UTANEOUS'; the data value is 'SUBCUTANEOUS' |
| 16 | AOCC01FL | 1107 | 1191 | 92.9% | 1191 | knock-on of CQ01NAM |
Swipe sideways to see every column.
R code run on the pilot SDTM AE and ADSL, compared with adam_adae.xpt by USUBJID and AESEQ. 17,592 of 17,865 cells match (98.47%). R 4.6.1, 2026-10-24.
Draft for review. Public data only.
The prompt
Write R code (base R and dplyr only) for each ADAE variable below, from its derivation text and sources.
One code block per variable, with a comment restating the derivation. Add no logic the text does not state.
If a row is ambiguous or looks like a typo, say so once under OPEN QUESTIONS and say which reading you coded.
Then give a test plan: 8 to 10 hand-derived cases, and R code that compares every variable with an existing
ADAE by USUBJID and AESEQ (matches per variable, first five mismatches). Label the code UNRUN.
[paste the derivation rows and source variables here]Works in any chat assistant that writes R; not tested in Microsoft 365 Copilot
Sample input used here
ADAE derivation rows, adam_define.xml, CDISCPILOT01
AESEQ | Sequence Number | integer | AE.AESEQ
ASTDT | Analysis Start Date | integer (date, DATE9.) | 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
ASTDTF | Analysis Start Date Imputation Flag | text | 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 | integer (date) | AE.AEENDTC, converted to a numeric SAS date
ASTDY | Analysis Start Relative Day | integer | IF ASTDT>=TRTSDT>MISSING then ASTDY=ASTDT-TRTSDT+1 Else if TRTSDT>ASTDT>MISSING then ASTDY=ASTDT-TRTSDT
AENDY | Analysis End Relative Day | integer | IF AENDT>=TRTSDT>MISSING then AENDY=AENDT-TRTSDT+1 Else if TRTSDT>AENDT>MISSING then AENDY=AENDT-TRTSDT
ADURN | AE Duration (N) | integer | ADURN=AENDT-ASTDT+1
ADURU | AE Duration Units | text | If ADURN is not missing then ADURU='DAY'
TRTEMFL | Treatment Emergent Analysis Flag | text (Y/N) | If ASTDT >= TRTSDT > . then TRTEMFL='Y'. Otherwise TRTEMFL='N'
AOCCFL | 1st Occurrence of Any AE Flag | text | 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 e
AOCCSFL | 1st Occurrence of SOC Flag | text | Subset to TRTEMFL='Y' and sort by Subject (USUBJID), System Organ Class (AEBODSYS), Start Date (ASTDT), and Sequence Number (AESEQ) and flag the first rec
AOCCPFL | 1st Occurrence of Preferred Term Flag | text | Subset to TRTEMFL='Y' and sort by Subject (USUBJID), System Organ Class (AEBODSYS), Preferred Term (AEDECOD), Start Date (ASTDT), and Sequence
AOCC02FL | 1st Occurrence 02 Flag for Serious | text | 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
CQ01NAM | Customized Query 01 Name | text | If AEDECOD contains any of the character strings of ('APPLICATION', 'DERMATITIS', 'ERYTHEMA', 'BLISTER') OR if AEBODSYS='SKIN AND SUBC UTANEOUS TISSUE DISORCDISC pilot define.xml, credited to CDISC. Comment text with entities normalised; the 'SUBC UTANEOUS' spelling is in the source.
How this was checked
Result
Generated by Claude on 2026-10-24 (cold run asking for R); an earlier Python run was generated on 2026-10-19.
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.
Cold re-run of this prompt on 2026-10-24. Cold re-run with the short prompt, asking for R (the verified 2026-10-19 run answered in Python because R was not installed). Run in R 4.6.1: same per-variable counts as the verified Python run (17,592 of 17,865; 273 mismatching cells in the same four variables). The AI again flagged both the 'SUBC UTANEOUS' typo and the AOCC01FL contradiction and again coded the literal text.
The longer prompt that was checked
Draft code for the ADAE variables in the rows below. Language: R if you can; if R is not available, use Python (pandas). Allowed: base functions and dplyr only (no admiral). For each variable give one code block with a comment restating the derivation text. Use only the rows and source variables I pasted; do not add logic the text does not state. Where the text looks ambiguous or self-contradictory, say so once in OPEN QUESTIONS and say which reading you coded. Then give a test plan: 8 to 10 small synthetic cases with the expected value derived by hand from the text, and a plan for comparing every variable with an existing ADAE (keys USUBJID + AESEQ, variable by variable, counts of matches and a listing of mismatches). Label the code UNRUN. Do not claim it is correct. [define.xml rows and source variables pasted here]
A one-line prompt for comparison
Prompt: Write R code to derive the ADAE variables from the spec, TRTEMFL and the dates and the first-occurrence flags. I'll run it.
I do not have your spec in front of me, so this is a typical ADAE derivation using common ADaM conventions. Adjust names to your spec.
library(dplyr)
library(lubridate)
adae <- ae %>%
left_join(adsl %>% select(USUBJID, TRTSDT, TRTEDT), by = "USUBJID") %>%
mutate(
ASTDT = ymd(AESTDTC),
AENDT = ymd(AEENDTC),
TRTEMFL = if_else(ASTDT >= TRTSDT & ASTDT <= TRTEDT + 30, "Y", "N"),
ASTDY = as.numeric(ASTDT - TRTSDT) + 1,
AENDY = as.numeric(AENDT - TRTSDT) + 1,
ADURN = as.numeric(AENDT - ASTDT) + 1,
...All 5 checks
- Tests built from the spec share its blind spots. All 9 hand-derived test rows passed while 273 real cells failed.
- Do not accept a fix reverse-engineered from the target dataset. The ADURN rule came from the data; get it written down.
Sources
- CDISC SDTM/ADaM pilot project (CDISC terms of use; used unmodified, not redistributed)
Scripts
- R code, as the AI wrote it
- Runner and comparison (not AI code)
- Licence (MIT)
- Expected console output: compare_after_two_fixes.txt
- Expected console output: compare_as_written.txt
- Expected console output: first_run_error.txt
- Expected console output: toy_test.txt
- Full input used here
- Record of the earlier run (JSON; the page above reflects the newer check)
- Scripts to rerun this 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.