StatAgentWeekly email

R script that recomputes a CSR table and flags mismatches

Paste the ADSL variables and a CSR table, get an R script that recomputes each derivable cell and prints MATCH or MISMATCH.

About 20 minR, Excel and WordWorked example, recomputed

What you get

80 OF 80 CHECKED CELLS MATCH; 16 RACE CELLS NEED A 2ND PROMPT (THEN 96 OF 96)

Table 14-2.01

Summary of Demographic and Baseline Characteristics

Population: Intent-to-Treat

RowPlacebo(N=86)Xanomeline Low Dose(N=84)Xanomeline High Dose(N=84)Total(N=254)
Age (y)
n868484254
Mean75.275.774.475.1
SD8.598.297.898.25
Median76.077.576.077.0
Min52.051.056.051.0
Max89.088.088.089.0
Age group
<65 yrs14 (16%)8 (10%)11 (13%)33 (13%)
65-80 yrs42 (49%)47 (56%)55 (65%)144 (57%)
>80 yrs30 (35%)29 (35%)18 (21%)77 (30%)
Sex
Male33 (38%)34 (40%)44 (52%)111 (44%)
Female53 (62%)50 (60%)40 (48%)143 (56%)
Race (origin)
Caucasian75 (87%)72 (86%)71 (85%)218 (86%)
African Descent8 (9%)6 (7%)9 (11%)23 (9%)
Hispanic3 (3%)6 (7%)3 (4%)12 (5%)
Other001 (1%)1 (<1%)
Baseline weight (kg)
n868384253
Mean62.867.370.066.6
SD12.7714.1214.6514.13
Median60.664.969.266.7
Min34.045.441.734.0
Max86.2106.1108.0108.0
Baseline BMI group
<2559 (69%)47 (56%)44 (52%)150 (59%)
25-<3021 (24%)27 (32%)28 (33%)76 (30%)
>=306 (7%)10 (12%)12 (14%)28 (11%)

1. Recomputed by recompute_table_14-2.01.R. Not recomputed: p-values, MMSE, duration of disease, education, height.

Swipe sideways to see every column.

80 of 80 checked cells matched from the prompt above. The 16 Race cells needed a second prompt in which the reader supplies the rule (Hispanic ethnicity takes precedence); then 96 of 96 matched.

Draft for review. Public data only.

Get the next workflow by email

The prompt

Write an R script that recomputes the CSR table below from adam_adsl.xpt and checks every cell.
Use haven and dplyr, population ITTFL = "Y", the four columns shown, percents on the column N, round half up.
Take the CSR values from the table below. Print each cell as MATCH or MISMATCH, then how many cells matched.
If a row cannot be derived from the variables listed, say so; do not guess.

[paste the variable list and the CSR table here]

Works in any chat assistant that writes R; not tested in Microsoft 365 Copilot

Sample input used here

ADSL variables and CSR Table 14-2.01, CDISCPILOT01

USUBJID  Unique Subject Identifier | text | DM.USUBJID
TRT01P   Planned Treatment for Period 01 | text | DM.ARM (values: Placebo, Xanomeline Low Dose, Xanomeline High Dose)
ITTFL    Intent-To-Treat Population Flag | text | Y if ARMCD ne ' '. N otherwise
SAFFL    Safety Population Flag | text | Y if ITTFL='Y' and TRTSDT ne missing. N otherwise
AGE      Age | integer | DM.AGE
AGEGR1   Pooled Age Group 1 | text | derived from AGEGR1N (values: <65, 65-80, >80)
SEX      Sex | text | DM.SEX (M, F)
RACE     Race | text | DM.RACE
...
CSR Table 14-2.01 values for each row follow (see input_paste.txt)

CDISC pilot project define.xml and CSR (PDF pages 46 to 48), credited to CDISC.

How this was checked

Result

Generated by Claude on 2026-10-22.

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.

Reply 1 (cold)R script checked 80 cells, 80 of 80 matched when run unedited.
Race16 Race cells declined as not derivable; no guess. After the reader supplied the ethnicity rule, reply 2 added the block and 96 of 96 matched.
Independent checkSeparate Python recomputation of all 96 cells agrees with the R output.
Not checkedp-values and four other row groups were not recomputed.

Cold re-run of this prompt on 2026-10-22. Cold run of the short prompt (no tools, nothing executed by the model): 80 of 80 checked cells matched, 16 Race cells declined. With the ethnicity rule supplied in a second turn: 96 of 96. The earlier 2026-10-19 Python run reached 96 of 96 after the model itself found the ETHNIC rule from the mismatch; the new run did not find it unaided.

A one-line prompt for comparison

Prompt: QC the demographics table using the data

I don't have the demographics table or the dataset in front of me, so here is a general approach to QC-ing a demographics table, plus a template you can adapt.

Steps
1. Confirm the analysis population (for example the safety population, SAFFL = "Y") and the treatment variable (TRT01P or TRT01A).
2. Recompute counts and percentages for categorical variables (SEX, RACE, ETHNIC, AGEGR1) and mean, SD, median, min, max for continuous variables (AGE, WEIGHT, HEIGHT, BMI).
3. Compare to the table programmatically and investigate differences.
4. Check missing values, rounding, and denominators.

Template (Python)

import pandas as pd
adsl = pd.read_sas("adsl.xpt")
adsl = adsl[adsl["SAFFL"] == "Y"]
age = adsl.groupby("TRT01P")["AGE"].agg(["count","mean","std","median","min","max"]).round(1)
...

All 5 checks

  1. One subject has no weight or BMI but BMIBLGR1 is <25, so the BMI groups sum to 254 while n is 253. The CSR agrees; worth a query.
  2. P-values and four other row groups were not recomputed. Say so in the QC log.

Second prompt, for the 16 Race cells

Add the Race rows (Caucasian, African Descent, Hispanic, Other) to the script.
Rule: Hispanic ethnicity takes precedence over race, so classify on ETHNIC first, then RACE.
Print MATCH or MISMATCH for these cells and give the new total.

The model declined these cells on the first prompt because Hispanic is not a RACE value. The rule is inferred from the counts, not written in the CSR; confirm it in the SAP. The exact wording used in the run was not kept; this is the same rule in a short form.

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.