CDISC and SDTM Data in R
SDTM organizes collected study observations into named domains. DM holds one row per
subject, EX holds exposure, AE holds adverse events, and LB holds laboratory
measurements. These structures connect through the subject identifier USUBJID.
The ADaM lesson derives analysis variables from
the same observations.
This lesson uses the independent synthetic study BTISYN01. Its observations,
medicine names, and event categories are invented for method instruction. They do
not describe a clinical trial or support a clinical conclusion. The
study specification
and generator
define the records without CDISC pilot data or package example datasets. The fixture
data use CC0 1.0.
The companion runner mounts the shared fixtures at /opt/fixtures/pharmaverse.
The RDS file preserves dates and column types, and the adjacent CSV files expose
each source table for inspection. The fixture manifest records their SHA-256 hashes.
Download the shared synthetic study files for the generator, specification, fixtures, and data license.
library(dplyr)
study <- readRDS("/opt/fixtures/pharmaverse/synthetic_study.rds")domains <- c("dm", "vs", "ae", "lb", "ex", "mh", "ds", "cm", "eg", "ts")domain_sizes <- data.frame( domain = domains, rows = vapply(study[domains], nrow, integer(1)), columns = vapply(study[domains], ncol, integer(1)))print(domain_sizes, row.names = FALSE) domain rows columns dm 36 10 vs 36 8 ae 152 10 lb 432 13 ex 60 8 mh 36 6 ds 36 6 cm 36 7 eg 36 8 ts 5 5DM holds one row per subject
Section titled “DM holds one row per subject”The planned ARM and actual ACTARM can differ. Subjects 1 and 2 exchange actual
arms in this synthetic specification. Three assigned subjects remain untreated,
and three other subjects are screen failures. These records remain in DM because
the domain describes enrolled subjects, including subjects outside the safety
population.
dm <- study$dm
table(dm$ARM)sub_dm <- dm[1:4, c("USUBJID", "SUBJID", "SITEID", "AGE", "SEX", "ARM", "ACTARM")]print(as.data.frame(sub_dm), row.names = FALSE) Active High Active Low Placebo Screen Failure 11 11 11 3 USUBJID SUBJID SITEID AGE SEX ARM ACTARM BTISYN01-001 001 S01 26 F Placebo Active Low BTISYN01-002 002 S01 27 M Active Low Placebo BTISYN01-003 003 S01 28 F Active High Active High BTISYN01-004 004 S01 29 M Placebo PlaceboTS records trial-level facts instead of subject observations. These invented values describe the design specified for the example.
ts <- study$ts
ts[ts$TSPARMCD %in% c("AGEMIN", "AGEMAX", "TBLIND", "TCNTRL", "ADDON"), c("TSPARMCD", "TSVAL")]# A tibble: 5 × 2 TSPARMCD TSVAL <chr> <chr>1 AGEMIN 25 years2 AGEMAX 65 years3 TBLIND DOUBLE BLIND4 TCNTRL PLACEBO5 ADDON NFindings preserve original and standardized results
Section titled “Findings preserve original and standardized results”LB holds one row per subject, test, and visit. LBORRES preserves the collected
value as text, while LBSTRESN holds the numeric standardized result. Here albumin
converts from g/dL to g/L, so the standardized value is ten times the original.
lb <- study$lb
alb <- lb[lb$USUBJID == "BTISYN01-001" & lb$LBTESTCD == "ALB", ]alb[, c("USUBJID", "LBTESTCD", "LBORRES", "LBORRESU", "LBSTRESN", "LBSTRESU", "VISIT", "LBDY")]# A tibble: 4 × 8 USUBJID LBTESTCD LBORRES LBORRESU LBSTRESN LBSTRESU VISIT LBDY <chr> <chr> <chr> <chr> <dbl> <chr> <chr> <dbl>1 BTISYN01-001 ALB 3.7 g/dL 37 g/L SCREENING 1 -72 BTISYN01-001 ALB 3.8 g/dL 38 g/L SCREENING 2 -13 BTISYN01-001 ALB 3.9 g/dL 39 g/L WEEK 1 84 BTISYN01-001 ALB 4 g/dL 40 g/L WEEK 2 15AE records individual events. A subject can have repeated events with the same term, which matters when the later table counts subjects rather than event rows. The labels below are invented study categories. They do not reproduce a MedDRA hierarchy and are not suitable as submission terminology.
ae <- study$ae
table(ae$AESEV)one_ae <- ae[1, c("AETERM", "AEDECOD", "AEBODSYS", "AESEV", "AESTDTC", "AESTDY")]print(as.data.frame(one_ae), row.names = FALSE) MILD MODERATE SEVERE 75 54 23 AETERM AEDECOD AEBODSYS AESEV AESTDTC Synthetic event D Synthetic event D Invented study category MILD 2025-01-05 AESTDY -5Supplemental qualifiers hold additional facts
Section titled “Supplemental qualifiers hold additional facts”A supplemental domain records facts that do not have a standard variable in the
parent domain. SUPPDM connects to DM through RDOMAIN, USUBJID, IDVAR, and
IDVARVAL. The name and value occupy QNAM and QVAL. This fixture adds one
study-specific demonstration flag per subject.
suppdm <- study$suppdm
count(suppdm, QNAM)sub_supp <- suppdm[1:3, c("STUDYID", "RDOMAIN", "USUBJID", "QNAM", "QVAL", "QLABEL")]print(as.data.frame(sub_supp), row.names = FALSE)# A tibble: 1 × 2 QNAM n <chr> <int>1 DEMOFL 36 STUDYID RDOMAIN USUBJID QNAM QVAL QLABEL BTISYN01 DM BTISYN01-001 DEMOFL Y Synthetic demonstration record BTISYN01 DM BTISYN01-002 DEMOFL Y Synthetic demonstration record BTISYN01 DM BTISYN01-003 DEMOFL Y Synthetic demonstration recordMap a raw medication extract with sdtm.oak
Section titled “Map a raw medication extract with sdtm.oak”Raw records often use names and date formats that differ from the target domain.
sdtm.oak expresses each conversion as a separate mapping operation. This example
reads an invented medication extract and a small study dictionary from the same
fixture. The dictionary maps milligram to mg and gram to g; its identifiers
BTI_UNIT and BTI_TIME are local study identifiers, not CDISC codelist codes.
library(sdtm.oak)
cm_raw <- as.data.frame(study$cm_raw)study_ct <- as.data.frame(study$study_ct)cm_raw[1:3, c("PATNUM", "MDRAW", "DOSU", "MDBDR", "MDBTM", "MDPRIOR")] PATNUM MDRAW DOSU MDBDR MDBTM MDPRIOR1 1 Synthetic medicine A milligram 01-Jan-2025 08:00 12 1 Synthetic medicine B gram 11-Jan-2025 09:00 03 2 Synthetic medicine C milligram 12-Jan-2025 10:00 0cm_raw <- generate_oak_id_vars(cm_raw, pat_var = "PATNUM", raw_src = "cm_raw")
cm <- assign_no_ct( raw_dat = cm_raw, raw_var = "MDRAW", tgt_var = "CMTRT") %>% assign_ct( raw_dat = cm_raw, raw_var = "DOSU", tgt_var = "CMDOSU", ct_spec = study_ct, ct_clst = "BTI_UNIT", id_vars = oak_id_vars() ) %>% assign_datetime( raw_dat = cm_raw, raw_var = c("MDBDR", "MDBTM"), tgt_var = "CMSTDTC", raw_fmt = c(list(c("d-m-y", "dd mmm yyyy")), "H:M"), raw_unk = c("UN", "UNK"), id_vars = oak_id_vars() ) %>% hardcode_ct( raw_dat = condition_add(cm_raw, MDPRIOR == "1"), raw_var = "MDPRIOR", tgt_var = "CMSTRTPT", tgt_val = "BEFORE", ct_spec = study_ct, ct_clst = "BTI_TIME", id_vars = oak_id_vars() )
cm[1:4, c("oak_id", "patient_number", "CMTRT", "CMDOSU", "CMSTDTC", "CMSTRTPT")]# A tibble: 4 × 6 oak_id patient_number CMTRT CMDOSU CMSTDTC CMSTRTPT <int> <int> <chr> <chr> <iso8601> <chr>1 1 1 Synthetic medicine A mg 2025-01-01T08:00 BEFORE2 2 1 Synthetic medicine B g 2025-01-11T09:00 <NA>3 3 2 Synthetic medicine C mg 2025-01-12T10:00 <NA>4 4 3 Synthetic medicine D mg 2025-01-13T11:00 <NA>generate_oak_id_vars attaches a row identifier that the mapped variables retain.
assign_no_ct copies the medicine name, and assign_ct maps its unit through the
study dictionary. assign_datetime joins the date and time into an ISO 8601 value.
hardcode_ct assigns BEFORE only where the prior-medication flag is set.
The retained row identifier makes each result traceable to its input. A production mapping also needs reviewed study metadata, applicable controlled terminology, and cross-domain checks. This example demonstrates the mapping calls without claiming a complete submission dataset.
Scientific references
Section titled “Scientific references”The sdtm.oak package by Ganapathy and colleagues supplies the SDTM data transformation engine.
Further reading
Section titled “Further reading”-
sdtm.oak intervention mappings
The vignette follows raw medication rows through identifiers, topic variables, controlled terms, and partial dates. It explains the method adapted here.
-
The specification defines subject assignments, invented observations, and expected cases. The generator and fixture manifest make this example reproducible.
-
CDISC describes the standard and its domain structure. The synthetic fixture demonstrates selected structures and does not claim submission compliance.