Day 17 of 30·Clinical Data Handling & CDISC Basics
Dates & Times: from SAS Date Values to lubridate
ISO 8601, partial dates and date arithmetic for clinical derivations.
Learning objectives
- 1Convert character dates to Date with lubridate.
- 2Handle ISO 8601 and partial dates common in SDTM.
- 3Compute study day and durations correctly.
- 4Contrast SAS date offsets with R's date class.
Key SAS® → R comparison
| Task | SAS® | R |
|---|---|---|
| Parse date | input(x, yymmdd10.) | ymd(x) (lubridate) |
| Today | today() | Sys.Date() |
| Date diff | intck/intnx | difftime() / %--% |
| Datetime | datetime values | ymd_hms(x) |
Code examples
SAS®
adt = input(astdtc, yymmdd10.);
ady = adt - trtsdt + (adt >= trtsdt);R
library(lubridate)
adlb <- adlb |>
mutate(
adt = ymd(astdtc),
ady = as.integer(adt - trtsdt) +
if_else(adt >= trtsdt, 1L, 0L)
)Note: Study day has no day zero; the +1 on/after treatment start reproduces the standard CDISC rule.
Explanation
R's Date class counts days from 1970-01-01, where SAS counts from 1960-01-01, but you rarely deal with the offset directly — lubridate parses strings into proper dates and handles arithmetic for you. The ymd/ymd_hms family reads the ISO 8601 strings that dominate SDTM.
Clinical specifics — study day with no day zero, partial dates, and imputation rules — are handled explicitly in R, which makes the derivation rules visible and auditable rather than buried in informats.
Practice exercise
Parse SDTM --DTC character dates with lubridate, derive ADY relative to a treatment start date applying the no-day-zero rule, and compute exposure duration in days between two dates.