StatAgentWeekly email

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

3
B8fx1187
ABCDEF
1VariableMatching recordsRecordsPercentAfter two fixesCause of mismatch
2AESEQ11911191100.0%1191
3ASTDT11911191100.0%1191
4ASTDTF11911191100.0%1191
5AENDT11911191100.0%1191
6ASTDY11911191100.0%1191
7AENDY11911191100.0%1191
8ADURN1187119199.7%1191ADAE leaves duration missing when the start day was imputed; not in the define text
9ADURU1187119199.7%1191same 4 records as ADURN
10TRTEMFL11911191100.0%1191
11AOCCFL11911191100.0%1191
12AOCCSFL11911191100.0%1191
13AOCCPFL11911191100.0%1191
14AOCC02FL11911191100.0%1191
15CQ01NAM1010119184.8%1191define text has 'SUBC UTANEOUS'; the data value is 'SUBCUTANEOUS'
16AOCC01FL1107119192.9%1191knock-on of CQ01NAM
cell_matchShowing 15 of 15 rows

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.

Get the next workflow by email

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 DISOR

CDISC 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.

First runStopped: can only subtract from "Date" objects. TRTSDT arrives from haven as a Date.
After the date conversion17,592 of 17,865 cells match (98.47%). 11 of 15 variables match on all 1,191 records.
Why not 100%CQ01NAM 181 and AOCC01FL 84: a typo in the define text. ADURN and ADURU 4 each: a duration rule found only in the data.
After two fixes17,865 of 17,865 cells match.
Independent checkA separate Python recomputation (written for the earlier Python run) gives the same count for every variable.
LimitsOne cold run. The agent was told not to use tools; this is not technically enforced. ADSL and ADTTE rows were not attempted.

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

  1. Tests built from the spec share its blind spots. All 9 hand-derived test rows passed while 273 real cells failed.
  2. Do not accept a fix reverse-engineered from the target dataset. The ADURN rule came from the data; get it written down.

Sources

Scripts

Related workflows

All workflows. How we check. Public data only. Drafts for review, not validated output.

Get one workflow a week

Unsubscribe anytime. Privacy.