CDISC dataset migration

ADaM Programming in R for SAS Teams

Translate common ADaM derivation patterns from SAS to R while keeping timing, baseline windows, population flags, and sponsor-specific conventions visible for review.

Try the SAS to R converter

Example

Derive treatment start variables and a safety flag for ADSL

SAS

data ex_ext;
  set ex;
  /* Parse complete ISO datetimes; no imputation is applied. */
  if not missing(exstdtc) then exstdtm = input(exstdtc, e8601dt.);
  if not missing(exstdtm);
run;

proc sort data=ex_ext; by studyid usubjid exstdtm; run;
proc sort data=dm; by studyid usubjid; run;

data adsl;
  merge dm(in=in_dm) ex_ext(in=in_ex keep=studyid usubjid exstdtm);
  by studyid usubjid;
  retain trtsdtm saffl dm_exists;
  if first.usubjid then do;
    trtsdtm = .;
    saffl = 'N';
    dm_exists = 0;
  end;
  if in_dm then dm_exists = 1;
  if dm_exists and in_ex then do;
    saffl = 'Y';
    if missing(trtsdtm) then trtsdtm = exstdtm;
  end;
  if last.usubjid and dm_exists then output;
  drop dm_exists;
run;

R

library(admiral)
library(dplyr)
library(rlang)

ex_ext <- derive_vars_dtm(
  ex,
  dtc = EXSTDTC,
  new_vars_prefix = 'EXST',
  highest_imputation = 'n'
)

adsl <- dm |>
  derive_vars_merged(
    dataset_add = ex_ext,
    by_vars = exprs(STUDYID, USUBJID),
    new_vars = exprs(TRTSDTM = EXSTDTM),
    order = exprs(EXSTDTM),
    filter_add = !is.na(EXSTDTM),
    mode = 'first'
  ) |>
  derive_var_merged_exist_flag(
    dataset_add = ex_ext,
    by_vars = exprs(STUDYID, USUBJID),
    new_var = SAFFL,
    condition = !is.na(EXSTDTM),
    true_value = 'Y',
    false_value = 'N',
    missing_value = 'N',
    filter_add = !is.na(EXSTDTM)
  )

What to validate after conversion

  • Make analysis windows and sort order explicit.
  • Keep population-flag logic readable and testable.
  • Compare derivations with planned QC checks before release.

Always validate analysis timing, datetime parsing, sort order, treatment selection, population flags, and sponsor conventions against the SAS output and your study standards.

Frequently asked questions

Why does ordering matter for ADaM conversions?

SAS programs can rely on input order and BY-group boundaries. In R, sorting and grouping should be explicit so that baseline or last-record selection is reproducible.

Can a converted ADaM derivation be assumed equivalent to SAS?

No. Compare planned record-level outputs and summary checks, especially around dates, windows, imputations, and population rules.