Answers, Part I (chapters 1 to 4)
Each answer has a program that was run, and its output. Try the exercise yourself first. Then compare your numbers with the numbers here. A different program that gives the same numbers is also a right answer.
Chapter 1
Section titled “Chapter 1”Exercise 1-1
Section titled “Exercise 1-1”# Answer 1-1: count the women and men among the randomised subjectslibrary(dplyr)
pharmaverseadam::adsl |> filter(SAFFL == "Y") |> count(SEX)#> # A tibble: 2 x 2#> SEX n#> <chr> <int>#> 1 F 143#> 2 M 111The safety population has 143 women and 111 men: 254 subjects.
Exercise 1-2
Section titled “Exercise 1-2”# Answer 1-2: how many randomised subjects are older than 80?library(dplyr)
pharmaverseadam::adsl |> filter(SAFFL == "Y", AGE > 80) |> count()#> # A tibble: 1 x 1#> n#> <int>#> 1 7777 subjects are older than 80. count() with no column counts all rows. Both conditions are in one filter(), so a row must meet both.
Exercise 1-3
Section titled “Exercise 1-3”# Answer 1-3: without desc(), the first subject of each arm is the youngestlibrary(dplyr)
pharmaverseadam::adsl |> filter(SAFFL == "Y") |> select(USUBJID, ARM, AGE) |> arrange(ARM, AGE, USUBJID) |> group_by(ARM) |> slice_head(n = 1) |> ungroup()#> # A tibble: 3 x 3#> USUBJID ARM AGE#> <chr> <chr> <dbl>#> 1 01-701-1118 Placebo 52#> 2 01-701-1180 Xanomeline High Dose 56#> 3 01-701-1341 Xanomeline Low Dose 51Without desc(), the sort goes from the youngest up, so the first subject of each arm is the youngest: 52 on placebo, 56 on the high dose and 51 on the low dose.
Exercise 1-4
Section titled “Exercise 1-4”# Answer 1-4: subjects with a severe treatment-emergent adverse event, per armlibrary(dplyr)
pharmaverseadam::adae |> filter(TRTEMFL == "Y", AESEV == "SEVERE") |> group_by(TRT01A) |> summarise( records = n(), subjects = n_distinct(USUBJID) )#> # A tibble: 3 x 3#> TRT01A records subjects#> <chr> <int> <int>#> 1 Placebo 6 5#> 2 Xanomeline High Dose 10 8#> 3 Xanomeline Low Dose 25 165 subjects on placebo, 8 on the high dose and 16 on the low dose had a severe treatment-emergent adverse event. The records are more: 6, 10 and 25. A table of subjects reports 5, 8 and 16.
Exercise 1-5
Section titled “Exercise 1-5”# Answer 1-5: the age summary of the low dose armlibrary(dplyr)
arm <- "Xanomeline Low Dose"
pharmaverseadam::adsl |> filter(SAFFL == "Y", ARM == arm) |> summarise( arm = arm, n = n(), mean = round(mean(AGE), 1), min = min(AGE), max = max(AGE) )#> # A tibble: 1 x 5#> arm n mean min max#> <chr> <int> <dbl> <dbl> <dbl>#> 1 Xanomeline Low Dose 84 75.7 51 88The low dose arm has n = 84, a mean age of 75.7, and the oldest subject is 88. Only the value of arm changed. The rest of the program is the same.
Exercise 1-6
Section titled “Exercise 1-6”# Answer 1-6: the date of first treatment in SDTM (DM) and in ADSL, for subject 01-701-1015library(dplyr)
pharmaversesdtm::dm |> filter(USUBJID == "01-701-1015") |> select(USUBJID, RFXSTDTC)#> # A tibble: 1 x 2#> USUBJID RFXSTDTC#> <chr> <chr>#> 1 01-701-1015 2014-01-02
pharmaverseadam::adsl |> filter(USUBJID == "01-701-1015") |> select(USUBJID, TRTSDT)#> # A tibble: 1 x 2#> USUBJID TRTSDT#> <chr> <date>#> 1 01-701-1015 2014-01-02In SDTM, DM.RFXSTDTC holds the date of the first study treatment, as ISO 8601 text (<chr>). In ADSL, TRTSDT holds it as a real date (<date>). For subject 01-701-1015 both are 2014-01-02.
Exercise 1-7
Section titled “Exercise 1-7”It breaks the “one step away” rule of ADaM. Change from baseline is a derived value, so it belongs in the ADaM data set (CHG in a BDS data set). The table program only selects rows with flags, counts, summarises and prints. Then the value is derived once, documented in the specification, and checked by double programming.
Exercise 1-8
Section titled “Exercise 1-8”“DM holds all 306 subjects who signed informed consent. 52 of them were screen failures and were never randomised, so the safety population has the 254 randomised subjects who took the study drug.”
Chapter 2
Section titled “Chapter 2”Exercise 2-1
Section titled “Exercise 2-1”# Answer 2-1: keep only the placebo subjects of DMlibrary(dplyr)
pharmaversesdtm::dm |> filter(ARM == "Placebo") |> nrow()#> [1] 8686 subjects are in the placebo arm.
Exercise 2-2
Section titled “Exercise 2-2”# Answer 2-2: diastolic blood pressure. Records with no value, and the rows that two filters loselibrary(dplyr)
diabp <- pharmaversesdtm::vs |> filter(VSTESTCD == "DIABP")
nrow(diabp)#> [1] 8207diabp |> filter(is.na(VSSTRESN)) |> nrow()#> [1] 2diabp |> filter(VSSTRESN >= 90) |> nrow()#> [1] 927diabp |> filter(VSSTRESN < 90) |> nrow()#> [1] 7278There are 8207 diastolic blood pressure records, and 2 have no value. 927 are 90 or higher and 7278 are below 90. 927 + 7278 = 8205: the two filters lose the 2 missing records, as in section 2.4.
Exercise 2-3
Section titled “Exercise 2-3”# Answer 2-3: an imputed end date may not be later than 2014-03-31. Collected dates are not cappedlibrary(dplyr)library(lubridate)
cap <- as.Date("2014-03-31")
tibble(DTC = c("2014-03-15", "2014-03", "2014", "2014-06-30")) |> mutate( DTF = case_when( nchar(DTC) == 7 ~ "D", nchar(DTC) == 4 ~ "M" ), EARLY = ymd(DTC, truncated = 2), AENDT = case_when( DTF == "M" ~ make_date(year(EARLY), 12, 31), DTF == "D" ~ rollforward(EARLY), .default = EARLY ), AENDT_CAP = if_else(!is.na(DTF) & AENDT > cap, cap, AENDT) ) |> select(-EARLY)#> # A tibble: 4 x 4#> DTC DTF AENDT AENDT_CAP#> <chr> <chr> <date> <date>#> 1 2014-03-15 <NA> 2014-03-15 2014-03-15#> 2 2014-03 D 2014-03-31 2014-03-31#> 3 2014 M 2014-12-31 2014-03-31#> 4 2014-06-30 <NA> 2014-06-30 2014-06-30The year-only date 2014 is imputed late to 2014-12-31. The cap makes it 2014-03-31. The date 2014-06-30 is also later than the cap, but it was collected: its flag is NA, so the cap does not change it. The condition !is.na(DTF) does that. The year-month date 2014-03 becomes 2014-03-31, which is not later than the cap.
Exercise 2-4
Section titled “Exercise 2-4”# Answer 2-4: count the adverse events by severity, in clinical orderlibrary(dplyr)
pharmaversesdtm::ae |> mutate(AESEV = factor(AESEV, levels = c("MILD", "MODERATE", "SEVERE"))) |> count(AESEV)#> # A tibble: 3 x 2#> AESEV n#> <fct> <int>#> 1 MILD 770#> 2 MODERATE 378#> 3 SEVERE 43770 mild, 378 moderate and 43 severe events. Without the factor, the order is the same here by chance: the three words are already in alphabetical order. The factor makes the order a rule, not a chance.
Exercise 2-5
Section titled “Exercise 2-5”# Answer 2-5: AE start dates. Strict conversion first, then early imputation with a flaglibrary(dplyr)library(lubridate)
ae <- pharmaversesdtm::ae |> select(USUBJID, AESTDTC) |> mutate( ASTDT_STRICT = as.Date(AESTDTC, format = "%Y-%m-%d"), ASTDTF = case_when( nchar(AESTDTC) == 7 ~ "D", nchar(AESTDTC) == 4 ~ "M" ), ASTDT = ymd(AESTDTC, truncated = 2) )
sum(is.na(ae$ASTDT_STRICT))#> [1] 26ae |> count(ASTDTF)#> # A tibble: 3 x 2#> ASTDTF n#> <chr> <int>#> 1 D 15#> 2 M 11#> 3 <NA> 1165sum(is.na(ae$ASTDT))#> [1] 026 start dates stay missing with the strict conversion. 15 get the flag D (year and month known) and 11 the flag M (only the year known). After the early imputation, no start date is missing. Compare with section 2.6: ymd() without truncated failed on 19 of these 26 dates and made wrong dates from the other 7.
Chapter 3
Section titled “Chapter 3”Exercise 3-1
Section titled “Exercise 3-1”# Answer 3-1: search the verbatim term AETERM for "pain"library(dplyr)library(stringr)
pharmaversesdtm::ae |> filter(str_detect(AETERM, regex("pain", ignore_case = TRUE))) |> nrow()#> [1] 3333 records have “pain” in AETERM, the same number as in AEDECOD. That is not a rule: the verbatim term and the coded term can differ.
Exercise 3-2
Section titled “Exercise 3-2”# Answer 3-2: a missing value in a sum and in a meana <- 1b <- NAc <- 3
(a + b + c) / 3#> [1] NAmean(c(a, b, c))#> [1] NAmean(c(a, b, c), na.rm = TRUE)#> [1] 2(a + b + c) / 3 is NA, and mean() is NA too. With na.rm = TRUE, mean() uses the two values that it has: (1 + 3) / 2 = 2. Note that it divides by 2, not by 3.
Exercise 3-3
Section titled “Exercise 3-3”# Answer 3-3: 1 subject of 8 is 12.5%. Shown with no decimalsround_half_up <- function(x, digits = 0) { scale <- 10^digits sign(x) * floor(abs(x) * scale + 0.5 + 1e-9) / scale}
pct <- 100 * 1 / 8pct#> [1] 12.5round(pct)#> [1] 12sprintf("%.0f", pct)#> [1] "12"round_half_up(pct)#> [1] 13round() and sprintf() give 12, because 12 is the even number. round_half_up() gives 13, as a clinical table expects.
Exercise 3-4
Section titled “Exercise 3-4”# Answer 3-4: the subjects with an AE start date that has only the yearlibrary(dplyr)library(stringr)
pharmaversesdtm::ae |> filter(str_detect(AESTDTC, "^\\d{4}$")) |> distinct(USUBJID) |> nrow()#> [1] 88 subjects have an AE start date with only the year. There are 11 such records, so some subjects have more than one. distinct(USUBJID) counts each subject once.
Exercise 3-5
Section titled “Exercise 3-5”# Answer 3-5: age at first treatment from the birth date, compared with AGE in DMlibrary(dplyr)library(lubridate)
age_check <- pharmaversesdtm::dm |> filter(ARMCD != "Scrnfail") |> mutate( AGE_CALC = floor(time_length(interval(ymd(BRTHDTC), ymd(RFXSTDTC)), "years")) ) |> select(USUBJID, BRTHDTC, RFXSTDTC, AGE, AGE_CALC)
age_check |> filter(USUBJID == "01-701-1015")#> # A tibble: 1 x 5#> USUBJID BRTHDTC RFXSTDTC AGE AGE_CALC#> <chr> <chr> <chr> <dbl> <dbl>#> 1 01-701-1015 1950-12-26 2014-01-02 63 63age_check |> count(SAME = AGE == AGE_CALC)#> # A tibble: 1 x 2#> SAME n#> <lgl> <int>#> 1 TRUE 254The computed age agrees with AGE for all 254 randomised subjects. Subject 01-701-1015 was born on 1950-12-26 and had the first dose on 2014-01-02: 63 years. count() with a condition (SAME = AGE == AGE_CALC) counts the rows where it is TRUE and where it is FALSE. Here there is no FALSE row.
Chapter 4
Section titled “Chapter 4”Exercise 4-1
Section titled “Exercise 4-1”# Answer 4-1: one row per subject and system organ class. How many rows does distinct() remove?library(dplyr)
ae <- pharmaversesdtm::aeae_soc <- ae |> distinct(USUBJID, AEBODSYS)
nrow(ae)#> [1] 1191nrow(ae_soc)#> [1] 582nrow(ae) - nrow(ae_soc)#> [1] 6091191 records become 582 rows: distinct() removes 609.
Exercise 4-2
Section titled “Exercise 4-2”# Answer 4-2: the number of subjects with an AE, and the subject with the most AE recordslibrary(dplyr)
ae_n <- pharmaversesdtm::ae |> count(USUBJID)
nrow(ae_n)#> [1] 225ae_n |> slice_max(n, n = 1)#> # A tibble: 1 x 2#> USUBJID n#> <chr> <int>#> 1 01-701-1302 23count(USUBJID) gives one row per subject, so 225 subjects have at least one adverse event. Subject 01-701-1302 has the most records: 23.
Exercise 4-3
Section titled “Exercise 4-3”# Answer 4-3: percent change from baseline weight. Divide only when the baseline is there and not 0library(dplyr)
pharmaversesdtm::vs |> filter(USUBJID == "01-701-1015", VSTESTCD == "WEIGHT") |> arrange(VISITNUM) |> mutate( BASE = VSSTRESN[VSBLFL %in% "Y"], BASEVIS = VISITNUM[VSBLFL %in% "Y"], CHG = if_else(VISITNUM > BASEVIS, VSSTRESN - BASE, NA), PCHG = if_else(!is.na(BASE) & BASE != 0, round(100 * CHG / BASE, 2), NA), .by = USUBJID ) |> select(VISIT, VSSTRESN, BASE, CHG, PCHG) |> print(n = 4)#> # A tibble: 11 x 5#> VISIT VSSTRESN BASE CHG PCHG#> <chr> <dbl> <dbl> <dbl> <dbl>#> 1 SCREENING 1 54.0 54.4 NA NA#> 2 BASELINE 54.4 54.4 NA NA#> 3 WEEK 2 53.1 54.4 -1.36 -2.5#> 4 WEEK 4 54.0 54.4 -0.450 -0.83#> # i 7 more rowsBefore you divide, check that the baseline exists (not NA) and is not 0. A division by 0 gives Inf or NaN, not an error. At WEEK 2 the change is -1.36 kg, which is -2.5% of the baseline 54.43.
Exercise 4-4
Section titled “Exercise 4-4”# Answer 4-4: drop the groups after summarise(), so the percent is of all 254 subjectslibrary(dplyr)
pct <- pharmaverseadam::adsl |> filter(SAFFL == "Y") |> group_by(ARM, SEX) |> summarise(n = n(), .groups = "drop") |> mutate(PCT = round(100 * n / sum(n), 1))pct#> # A tibble: 6 x 4#> ARM SEX n PCT#> <chr> <chr> <int> <dbl>#> 1 Placebo F 53 20.9#> 2 Placebo M 33 13#> 3 Xanomeline High Dose F 40 15.7#> 4 Xanomeline High Dose M 44 17.3#> 5 Xanomeline Low Dose F 50 19.7#> 6 Xanomeline Low Dose M 34 13.4
sum(pct$n)#> [1] 254sum(pct$PCT)#> [1] 100.groups = "drop" removes all groups after summarise(). Now sum(n) is 254, and the percents add up to 100. ungroup() after summarise(), or .by = c(ARM, SEX), gives the same result.
Exercise 4-5
Section titled “Exercise 4-5”# Answer 4-5: the worst severity of each subject, then the subjects per worst severitylibrary(dplyr)
pharmaversesdtm::ae |> mutate(SEVN = match(AESEV, c("MILD", "MODERATE", "SEVERE"))) |> summarise(WORST = max(SEVN), .by = USUBJID) |> count(WORST)#> # A tibble: 3 x 2#> WORST n#> <int> <int>#> 1 1 77#> 2 2 117#> 3 3 31summarise(WORST = max(SEVN), .by = USUBJID) gives one row per subject with the worst severity. 77 subjects had at worst a mild event, 117 a moderate one and 31 a severe one: 225 subjects, as in exercise 4-2. A table would show the words MILD, MODERATE and SEVERE again, and the treatment arm in the columns.
Found an error or have a question? Write to datastep.studio@gmail.com· Every program runs in DataStep Studio for R, the R extension for VS Code and Positron.
Data: the CDISC pilot study (cdiscpilot01), from the R packagespharmaversesdtm andpharmaverseadam (Apache License 2.0).
R is a project of the R Foundation. SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. This book is not affiliated with or endorsed by SAS Institute Inc. or the R Foundation. CDISC, SDTM and ADaM are trademarks of the Clinical Data Interchange Standards Consortium.
© 2026 DataStep Labs. All rights reserved.