R translation of a SAS program, checked against the SAS output
Paste a SAS program, get an R translation with a column-by-column comparison; run it and see what breaks.
About 30 minRWorked example, recomputed
What you get
# Run 1: Rscript run.R <data folder> (the AI's code, unedited)
Error: object 'AEDECOD' not found
Execution halted
# The fix: one rename, 6 places on 4 lines (the helper column .d was matched to mutate()'s .data argument)
- .d = coalesce(str_trim(AEDECOD), ""),
+ aedecod_trim = coalesce(str_trim(AEDECOD), ""),
# Run 2: Rscript run_fixed.R <data folder>
rows SAS: 1191 rows R: 1191
only in SAS: ADURN ADURU
only in R: ADUR
# A tibble: 0 × 2
# ℹ 2 variables: variable <chr>, n_diff <int>
# Empty table = no differing variable in the 53 columns the two files share.
# The code does not pair ADUR (R) with ADURN (SAS file). check_extra.R does: 0 of 1191 rows differ.Swipe sideways to see every column.
Left to right in time: the AI's R as first run (it stopped), the one-line fix, the run that matches adam_adae.xpt. R 4.6.1, 2026-10-24.
Draft for review. Public data only.
The prompt
Translate the SAS program below to R (tidyverse, haven). Keep the logic and step order; do not improve it.
Above each line where SAS and R can differ (missing values, blanks, dates, sort ties, merges), comment how.
If you are unsure SAS and R match at a line, say so. Invent nothing; list gaps under QUESTIONS.
Then write R code that compares your result with the SAS output dataset: row counts, keys, every column.
End with the three places most likely to differ from SAS.
[paste the SAS program and the names of the input files and the SAS output dataset here]Works in any chat assistant that writes R; not tested in Microsoft 365 Copilot
Sample input used here
SAS program adam_adae.sas (lines shown), CDISCPILOT01
proc sort data=sdtmnew.AE OUT=AE; by USUBJID AESEQ; run; data AE; set AE; by USUBJID AESEQ; ASTDT=input(strip(AESTDTC),?? yymmdd10.); AENDT=input(strip(AEENDTC),?? yymmdd10.); if ASTDT>.z and AENDT>.z then ADUR=AENDT-ASTDT+1; format ASTDT AENDT DATE9.; run; proc sort data=adamnew.ADSL OUT=TEMP(keep=SITEID USUBJID TRT01A TRT01AN AGE AGEGR1 AGEGR1N RACE RACEN SEX SAFFL TRTSDT TRTEDT RENAME=(TRT01A=TRTA TRT01AN=TRTAN)); by USUBJID; run; data AE; merge AE(in=inAE) TEMP; by USUBJID;
CDISC pilot project program, credited to CDISC. Blank lines and commented-out code removed; the full paste is in the download.
How this was checked
Result
Generated by Claude on 2026-10-19; R code run in R 4.6.1 on 2026-10-20 and 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-19).
Re-check on 2026-10-24. Published scripts re-run with R 4.6.1: run.R stops with the same error, run_fixed.R and check_extra.R give the same output as the saved run. Code only: the AI answer was not re-generated.
The longer prompt that was checked
Translate the SAS program below to R (tidyverse; haven for the xpt files). It builds the ADAE dataset from SDTM AE and ADSL. 1. Preserve the logic exactly, including the order of steps. Do not improve it. 2. Above every line where SAS and R can behave differently (missing values in comparisons, character blanks, dates, sort order and ties, merge semantics), add a comment saying which way SAS behaves and what your R does. 3. If you are not sure SAS and R match at a line, say so in a comment. Do not claim equivalence you have not checked. 4. Do not invent variables or results. Anything ambiguous or missing from the excerpt (for example setup.sas) goes under QUESTIONS. 5. Then write a second R block that compares your result with the SAS output adam_adae.xpt: row counts, key uniqueness, then every column, with the tolerance you choose and why. Output: (a) R code, (b) assumptions, (c) QUESTIONS, (d) comparison code, (e) the three places you consider most likely to differ from SAS. SAS CODE AND DATA NOTES: [excerpt below]
A one-line prompt for comparison
Lazy request: 'convert our ADAE program, make sure the R matches'
Generic dplyr code with invented file and column names; claimed the counts would match.
Not run for this page; the verified flagship shows it failing on invented names.
All 5 checks
- Ask which program version made the SAS output. The file has ADURN and ADURU where the program has ADUR, so the comparison had to be paired by hand.
- Do not accept 'identical logic' in a comment. Every SAS-versus-R claim is a hypothesis until the comparison shows zero differences.
Sources
- CDISC SDTM/ADaM pilot project (CDISC terms of use; used unmodified, not redistributed)
Scripts
- First run, as the AI wrote it
- After the one rename
- Extra checks (not AI code)
- Licence (MIT)
- Expected console output: check_extra_output.txt
- Expected console output: run_fixed_output.txt
- Expected console output: run_output.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.