Skip to content

Answers, Part VI (chapters 20 to 26)

Each answer has a program that was run, or the output of the dsr tool that was run on the pilot data. Try the exercise yourself first. In the editor you get the same numbers with the keys of the chapter.

Run to Line with the cursor on line 10, or:

$ dsr run programs/20_1_weight.R --to-line 10
4 NOTE: adsl has 254 rows and 4 columns.
8 NOTE: weight has 253 rows and 2 columns.
Workspace: new: adsl (tbl_df 254 x 4), weight (tbl_df 253 x 2)
RESULT: CLEAN (0.12 s)

Two data frames: adsl with 254 rows and weight with 253 rows. The join of line 12 has not run yet.

programs/20_x2_bili_text.R
# Answer 20-2: the values of LBORRES that are not numbers
library(dplyr)
pharmaversesdtm::lb |>
filter(LBTESTCD == "BILI") |>
filter(is.na(suppressWarnings(as.numeric(LBORRES)))) |>
count(LBORRES)
#> # A tibble: 1 x 2
#> LBORRES n
#> <chr> <int>
#> 1 <0.2 5

Five results are <0.2: the value is below the limit of the test. as.numeric() cannot read the <, so it gives NA. suppressWarnings() is right here, because the program looks for exactly these values.

programs/20_x3_bili_base.R
# Answer 20-3: one baseline bilirubin per subject, so the join copies nothing
library(dplyr)
adsl <- pharmaverseadam::adsl |>
filter(SAFFL == "Y") |>
select(USUBJID, TRT01A)
bili <- pharmaversesdtm::lb |>
filter(LBTESTCD == "BILI", LBBLFL == "Y") |>
mutate(BILI = as.numeric(LBORRES)) |>
select(USUBJID, BILI)
adsl_bili <- adsl |>
left_join(bili, by = "USUBJID")
nrow(adsl_bili)
#> [1] 254
$ dsr run programs/20_x3_bili_base.R
4 NOTE: adsl has 254 rows and 2 columns.
8 NOTE: bili has 252 rows and 2 columns.
13 NOTE: adsl_bili has 254 rows and 3 columns.
[1] 254
Workspace: new: adsl (tbl_df 254 x 2), adsl_bili (tbl_df 254 x 3), bili (tbl_df 252 x 2)
RESULT: CLEAN (0.02 s)

254 rows, one per subject, and the verdict is CLEAN. The coercion warning is gone too: the five <0.2 values are not baseline values. bili has 252 rows, so two subjects have no baseline bilirubin.

$ dsr query adsl --where 'is.na(WEIGHTBL)'
adsl: 1 of 254 rows where is.na(WEIGHTBL), rows 1-1
USUBJID TRT01A AGE SEX WEIGHTBL WGTGR
01-702-1082 Xanomeline Low Dose 84 F NA NA

Subject 01-702-1082, a woman of 84 on the low dose. This is the <NA> row of the low dose arm in the table of 20_1.

The filter >80 under AGE, with SAFFL == "Y" in the WHERE box. In a terminal:

$ dsr query adsl --where 'SAFFL == "Y" & AGE > 80' --count
adsl: 77 of 306 rows where SAFFL == "Y" & AGE > 80
COUNT: 77

77 subjects. It is the group >80 of chapter 25.

$ dsr values adae ASTDTF
adae: ASTDTF (Analysis Start Date Imputation Flag), character: 3 values, 1191 rows
value r n pct
NA NA 1165 97.8
D "D" 15 1.3
M "M" 11 0.9
(r: the value as R writes it in a condition; NA is picked with is.na())

26 records have an imputed start date: 15 with the day imputed (D) and 11 with the month and day imputed (M). Chapter 23 finds the same 26 partial dates in SDTM AE, where a program without an imputation loses them.

$ dsr values adae TRT01A --keep 'Placebo,Xanomeline Low Dose'
TRT01A %in% c("Placebo", "Xanomeline Low Dose")
PICK: 755 of 1191 rows | dsr query adae --where 'TRT01A %in% c("Placebo", "Xanomeline Low Dose")'

TRT01A %in% c("Placebo", "Xanomeline Low Dose"), 755 of 1191 rows.

$ dsr query adae --where 'TRTEMFL == "Y" & AESEV == "SEVERE" & TRT01A == "Placebo"' --vars 'USUBJID AESEQ AEDECOD ASTDY' --sort 'USUBJID AESEQ'
adae: 6 of 1191 rows where TRTEMFL == "Y" & AESEV == "SEVERE" & TRT01A == "Placebo", rows 1-6
USUBJID AESEQ AEDECOD ASTDY
01-704-1445 1 COMPLETED SUICIDE 174
01-709-1259 6 BENIGN PROSTATIC HYPERPLASIA 51
01-710-1083 1 MYOCARDIAL INFARCTION 12
01-710-1271 2 MYOCARDIAL INFARCTION 56
01-710-1368 1 HIP FRACTURE 64
01-710-1368 4 HIP FRACTURE 64

Subject 01-710-1368 has two records of HIP FRACTURE on day 64. So 6 records are 5 subjects: the placebo count of subjects is 5, not 6.

$ dsr format programs/22_1_messy.R --check
would change: programs/22_1_messy.R (proof: only the layout changed: the 52 tokens and 1 comments are the same)

52 tokens and 1 comment. The file would change only in its layout.

programs/22_x2_typo_fixed.R
# Answer 22-2: the two typos of 22_2_typo.R, fixed
library(dplyr)
adsl_saf <- pharmaverseadam::adsl |>
filter(SAFFL == "Y")
adsl_saf |>
group_by(TRT01A) |>
summarise(subjects = n_distinct(USUBJID))
#> # A tibble: 3 x 2
#> TRT01A subjects
#> <chr> <int>
#> 1 Placebo 86
#> 2 Xanomeline High Dose 72
#> 3 Xanomeline Low Dose 96
nrow(adsl_saf)
#> [1] 254
$ dsr lint programs/22_x2_typo_fixed.R
LINT: CLEAN (1 files, with lintr)

86 subjects on placebo, 72 on the high dose and 96 on the low dose, by actual treatment (TRT01A): 254 in all.

$ dsr rename pct_subjects pct_of_n --check
...
RENAME: WOULD CHANGE | pct_subjects -> pct_of_n (function, the project): 3 tokens in 1 file (proof: 3 tokens changed (pct_subjects to pct_of_n) in 1 file; the other 82 tokens, every comment and every string are the same; R's parser confirms it)

3 tokens change (the definition and the two calls); the other 82 tokens stay the same.

$ dsr call optimize programs/20_1_weight.R:14:20 --check
CALL: UNCHANGED (programs/20_1_weight.R:14: if_else() of the package dplyr: 0 defaults removed, the call on one line; the call is already so; line limit 80)

Nothing changes. The call gives no argument with its default value, and it already fits on one line.

45 records because ASTDY >= 1 is FALSE, and 26 because it is NA (the output of 23.2). The 45 events started before the first dose: dropping them is right. The 26 have a partial start date. Some of them started after the first dose, for example the two events of 01-701-1239 in March and April 2014: losing them is the problem.

$ dsr pipe programs/23_1_lost.R:9 --follow 01-701-1015
...
Follow USUBJID == "01-701-1015":
0 ae 3 rows: 01-701-1015 / 1, 01-701-1015 / 2, 01-701-1015 / 3
1 inner_join(adsl, by = "USUBJID") 3 -> 3
+TRT01A = "Placebo"
+TRTSDT = "2014-01-02"
2 mutate(ASTDT = as.Date(AESTDTC), ASTDY = as. 3 -> 3
+ASTDT = "2014-01-03" (01-701-1015 / 1), "2014-01-03" (01-701-1015 / 2), "2014-01-09" (01-701-1015 / 3)
+ASTDY = 2 (01-701-1015 / 1), 2 (01-701-1015 / 2), 8 (01-701-1015 / 3)
3 filter(ASTDY >= 1) 3 -> 3
4 select(USUBJID, AESEQ, AEDECOD, AESTDTC, AST 3 -> 3
RESULT: CLEAN | FOLLOW: 3 rows of USUBJID == "01-701-1015" at the start, 3 at the end (data after step k: @pipe[k]; rows step k dropped: @pipe[k]:dropped)

None leaves. The three events are on study days 2, 2 and 8, as in ADAE (chapter 21).

programs/23_x3_disposition.R
# Answer 23-3: keep only the disposition event, so each subject stays one row
library(dplyr)
adsl <- pharmaverseadam::adsl |>
filter(SAFFL == "Y") |>
select(USUBJID, TRT01A)
adsl_ds <- adsl |>
left_join(
pharmaversesdtm::ds |>
filter(DSCAT == "DISPOSITION EVENT"),
by = "USUBJID"
) |>
select(USUBJID, TRT01A, DSDECOD)
adsl_ds |>
count(TRT01A)
#> # A tibble: 3 x 2
#> TRT01A n
#> <chr> <int>
#> 1 Placebo 86
#> 2 Xanomeline High Dose 72
#> 3 Xanomeline Low Dose 96
$ dsr run programs/23_x3_disposition.R
4 NOTE: adsl has 254 rows and 2 columns.
8 NOTE: adsl_ds has 254 rows and 3 columns.
...
RESULT: CLEAN (0.01 s)

86, 72 and 96 subjects: each subject has one disposition event, so the join copies nothing, and the verdict is CLEAN.

$ dsr trace programs/23_3_study_day.R --call 'study_day(as.Date("2013-12-26"), as.Date("2014-01-02"))'
programs/23_3_study_day.R:3 days <- as.numeric(date - ref)
days = -7
programs/23_3_study_day.R:4 if (days > 0) {
-> days > 0 [FALSE] days = -7
programs/23_3_study_day.R:7 days
[1] -7
Value: -7
RESULT: CLEAN (0.04 s)

-7 is right: a day before the first dose gets no +1, because there is no day 0. The fix of line 4:

programs/23_x4_study_day.R
# Answer 23-4: the study day rule of CDISC: day 1 is the day of the first dose, there is no day 0
study_day <- function(date, ref) {
days <- as.numeric(date - ref)
if (days >= 0) {
days + 1
} else {
days
}
}
study_day(as.Date("2014-01-02"), as.Date("2014-01-02"))
#> [1] 1
study_day(as.Date("2013-12-26"), as.Date("2014-01-02"))
#> [1] -7

The day of the first dose is day 1 now, and the day 7 days before it stays -7.

At the stop in round 2, nsub has only the count of round 1:

$ dsr pry eval 'nsub'
MILD
186
RESULT: CLEAN (0 s)

Two next later, the run is at line 10 of round 3, and the count of round 2 is there:

$ dsr pry eval 'nsub'
MILD MODERATE
186 129
RESULT: CLEAN (0 s)

End the stop with dsr pry quit (or continue), and put the comment back on line 13.

. has 507 rows (stopped at pry(): . is 507 rows x 25 columns). These are the weights on or before the first dose. Most subjects have two: SCREENING 1 and BASELINE. dsr pry pstep 6 shows the step slice_tail(): 507 -> 254 rows, so it dropped 253 rows, one per subject with two weights.

$ dsr pry eval 'sum(data$PARAMCD == "PULSE")'
[1] 11505
RESULT: CLEAN (0 s)

ADVS has 11505 records of PULSE. With the right code, the program runs to its end:

programs/24_x3_pulse.R
# Answer 24-3: the parameter code of the heart rate in ADVS is PULSE, not HR
summarise_param <- function(data, paramcd) {
rows <- data[data$PARAMCD == paramcd, ]
if (nrow(rows) == 0) stop("no records for ", paramcd)
c(n = nrow(rows), mean = round(mean(rows$AVAL, na.rm = TRUE), 1))
}
advs <- pharmaverseadam::advs
summarise_param(advs, "PULSE")
#> n mean
#> 11505.0 73.1

VSDTC is ISO 8601 text (2014-01-02). Complete ISO dates sort as text in the same order as dates, so the last date on or before the first dose is the right record here. It would not be right with two records on the same date, with a partial date, or with a date and time in some records but not in others. VISITNUM is the order that the protocol planned, and it is the usual key for this sort.

81 (sum(is.na(grp)) changed by 25_1_age_group.R:5: 221 -> 81). These are the ages above 80, which line 6 handles next, and the four ages of exactly 65. After line 6, 4 are left.

$ dsr query adsl --where 'is.na(AGEGR)'
adsl: 4 of 254 rows where is.na(AGEGR), rows 1-4
USUBJID TRT01A AGE AGEGR
01-707-1206 Placebo 65 NA
01-709-1309 Xanomeline High Dose 65 NA
01-715-1107 Xanomeline Low Dose 65 NA
01-715-1319 Xanomeline High Dose 65 NA

Four subjects, all aged 65: one on placebo, two on the high dose, one on the low dose.

Line 10, step 2 of the pipeline, mutate(BILI = as.numeric(LBORRES)) (FIRST CAUSE: programs/20_2_bili.R:10: step 2). The values that became NA are the five <0.2 of exercise 20-2.

With age >= 65 on line 5:

$ dsr run programs/25_2_adsl_agegr.R
...
# A tibble: 3 × 2
AGEGR n
<chr> <int>
1 65-80 144
2 <65 33
3 >80 77
...
RESULT: CLEAN (0.01 s)

144 subjects are in 65-80: the 140 of before and the 4 aged 65. No subject is without a group.

$ dsr query cm --where 'nchar(CMSTDTC) == 4' --count
cm: 3731 of 7510 rows where nchar(CMSTDTC) == 4
COUNT: 3731
$ dsr filter cm CMSTDTC '????'
grepl("^....$", toupper(CMSTDTC))
FILTER: 3731 of 7510 rows | dsr query cm --where 'grepl("^....$", toupper(CMSTDTC))'

3731 records, half of CM, have only a year as start date. The column filter ???? (four characters) finds the same rows.

10 records and 8 subjects: the High Dose line of program 21_1.

Add this test at the end of 26_tests/test-age_group.R (the tested program below runs the same test on its own):

programs/26_x3_test_input.R
# Answer 26-3: the missing test, a text age must stop with a clear error
source("26_3_age_group.R")
testthat::test_that("a text age is an error", {
testthat::expect_error(age_group("70"), "must be a number")
})
#> Test passed with 1 success.
$ dsr test programs/26_tests --coverage
COVERAGE: programs/26_3_age_group.R: 7 of 7 lines ran
TESTS PASS (3 tests, 3 expectations in 1 files, 0 failed, 1.7 s)

Every line of age_group() ran now: the tests reach the check of the input too.

$ dsr verify programs/23_2_copies.R
VERIFY CHECK | 0 errors, 1 warning | FIRST CAUSE: programs/23_2_copies.R:8: [join] adsl_ds: rows went from 254 to 798 in a join: the other side has duplicate keys (check with dsr dups)
1. programs/23_2_copies.R:8: [join] adsl_ds: rows went from 254 to 798 in a join: the other side has duplicate keys (check with dsr dups) | why: The other side has duplicate keys: records were copied. | fix: dsr dups <other data> --key <by columns>; then remove the duplicates or say relationship = in the join.
STEPS
lint ok, 1 file (with lintr)
inputs no input made by another program
run CHECK | programs/23_2_copies.R:8: adsl_ds: rows went from 254 to 798 in a join: the other side has duplicate (0.01 s)
log 1 suspicious message(s)
fresh run CHECK | programs/23_2_copies.R:8: adsl_ds: rows went from 254 to 798 in a join: the other side has duplicate (log: programs/23_2_copies.log)
...

VERIFY CHECK: the program runs, also in a fresh session, but the join copies records. The fix tells you to find the duplicate keys of DS and to remove them, or to say in the join that many records per subject are what you want. Here they are not: exercise 23-3 keeps one record per subject. dsr verify writes a .log next to the program; delete it if you do not need it.

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.