Skip to content

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.

programs/01_x1_sex.R
# Answer 1-1: count the women and men among the randomised subjects
library(dplyr)
pharmaverseadam::adsl |>
filter(SAFFL == "Y") |>
count(SEX)
#> # A tibble: 2 x 2
#> SEX n
#> <chr> <int>
#> 1 F 143
#> 2 M 111

The safety population has 143 women and 111 men: 254 subjects.

programs/01_x2_over80.R
# 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 77

77 subjects are older than 80. count() with no column counts all rows. Both conditions are in one filter(), so a row must meet both.

programs/01_x3_youngest.R
# Answer 1-3: without desc(), the first subject of each arm is the youngest
library(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 51

Without 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.

programs/01_x4_severe.R
# Answer 1-4: subjects with a severe treatment-emergent adverse event, per arm
library(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 16

5 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.

programs/01_x5_low_dose.R
# Answer 1-5: the age summary of the low dose arm
library(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 88

The 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.

programs/01_x6_trtsdt.R
# Answer 1-6: the date of first treatment in SDTM (DM) and in ADSL, for subject 01-701-1015
library(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-02

In 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.

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.

“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.”

programs/02_x1_placebo.R
# Answer 2-1: keep only the placebo subjects of DM
library(dplyr)
pharmaversesdtm::dm |>
filter(ARM == "Placebo") |>
nrow()
#> [1] 86

86 subjects are in the placebo arm.

programs/02_x2_diabp.R
# Answer 2-2: diastolic blood pressure. Records with no value, and the rows that two filters lose
library(dplyr)
diabp <- pharmaversesdtm::vs |>
filter(VSTESTCD == "DIABP")
nrow(diabp)
#> [1] 8207
diabp |> filter(is.na(VSSTRESN)) |> nrow()
#> [1] 2
diabp |> filter(VSSTRESN >= 90) |> nrow()
#> [1] 927
diabp |> filter(VSSTRESN < 90) |> nrow()
#> [1] 7278

There 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.

programs/02_x3_cap.R
# Answer 2-3: an imputed end date may not be later than 2014-03-31. Collected dates are not capped
library(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-30

The 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.

programs/02_x4_severity.R
# Answer 2-4: count the adverse events by severity, in clinical order
library(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 43

770 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.

programs/02_x5_ae_dates.R
# Answer 2-5: AE start dates. Strict conversion first, then early imputation with a flag
library(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] 26
ae |> count(ASTDTF)
#> # A tibble: 3 x 2
#> ASTDTF n
#> <chr> <int>
#> 1 D 15
#> 2 M 11
#> 3 <NA> 1165
sum(is.na(ae$ASTDT))
#> [1] 0

26 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.

programs/03_x1_aeterm.R
# 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] 33

33 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.

programs/03_x2_mean_na.R
# Answer 3-2: a missing value in a sum and in a mean
a <- 1
b <- NA
c <- 3
(a + b + c) / 3
#> [1] NA
mean(c(a, b, c))
#> [1] NA
mean(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.

programs/03_x3_percent.R
# Answer 3-3: 1 subject of 8 is 12.5%. Shown with no decimals
round_half_up <- function(x, digits = 0) {
scale <- 10^digits
sign(x) * floor(abs(x) * scale + 0.5 + 1e-9) / scale
}
pct <- 100 * 1 / 8
pct
#> [1] 12.5
round(pct)
#> [1] 12
sprintf("%.0f", pct)
#> [1] "12"
round_half_up(pct)
#> [1] 13

round() and sprintf() give 12, because 12 is the even number. round_half_up() gives 13, as a clinical table expects.

programs/03_x4_year_only.R
# Answer 3-4: the subjects with an AE start date that has only the year
library(dplyr)
library(stringr)
pharmaversesdtm::ae |>
filter(str_detect(AESTDTC, "^\\d{4}$")) |>
distinct(USUBJID) |>
nrow()
#> [1] 8

8 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.

programs/03_x5_age.R
# Answer 3-5: age at first treatment from the birth date, compared with AGE in DM
library(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 63
age_check |> count(SAME = AGE == AGE_CALC)
#> # A tibble: 1 x 2
#> SAME n
#> <lgl> <int>
#> 1 TRUE 254

The 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.

programs/04_x1_bodsys.R
# Answer 4-1: one row per subject and system organ class. How many rows does distinct() remove?
library(dplyr)
ae <- pharmaversesdtm::ae
ae_soc <- ae |> distinct(USUBJID, AEBODSYS)
nrow(ae)
#> [1] 1191
nrow(ae_soc)
#> [1] 582
nrow(ae) - nrow(ae_soc)
#> [1] 609

1191 records become 582 rows: distinct() removes 609.

programs/04_x2_most_ae.R
# Answer 4-2: the number of subjects with an AE, and the subject with the most AE records
library(dplyr)
ae_n <- pharmaversesdtm::ae |>
count(USUBJID)
nrow(ae_n)
#> [1] 225
ae_n |> slice_max(n, n = 1)
#> # A tibble: 1 x 2
#> USUBJID n
#> <chr> <int>
#> 1 01-701-1302 23

count(USUBJID) gives one row per subject, so 225 subjects have at least one adverse event. Subject 01-701-1302 has the most records: 23.

programs/04_x3_pchg.R
# Answer 4-3: percent change from baseline weight. Divide only when the baseline is there and not 0
library(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 rows

Before 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.

programs/04_x4_ungroup.R
# Answer 4-4: drop the groups after summarise(), so the percent is of all 254 subjects
library(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] 254
sum(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.

programs/04_x5_worst.R
# Answer 4-5: the worst severity of each subject, then the subjects per worst severity
library(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 31

summarise(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.