{
  "id": "define-derivation-pseudocode",
  "title": "Turn define.xml derivation text into pseudocode and runnable code, then check it against the ADaM data",
  "study": {
    "name": "CDISCPILOT01 (CDISC SDTM/ADaM Pilot Project, xanomeline TTS in Alzheimer's disease)",
    "source_url": "https://github.com/cdisc-org/sdtm-adam-pilot-project",
    "licence_note": "Datasets, define.xml and programs are CDISC Pilot Project materials, used unmodified and attributed to CDISC under the Terms of Use in the repository README (no modification, no sale of the data, attribution required). The study is a CDISC mock submission with fictional dates. Not legal advice."
  },
  "task": "Convert the ADAE derivation comments in adam_define.xml (analysis dates, relative days, duration, treatment-emergent flag, dermatological query and first-occurrence flags) into numbered pseudocode and pandas code, run it on sdtm_ae.xpt and adam_adsl.xpt, and compare every derived value with adam_adae.xpt.",
  "time_to_try_minutes": {
    "minutes": 30,
    "label": "estimate, not measured: about 30, including pasting the derivation text and reading the ambiguity list yourself"
  },
  "good": {
    "input_excerpt": "[adam_define.xml, ADAE ItemDef comments; typographic quotes normalised]\nTRTSDT (Date of First Exposure to Treatment): ADSL.TRTSDT\nASTDT (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 missing no imputation is performed as these dates clearly indicate a start prior to the beginning of treatment. There are no events with completely missing start dates.\nASTDTF (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.\nAENDT (Analysis End Date): AE.AEENDTC, converted to a numeric SAS date\nASTDY (Analysis Start Relative Day): IF ASTDT>=TRTSDT>MISSING then ASTDY=ASTDT-TRTSDT+1 Else if TRTSDT>ASTDT>MISSING then ASTDY=ASTDT-TRTSDT\nAENDY (Analysis End Relative Day): IF AENDT>=TRTSDT>MISSING then AENDY=AENDT-TRTSDT+1 Else if TRTSDT>AENDT>MISSING then AENDY=AENDT-TRTSDT\nADURN (AE Duration (N)): ADURN=AENDT-ASTDT+1\nADURU (AE Duration Units): If ADURN is not missing then ADURU='DAY'\nTRTEMFL (Treatment Emergent Analysis Flag): If ASTDT >= TRTSDT > . then TRTEMFL='Y'. Otherwise TRTEMFL='N'\nAOCCFL (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\nAOCCSFL (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 Subject and SOC\nAOCCPFL (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 record (set AOCCPFL='Y') within each Subject, SOC, and PT\nAOCC02FL (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\nCQ01NAM (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 SWEAT', 'HYPERHIDROSIS', 'ALOPECIA') then CQ01NAM='DERMATOLOGIC EVENTS' Otherwise CQ01NAM=NULL\nAOCC01FL (1st Occurrence 01 Flag for CQ01): Subset to CQ01NAM='' and TRTEMFL='Y' and sort by Subject (USUBJID), Start Date (ASTDT), and Sequence Number (AESEQ) and flag the first record (set AOCC01FL='Y') within each Subject (Flag First Treatment Emergent Dermatological Event for Time to Event Analysis)\n[Input files: sdtm_ae.xpt, one row per adverse event (USUBJID, AESEQ, AEDECOD, AEBODSYS, AESER, AESTDTC, AEENDTC); adam_adsl.xpt, one row per subject (USUBJID, TRTSDT).]",
    "input_excerpt_words": 434,
    "prompt": "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.\n\n[derivation text and input file notes pasted here]",
    "output": "PSEUDOCODE (cold draft, written from the excerpt only)\n\nInputs: AE = sdtm_ae.xpt (one row per AE), ADSL = adam_adsl.xpt (one row per subject).\nStep 0. d = AE left-joined to ADSL on USUBJID, keeping ADSL.TRTSDT.\nStep 1. ASTDT, ASTDTF\n  s = first 10 characters of AESTDTC.\n  if s is YYYY-MM-DD: ASTDT = that date; ASTDTF = blank.\n  else if s is YYYY-MM: ASTDT = YYYY-MM-01; ASTDTF = 'D'.\n  else (year only or empty): ASTDT = missing, no imputation, ASTDTF blank.\nStep 2. AENDT = AEENDTC as a date if it is a full YYYY-MM-DD, else missing (the text gives no rule for partial end dates).\nStep 3. ASTDY: if ASTDT and TRTSDT are both non-missing: ASTDT >= TRTSDT gives ASTDT - TRTSDT + 1, else ASTDT - TRTSDT (no day 0). Otherwise missing. AENDY: same with AENDT.\nStep 4. ADURN = AENDT - ASTDT + 1 when both are non-missing, else missing. ADURU = 'DAY' when ADURN is non-missing, else blank.\nStep 5. TRTEMFL = 'Y' if ASTDT and TRTSDT are non-missing and ASTDT >= TRTSDT, else 'N'.\nStep 6. CQ01NAM = 'DERMATOLOGIC EVENTS' if AEDECOD contains APPLICATION, DERMATITIS, ERYTHEMA or BLISTER, or if AEBODSYS equals the SOC string in the text and AEDECOD is not COLD SWEAT, HYPERHIDROSIS or ALOPECIA; else blank.\nStep 7. First-occurrence flags. Keep rows with TRTEMFL='Y' (plus the extra condition for each flag), sort by the keys listed, take the first row of each group, set the flag to 'Y' on it, blank elsewhere.\n  AOCCFL: group USUBJID, sort ASTDT, AESEQ.\n  AOCCSFL: group USUBJID + AEBODSYS.\n  AOCCPFL: group USUBJID + AEBODSYS + AEDECOD.\n  AOCC02FL: also AESER='Y', group USUBJID.\n  AOCC01FL: see ambiguity 1.\n\nAMBIGUITIES I FLAGGED (not silently resolved)\n1. AOCC01FL says \"Subset to CQ01NAM=''\" but its label and its parenthetical say it flags the first treatment-emergent dermatological event. Taken literally it would flag non-dermatological events. I used CQ01NAM non-blank.\n2. The SOC string in the CQ01NAM rule is written 'SKIN AND SUBC UTANEOUS TISSUE DISORDERS' with a space inside SUBCUTANEOUS. It may be a typo. I implemented the text as written, and the last line of the run output (distinct SOC values containing SKIN) shows the dataset spells it SUBCUTANEOUS, so this branch matched nothing. I did NOT correct it in this run, so CQ01NAM and AOCC01FL are probably undercounted.\n3. A partial AEENDTC has no rule. I left AENDT missing, which also makes ADURN missing.\n4. A start date with year only gets no imputation, so ASTDT is missing and TRTEMFL is 'N'. The text says no start dates are completely missing; year-only dates are partial, not missing, so this is consistent with it.\n5. The text does not say how ties in ASTDT are broken beyond AESEQ, or what happens if TRTSDT is missing (I made TRTEMFL 'N').\n\nRUN (python -I derive_adae.py sdtm_ae.xpt adam_adsl.xpt out.csv; one run, no edits afterwards)\nrecords: 1191\nTRTEMFL=Y: 1126\nASTDTF = flagged: 15\nAOCCFL = flagged: 218\nAOCCSFL = flagged: 550\nAOCCPFL = flagged: 781\nAOCC02FL = flagged: 3\nAOCC01FL = flagged: 118\nCQ01NAM nonblank: 312\nASTDT missing: 11  AENDT missing: 473\nAEBODSYS values containing SKIN: ['SKIN AND SUBCUTANEOUS TISSUE DISORDERS']\n\nCODE (derive_adae.py, verbatim):\n\n\"\"\"ADAE derivations from define.xml text, cold draft. Usage: python -I derive_adae.py sdtm_ae.xpt adam_adsl.xpt out.csv\"\"\"\nimport sys\nimport numpy as np\nimport pandas as pd\n\nae = pd.read_sas(sys.argv[1], format=\"xport\", encoding=\"latin1\")\nadsl = pd.read_sas(sys.argv[2], format=\"xport\", encoding=\"latin1\")\n\ndef to_days(s):\n    \"\"\"TRTSDT: SAS date -> days since 1960-01-01 as float (NaN if missing).\"\"\"\n    if np.issubdtype(s.dtype, np.datetime64):\n        return (s - pd.Timestamp(\"1960-01-01\")).dt.days.astype(float)\n    return s.astype(float)\n\nadsl = adsl.assign(TRTSDT=to_days(adsl[\"TRTSDT\"]))\nd = ae.merge(adsl[[\"USUBJID\", \"TRTSDT\"]], on=\"USUBJID\", how=\"left\")\n\nEPOCH = pd.Timestamp(\"1960-01-01\")\n\ndef parse_start(s):\n    \"\"\"Return (SAS date number, imputation flag). Day missing -> '01' and flag 'D'.\n    Month and day missing -> no imputation, date stays missing. Time part ignored.\"\"\"\n    s = (s or \"\").strip()[:10]\n    parts = s.split(\"-\")\n    if len(parts) == 3 and len(parts[2]) == 2:\n        dt = pd.to_datetime(s, errors=\"coerce\"); flag = \"\"\n    elif len(parts) == 2:\n        dt = pd.to_datetime(s + \"-01\", errors=\"coerce\"); flag = \"D\"\n    else:\n        return np.nan, \"\"\n    return ((dt - EPOCH).days if pd.notna(dt) else np.nan), flag\n\ndef parse_end(s):\n    \"\"\"Plain conversion; a partial end date is left missing (the text gives no imputation rule).\"\"\"\n    s = (s or \"\").strip()[:10]\n    if len(s) == 10:\n        dt = pd.to_datetime(s, errors=\"coerce\")\n        return (dt - EPOCH).days if pd.notna(dt) else np.nan\n    return np.nan\n\nst = d[\"AESTDTC\"].map(parse_start)\nd[\"ASTDT\"] = st.map(lambda t: t[0]); d[\"ASTDTF\"] = st.map(lambda t: t[1])\nd[\"AENDT\"] = d[\"AEENDTC\"].map(parse_end)\n\ndef rel_day(x, ref):\n    # SAS: x>=ref>missing -> x-ref+1 ; ref>x>missing -> x-ref ; otherwise missing\n    out = pd.Series(np.nan, index=x.index)\n    ok = x.notna() & ref.notna()\n    out[ok & (x >= ref)] = (x - ref + 1)[ok & (x >= ref)]\n    out[ok & (x < ref)] = (x - ref)[ok & (x < ref)]\n    return out\nd[\"ASTDY\"] = rel_day(d[\"ASTDT\"], d[\"TRTSDT\"])\nd[\"AENDY\"] = rel_day(d[\"AENDT\"], d[\"TRTSDT\"])\nd[\"ADURN\"] = d[\"AENDT\"] - d[\"ASTDT\"] + 1\nd[\"ADURU\"] = np.where(d[\"ADURN\"].notna(), \"DAY\", \"\")\n\nd[\"TRTEMFL\"] = np.where(d[\"ASTDT\"].notna() & d[\"TRTSDT\"].notna() & (d[\"ASTDT\"] >= d[\"TRTSDT\"]), \"Y\", \"N\")\n\n# CQ01NAM\ndec = d[\"AEDECOD\"].fillna(\"\").str.upper()\nhit_str = dec.apply(lambda x: any(k in x for k in (\"APPLICATION\", \"DERMATITIS\", \"ERYTHEMA\", \"BLISTER\")))\nhit_soc = (d[\"AEBODSYS\"].fillna(\"\") == \"SKIN AND SUBC UTANEOUS TISSUE DISORDERS\") & ~dec.isin([\"COLD SWEAT\", \"HYPERHIDROSIS\", \"ALOPECIA\"])\nd[\"CQ01NAM\"] = np.where(hit_str | hit_soc, \"DERMATOLOGIC EVENTS\", \"\")\n\ndef first_flag(df, mask, by):\n    \"\"\"Flag first record per `by` after sorting by by + ASTDT + AESEQ, within rows where mask is True.\"\"\"\n    sub = df[mask].sort_values(by + [\"ASTDT\", \"AESEQ\"])\n    first = sub.groupby(by, sort=False).head(1).index\n    return np.where(df.index.isin(first), \"Y\", \"\")\n\nte = d[\"TRTEMFL\"] == \"Y\"\nd[\"AOCCFL\"] = first_flag(d, te, [\"USUBJID\"])\nd[\"AOCCSFL\"] = first_flag(d, te, [\"USUBJID\", \"AEBODSYS\"])\nd[\"AOCCPFL\"] = first_flag(d, te, [\"USUBJID\", \"AEBODSYS\", \"AEDECOD\"])\nd[\"AOCC02FL\"] = first_flag(d, te & (d[\"AESER\"] == \"Y\"), [\"USUBJID\"])\n# AOCC01FL: text says CQ01NAM='' but the label and comment say first dermatologic event, so I used CQ01NAM nonblank (flagged as ambiguity).\nd[\"AOCC01FL\"] = first_flag(d, te & (d[\"CQ01NAM\"] != \"\"), [\"USUBJID\"])\n\ncols = [\"USUBJID\", \"AESEQ\", \"ASTDT\", \"ASTDTF\", \"ASTDY\", \"AENDT\", \"AENDY\", \"ADURN\", \"ADURU\", \"TRTSDT\",\n        \"TRTEMFL\", \"AOCCFL\", \"AOCCSFL\", \"AOCCPFL\", \"AOCC02FL\", \"CQ01NAM\", \"AOCC01FL\"]\nd[cols].to_csv(sys.argv[3], index=False)\nprint(\"records:\", len(d))\nprint(\"TRTEMFL=Y:\", int((d.TRTEMFL == \"Y\").sum()))\nfor c in [\"ASTDTF\", \"AOCCFL\", \"AOCCSFL\", \"AOCCPFL\", \"AOCC02FL\", \"AOCC01FL\"]:\n    print(c, \"= flagged:\", int((d[c] != \"\").sum()))\nprint(\"CQ01NAM nonblank:\", int((d.CQ01NAM != \"\").sum()))\nprint(\"ASTDT missing:\", int(d.ASTDT.isna().sum()), \" AENDT missing:\", int(d.AENDT.isna().sum()))\nprint(\"AEBODSYS values containing SKIN:\", sorted(set(x for x in d.AEBODSYS.dropna() if \"SKIN\" in x.upper())))",
    "trim_note": "Output is the complete cold answer: pseudocode, flagged ambiguities, the run's printed lines and the full script as written. The comparison against the CDISC dataset is not part of the cold answer; it is in the verification section and in define-derivation-pseudocode_recompute.py. The excerpt is 14 ADAE variables copied from adam_define.xml (plus TRTSDT) with typographic quotes normalised; the remaining ADAE variables are pass-through or not derived here and were left out. Correction made at verification: the excerpt first saved on this page was the wrong text (the CSR methods SAP and protocol excerpt from a sibling example, pasted by mistake). It has been rebuilt from adam_define.xml. The ItemDef comments are exact; the bracketed note about the input files is a reconstruction, because the exact notes pasted to the cold run were not saved. The cold output and script are unchanged, and they match this rebuilt text (for example the 'SUBC UTANEOUS' string and the AOCC01FL subset wording).",
    "contamination_note": "The bad run was written before any source file was opened. The cold good run used only the excerpt and prompt below. adam_adae.sas and adam_adae.xpt were not opened until the cold script had run once and its output was saved (no edits after the run). The brief and a model file were read earlier, and the assistant has general knowledge of CDISC pilot data. A second run with the define typo fixed was done after the answer key was read and is reported only as verification."
  },
  "bad": {
    "input": "Write pseudocode for the derivations in the define file",
    "output": "I don't have the define file in front of me, so here is a generic pattern for turning define.xml derivations into pseudocode.\n\nFor each derived variable:\n1. Identify the source dataset and variables (for example ADSL, ADAE, SDTM AE).\n2. Write the derivation in plain steps.\n3. Handle missing values and flags.\n\nExample pseudocode (AE analysis dataset):\n\n  READ AE, ADSL\n  MERGE AE with ADSL by USUBJID\n  ASTDT = convert AESTDTC to a date\n  AENDT = convert AEENDTC to a date\n  ASTDY = ASTDT - TRTSDT + 1\n  AENDY = AENDT - TRTSDT + 1\n  TRTEMFL = \"Y\" if ASTDT >= TRTSDT else null\n  AOCCFL = \"Y\" for first record per subject\n  SAFFL carried from ADSL\n\nIf you paste the derivation text from the define file I can adapt this to your study.",
    "what_went_wrong": [
      "No define text, so the assistant described a generic ADAE. Nothing in it is traceable to the file, and it invented one-line rules. It set TRTEMFL to null when an AE starts before treatment, but this define sets 'N'.",
      "It used ASTDY = ASTDT - TRTSDT + 1 for every record. The define uses ASTDT - TRTSDT before treatment start (no day 0). A programmer following the bad answer would get ASTDY wrong for the 54 records that start before treatment.",
      "It flagged AOCCFL on the 'first record per subject' with no treatment-emergent subset, no sort order and no tie-break by AESEQ. The define subsets to TRTEMFL='Y' first.",
      "It said nothing about partial dates. 26 of 1191 start dates are partial (15 month-year, 11 year-only) and the define has a specific rule for them. It also left out ASTDTF, ADURN, ADURU, CQ01NAM, AOCCSFL, AOCCPFL and the serious-event flag.",
      "It ended by asking for the define text, which is the right move but hands the job back with nothing usable."
    ]
  },
  "what_changed": [
    "Input: 'the define file' became 14 verbatim derivation comments with their labels, plus the two input files and the fields in them.",
    "Output: a generic list became numbered steps with missing-value handling for every variable, plus runnable code with per-variable counts.",
    "The prompt asked for ambiguities, contradictions and typos to be reported rather than resolved silently. That produced the AOCC01FL and SOC-string flags.",
    "The run printed a diagnostic (SOC values containing SKIN) that exposed the typo in the define text before any comparison with the dataset.",
    "The weak spot remained: the assistant flagged the typo but still shipped the literal rule, so check the flagged items before accepting the counts."
  ],
  "check_before_trust": [
    "Run the code on the real files and compare every derived variable with the ADaM dataset if you have it. Here 10 of 14 columns matched on all 1191 records and four did not.",
    "Treat every ambiguity the assistant lists as an open question, not a decision. Two of the four mismatches came from an item it flagged but did not resolve.",
    "Look for strings copied from the define into a filter (SOC names, preferred terms). Compare them with the distinct values in the data. A space in the define text silently turned a rule off.",
    "Ask which step runs first. Duration was computed before the partial start date was imputed in the production program, a fact that appears in neither the define nor the excerpt. Ask the programmer or read the program.",
    "Check missing-value behaviour for every comparison, especially SAS rules where a missing value counts as smaller than any number. The assistant handled this correctly here, but confirm it for your own variables.",
    "Do not treat define.xml as the specification of record when it disagrees with the program or the data. In this package the define, the SAS file and the dataset differ on the duration variable names and on one flag's filter text."
  ],
  "verification": {
    "answer_key": "adam_adae.sas (the pilot's ADAE program, read only after the cold answer and its run were saved) and adam_adae.xpt (the CDISC ADAE dataset, used unmodified), compared record by record on USUBJID and AESEQ with define-derivation-pseudocode_recompute.py. Page cites are adam_define.pdf pages.",
    "rows": [
      {
        "claim": "ASTDT: 11 missing (year-only start dates), 15 month-year dates imputed to day 01; all 1191 values equal the dataset",
        "verdict": "correct",
        "cite": "adam_adae.xpt; define ItemDef ADAE.ASTDT (adam_define.pdf p.34); adam_adae.sas lines 13 and 28 to 31"
      },
      {
        "claim": "ASTDTF = 'D' on 15 records",
        "verdict": "correct",
        "cite": "15 of 15 match. adam_adae.sas lines 28 to 31; adam_adae.xpt"
      },
      {
        "claim": "ASTDY and AENDY, including no day 0: all 1191 records match for both",
        "verdict": "correct",
        "cite": "adam_adae.sas lines 33 to 36; define p.34"
      },
      {
        "claim": "AENDT: 718 converted, 473 blank, all equal the dataset",
        "verdict": "correct",
        "cite": "adam_adae.sas line 14; sdtm_ae.xpt has no partial AEENDTC"
      },
      {
        "claim": "TRTEMFL = 'Y' on 1126 of 1191 records; year-only starts get 'N'",
        "verdict": "correct",
        "cite": "0 mismatches. adam_adae.sas lines 40 to 41"
      },
      {
        "claim": "AOCCFL 218, AOCCSFL 550, AOCCPFL 781, AOCC02FL 3: record-by-record match",
        "verdict": "correct",
        "cite": "0 mismatches each. adam_adae.sas lines 54 to 90; define pp 37 to 39"
      },
      {
        "claim": "ADURN = AENDT - ASTDT + 1 (define text), so 4 records with an imputed month-year start and a full end date get 88, 96, 88, 96",
        "verdict": "partly",
        "cite": "Follows the define text, but 4 records differ from the dataset (dataset blank). adam_adae.sas computes duration at lines 13 to 15, before the day-01 imputation at lines 28 to 31. The 4 rows are subject 01-716-1418, AESEQ 5 to 8, AESTDTC 2013-07"
      },
      {
        "claim": "ADURU = 'DAY' when ADURN is non-missing: 718 records",
        "verdict": "partly",
        "cite": "The same 4 records differ (dataset 714). adam_adae.xpt; the SAS file has no ADURU and names the duration ADUR (lines 15, 120), so program and define disagree"
      },
      {
        "claim": "CQ01NAM: SOC string with a space in 'SUBC UTANEOUS' flagged as a likely typo but not corrected; 312 flagged vs 493 in the dataset",
        "verdict": "partly",
        "cite": "Typo spotted and disclosed, but the delivered output is wrong (181 mismatches). adam_adae.sas line 46 uses 'SKIN AND SUBCUTANEOUS TISSUE DISORDERS'; define p.39 has the space"
      },
      {
        "claim": "AOCC01FL: text says CQ01NAM='' but intent is first dermatological event; used non-blank",
        "verdict": "partly",
        "cite": "Correct on the rule; counts wrong downstream (118 vs 152, 84 mismatches) only because of the CQ01NAM typo. adam_adae.sas line 61 (CQ01NAM='DERMATOLOGIC EVENTS'); define p.39"
      },
      {
        "claim": "With only the SOC spelling corrected, CQ01NAM and AOCC01FL both match the dataset on all 1191 records",
        "verdict": "correct",
        "cite": "Checked after the cold run, --fix-soc-typo. define-derivation-pseudocode_recompute.py"
      },
      {
        "claim": "The define text for the AOCC01FL subset (CQ01NAM='') is an error in the define",
        "verdict": "correct",
        "cite": "The program filters on 'DERMATOLOGIC EVENTS'. adam_adae.sas line 61; define p.39"
      },
      {
        "claim": "Define says no start dates are completely missing; year-only dates exist and were left unimputed",
        "verdict": "correct",
        "cite": "11 records have a 4-character AESTDTC and missing ASTDT in the dataset. sdtm_ae.xpt; adam_adae.xpt"
      },
      {
        "claim": "Cold answer: partial AEENDTC has no rule, left missing",
        "verdict": "false gap",
        "cite": "No partial end dates exist in this data, so the question never arises here. sdtm_ae.xpt (AEENDTC is 10 characters or blank)"
      }
    ],
    "errors_found": [
      "CQ01NAM undercounted: 312 flagged against 493 in the dataset (181 mismatches), and AOCC01FL 118 against 152 (84 mismatches). Cause: the define text spells the SOC 'SKIN AND SUBC UTANEOUS TISSUE DISORDERS'. The cold answer flagged the problem in its own diagnostic line but implemented the text literally, so the delivered numbers are wrong. With the spelling fixed both variables match on all 1191 records.",
      "ADURN and ADURU: 4 records (subject 01-716-1418, AESEQ 5 to 8) differ from the dataset. The define text gives a value; the program computes duration before imputing the start day, so the dataset has none. Neither the define nor the excerpt says so. This is a gap in the source, not a reading error.",
      "Define versus program naming: the define says ADURN and ADURU; adam_adae.sas creates ADUR and no ADURU. The dataset has ADURN and ADURU, so the SAS file in the repo is not the exact program that built the xpt. Not detectable from the excerpt.",
      "Define error: the AOCC01FL subset text says CQ01NAM='', the program uses CQ01NAM='DERMATOLOGIC EVENTS'. The cold answer read it correctly from the label."
    ],
    "summary": "From the define text alone, 10 of the 14 derived columns (ASTDT, ASTDTF, AENDT, ASTDY, AENDY, TRTEMFL, AOCCFL, AOCCSFL, AOCCPFL, AOCC02FL) matched the CDISC ADAE dataset on all 1191 records. Four did not: CQ01NAM and AOCC01FL because of a typo inside the define text (flagged but not corrected in the cold run), and ADURN and ADURU on 4 records because the production program imputes the start day after computing duration. The assistant found the typo and the AOCC01FL contradiction itself; it could not have found the duration order from the define alone."
  },
  "run": {
    "generated_by": "Claude (Anthropic), run as a subagent on 2026-10-20",
    "not_tested_in": "Microsoft 365 Copilot"
  },
  "run_summary": [
    {
      "kind": "ok",
      "label": "Matched",
      "text": "10 of 14 derived columns match on all 1,191 records."
    },
    {
      "kind": "fail",
      "label": "Did not match",
      "text": "CQ01NAM and AOCC01FL (a typo inside the define text); ADURN and ADURU on 4 records (the real program computes duration before imputing the start day)."
    },
    {
      "kind": "warn",
      "label": "Found on its own",
      "text": "The typo and the AOCC01FL contradiction were flagged; the duration order cannot be seen from the define alone."
    }
  ]
}