Introduction
This article describes creating an ADSL
ADaM. Examples
are currently presented and tested using DM
,
EX
, AE
, LB
and DS
SDTM domains. However, other domains could be used.
Note: All examples assume CDISC SDTM and/or ADaM format as input unless otherwise specified.
Programming Flow
- Read in Data
- Derive Period, Subperiod, and Phase Variables
(e.g.
APxxSDT
,APxxEDT
, …) - Derive Treatment Variables
(
TRT0xP
,TRT0xA
) - Derive/Impute Numeric Treatment Date/Time and
Duration (
TRTSDT
,TRTEDT
,TRTDURD
) - Derive Disposition Variables
- Derive Death Variables
- Derive Last Known Date Alive
(
LSTALVDT
) - Derive Groupings and Populations
- Derive Other Variables
- Add Labels and Attributes
Read in Data
To start, all data frames needed for the creation of
ADSL
should be read into the environment. This will be a
company specific process. Some of the data frames needed may be
DM
, EX
, DS
, AE
, and
LB
.
For example purpose, the CDISC Pilot SDTM datasets—which are included
in {admiral.test}
—are used.
library(admiral)
library(dplyr, warn.conflicts = FALSE)
library(admiral.test)
library(lubridate)
library(stringr)
data("admiral_dm")
data("admiral_ds")
data("admiral_ex")
data("admiral_ae")
data("admiral_lb")
dm <- convert_blanks_to_na(admiral_dm)
ds <- convert_blanks_to_na(admiral_ds)
ex <- convert_blanks_to_na(admiral_ex)
ae <- convert_blanks_to_na(admiral_ae)
lb <- convert_blanks_to_na(admiral_lb)
The DM
domain is used as the basis for
ADSL
:
Derive Period, Subperiod, and Phase Variables
(e.g. APxxSDT
, APxxEDT
, …)
See the “Visit and Period Variables” vignette for more information.
If the variables are not derived based on a period reference dataset, they may be derived at a later point of the flow. For example, phases like “Treatment Phase” and “Follow up” could be derived based on treatment start and end date.
Derive Treatment Variables (TRT0xP
,
TRT0xA
)
The mapping of the treatment variables is left to the ADaM programmer. An example mapping for a study without periods may be:
For studies with periods see the “Visit and Period Variables” vignette.
Derive/Impute Numeric Treatment Date/Time and Duration
(TRTSDTM
, TRTEDTM
, TRTDURD
)
The function derive_vars_merged()
can be used to derive
the treatment start and end date/times using the ex
domain.
A pre-processing step for ex
is required to convert the
variable EXSTDTC
and EXSTDTC
to datetime
variables and impute missing date or time components. Conversion and
imputation is done by derive_vars_dtm()
.
Example calls:
# impute start and end time of exposure to first and last respectively, do not impute date
ex_ext <- ex %>%
derive_vars_dtm(
dtc = EXSTDTC,
new_vars_prefix = "EXST"
) %>%
derive_vars_dtm(
dtc = EXENDTC,
new_vars_prefix = "EXEN",
time_imputation = "last"
)
adsl <- adsl %>%
derive_vars_merged(
dataset_add = ex_ext,
filter_add = (EXDOSE > 0 |
(EXDOSE == 0 &
str_detect(EXTRT, "PLACEBO"))) & !is.na(EXSTDTM),
new_vars = exprs(TRTSDTM = EXSTDTM, TRTSTMF = EXSTTMF),
order = exprs(EXSTDTM, EXSEQ),
mode = "first",
by_vars = exprs(STUDYID, USUBJID)
) %>%
derive_vars_merged(
dataset_add = ex_ext,
filter_add = (EXDOSE > 0 |
(EXDOSE == 0 &
str_detect(EXTRT, "PLACEBO"))) & !is.na(EXENDTM),
new_vars = exprs(TRTEDTM = EXENDTM, TRTETMF = EXENTMF),
order = exprs(EXENDTM, EXSEQ),
mode = "last",
by_vars = exprs(STUDYID, USUBJID)
)
This call returns the original data frame with the column
TRTSDTM
, TRTSTMF
, TRTEDTM
, and
TRTETMF
added. Exposure observations with incomplete date
and zero doses of non placebo treatments are ignored. Missing time parts
are imputed as first or last for start and end date respectively.
The datetime variables returned can be converted to dates using the
derive_vars_dtm_to_dt()
function.
adsl <- adsl %>%
derive_vars_dtm_to_dt(source_vars = exprs(TRTSDTM, TRTEDTM))
Now, that TRTSDT
and TRTEDT
are derived,
the function derive_var_trtdurd()
can be used to calculate
the Treatment duration (TRTDURD
).
adsl <- adsl %>%
derive_var_trtdurd()
Derive Disposition Variables
Disposition Dates (e.g. EOSDT
)
The functions derive_vars_dt()
and
derive_vars_merged()
can be used to derive a disposition
date. First the character disposition date (DS.DSSTDTC
) is
converted to a numeric date (DSSTDT
) calling
derive_vars_dt()
. Then the relevant disposition date is
selected by adjusting the filter_add
parameter.
To derive the End of Study date (EOSDT
), a call could
be:
# convert character date to numeric date without imputation
ds_ext <- derive_vars_dt(
ds,
dtc = DSSTDTC,
new_vars_prefix = "DSST"
)
adsl <- adsl %>%
derive_vars_merged(
dataset_add = ds_ext,
by_vars = exprs(STUDYID, USUBJID),
new_vars = exprs(EOSDT = DSSTDT),
filter_add = DSCAT == "DISPOSITION EVENT" & DSDECOD != "SCREEN FAILURE"
)
We would get :
This call would return the input dataset with the column
EOSDT
added. This function allows the user to impute
partial dates as well. If imputation is needed and the date is to be
imputed to the first of the month, then set
date_imputation = "FIRST"
.
Disposition Status (e.g. EOSSTT
)
The function derive_var_merged_cat()
can be used to
derive a disposition status at a specific timepoint. The relevant
disposition variable (DS.DSDECOD
) is selected by adjusting
the filter parameter and used to derive EOSSTT
.
To derive the End of Study status (EOSSTT
), the function
expects a mapping derivation for the cat_fun
argument. The
mapping derivation for the call below is
-
"NOT STARTED"
ifDSDECOD
is"SCREEN FAILURE"
-
"COMPLETED"
ifDSDECOD == "COMPLETED"
-
"DISCONTINUED"
ifDSDECOD
is not"COMPLETED"
orNA
-
"ONGOING"
otherwise
Example function format_eosstt()
:
format_eosstt <- function(x) {
case_when(
x %in% c("COMPLETED") ~ "COMPLETED",
x %in% c("SCREEN FAILURE") ~ NA_character_,
!is.na(x) ~ "DISCONTINUED",
TRUE ~ "ONGOING"
)
}
The customized mapping function format_eosstt()
can now
be passed to the main function:
adsl <- adsl %>%
derive_var_merged_cat(
dataset_add = ds,
by_vars = exprs(STUDYID, USUBJID),
filter_add = DSCAT == "DISPOSITION EVENT",
new_var = EOSSTT,
source_var = DSDECOD,
cat_fun = format_eosstt,
missing_value = "ONGOING"
)
This call would return the input dataset with the column
EOSSTT
added.
If the derivation must be changed, the user can create his/her own
function and pass it to the cat_fun
argument of the
function (cat_fun = new_mapping
) to map
DSDECOD
to a suitable EOSSTT
value.
Disposition Reason(s) (e.g. DCSREAS
,
DCSREASP
)
The main reason for discontinuation is usually stored in
DSDECOD
while DSTERM
provides additional
details regarding subject’s discontinuation (e.g., description of
"OTHER"
).
The function derive_vars_merged()
can be used to derive
a disposition reason (along with the details, if required) at a specific
timepoint. The relevant disposition variable(s)
(DS.DSDECOD
, DS.DSTERM
) are selected by
adjusting the filter parameter and used to derive the main reason (and
details).
To derive the End of Study reason(s) (DCSREAS
and
DCSREASP
), the function will map
-
DCSREAS
asDSDECOD
, andDCSREASP
asDSTERM
ifDSDECOD
is not"COMPLETED"
,"SCREEN FAILURE"
, orNA
,NA
otherwise
adsl <- adsl %>%
derive_vars_merged(
dataset_add = ds,
by_vars = exprs(USUBJID),
new_vars = exprs(DCSREAS = DSDECOD, DCSREASP = DSTERM),
filter_add = DSCAT == "DISPOSITION EVENT" & DSDECOD %notin% c("SCREEN FAILURE", "COMPLETED", NA)
)
This call would return the input dataset with the column
DCSREAS
and DCSREASP
added.
If the derivation must be changed, the user can define that
derivation in the filter_add
argument of the function to
map DSDECOD
and DSTERM
to a suitable
DCSREAS
/DCSREASP
value.
The call below maps DCSREAS
and DCREASP
as
follows:
-
DCSREAS
asDSDECOD
ifDSDECOD
is not"COMPLETED"
orNA
,NA
otherwise -
DCSREASP
asDSTERM
ifDSDECOD
is equal toOTHER
,NA
otherwise
adsl <- adsl %>%
derive_vars_merged(
dataset_add = ds,
by_vars = exprs(USUBJID),
new_vars = exprs(DCSREAS = DSDECOD),
filter_add = DSCAT == "DISPOSITION EVENT" & DSDECOD %notin% c("SCREEN FAILURE", "COMPLETED", NA)
) %>%
derive_vars_merged(
dataset_add = ds,
by_vars = exprs(USUBJID),
new_vars = exprs(DCSREASP = DSTERM),
filter_add = DSCAT == "DISPOSITION EVENT" & DSDECOD %in% "OTHER"
)
Randomization Date (RANDDT
)
The function derive_vars_merged()
can be used to derive
randomization date variable. To map Randomization Date
(RANDDT
), the call would be:
adsl <- adsl %>%
derive_vars_merged(
dataset_add = ds_ext,
filter_add = DSDECOD == "RANDOMIZED",
by_vars = exprs(STUDYID, USUBJID),
new_vars = exprs(RANDDT = DSSTDT)
)
This call would return the input dataset with the column
RANDDT
is added.
Derive Death Variables
Death Date (DTHDT
)
The function derive_vars_dt()
can be used to derive
DTHDT
. This function allows the user to impute the date as
well.
Example calls:
adsl <- adsl %>%
derive_vars_dt(
new_vars_prefix = "DTH",
dtc = DTHDTC
)
This call would return the input dataset with the columns
DTHDT
added and, by default, the associated date imputation
flag (DTHDTF
) populated with the controlled terminology
outlined in the ADaM IG for date imputations. If the imputation flag is
not required, the user must set the argument
flag_imputation
to “none”.
If imputation is needed and the date is to be imputed to the first day of the month/year the call would be:
adsl <- adsl %>%
derive_vars_dt(
new_vars_prefix = "DTH",
dtc = DTHDTC,
date_imputation = "first"
)
See also Date and Time Imputation.
Cause of Death (DTHCAUS
)
The cause of death DTHCAUS
can be derived using the
function derive_var_dthcaus()
.
Since the cause of death could be collected/mapped in different
domains (e.g. DS
, AE
, DD
), it is
important the user specifies the right source(s) to derive the cause of
death from.
For example, if the date of death is collected in the AE form when
the AE is Fatal, the cause of death would be set to the preferred term
(AEDECOD
) of that Fatal AE, while if the date of death is
collected in the DS
form, the cause of death would be set
to the disposition term (DSTERM
). To achieve this, the
dthcaus_source()
objects must be specified and defined such
as it fits the study requirement.
dthcaus_source()
specifications:
-
dataset_name
: the name of the dataset where to search for death information, -
filter
: the condition to define death, -
date
: the date of death, -
mode
:first
orlast
to select the first/last date of death if multiple dates are collected, -
dthcaus
: variable or text used to populateDTHCAUS
. -
traceability_vars
: whether the traceability variables need to be added (e.g source domain, sequence, variable)
An example call to define the sources would be:
src_ae <- dthcaus_source(
dataset_name = "ae",
filter = AEOUT == "FATAL",
date = AESTDTM,
mode = "first",
dthcaus = AEDECOD
)
src_ds <- dthcaus_source(
dataset_name = "ds",
filter = DSDECOD == "DEATH" & grepl("DEATH DUE TO", DSTERM),
date = DSSTDT,
mode = "first",
dthcaus = "Death in DS"
)
Once the sources are defined, the function
derive_var_dthcaus()
can be used to derive
DTHCAUS
:
ae_ext <- derive_vars_dtm(
ae,
dtc = AESTDTC,
new_vars_prefix = "AEST",
highest_imputation = "M",
flag_imputation = "none"
)
adsl <- adsl %>%
derive_var_dthcaus(src_ae, src_ds, source_datasets = list(ae = ae_ext, ds = ds_ext))
The function also offers the option to add some traceability
variables (e.g. DTHDOM
would store the domain where the
date of death is collected, and DTHSEQ
would store the
xxSEQ
value of that domain). To add them, the
traceability_vars
argument must be added to the
dthcaus_source()
arguments:
src_ae <- dthcaus_source(
dataset_name = "ae",
filter = AEOUT == "FATAL",
date = AESTDTM,
mode = "first",
dthcaus = AEDECOD,
traceability_vars = exprs(DTHDOM = "AE", DTHSEQ = AESEQ)
)
src_ds <- dthcaus_source(
dataset_name = "ds",
filter = DSDECOD == "DEATH" & grepl("DEATH DUE TO", DSTERM),
date = DSSTDT,
mode = "first",
dthcaus = DSTERM,
traceability_vars = exprs(DTHDOM = "DS", DTHSEQ = DSSEQ)
)
adsl <- adsl %>%
select(-DTHCAUS) %>% # remove it before deriving it again
derive_var_dthcaus(src_ae, src_ds, source_datasets = list(ae = ae_ext, ds = ds_ext))
Duration Relative to Death
The function derive_vars_duration()
can be used to
derive duration relative to death like the Relative Day of Death
(DTHADY
) or the numbers of days from last dose to death
(LDDTHELD
).
Example calls:
- Relative Day of Death
adsl <- adsl %>%
derive_vars_duration(
new_var = DTHADY,
start_date = TRTSDT,
end_date = DTHDT
)
- Elapsed Days from Last Dose to Death
adsl <- adsl %>%
derive_vars_duration(
new_var = LDDTHELD,
start_date = TRTEDT,
end_date = DTHDT,
add_one = FALSE
)
Derive the Last Date Known Alive (LSTALVDT
)
Similarly as for the cause of death (DTHCAUS
), the last
known alive date (LSTALVDT
) can be derived from multiples
sources and the user must ensure the sources
(date_source()
) are correctly defined.
date_source()
specifications:
-
dataset_name
: the name of the dataset where to search for date information, -
filter
: the filter to apply on the datasets, -
date
: the date of interest, -
date_imputation
: whether and how to impute partial dates, -
traceability_vars
: whether the traceability variables need to be added (e.g source domain, sequence, variable)
An example could be :
ae_start_date <- date_source(
dataset_name = "ae",
date = AESTDT
)
ae_end_date <- date_source(
dataset_name = "ae",
date = AEENDT
)
lb_date <- date_source(
dataset_name = "lb",
date = LBDT,
filter = !is.na(LBDT)
)
trt_end_date <- date_source(
dataset_name = "adsl",
date = TRTEDT
)
Once the sources are defined, the function
derive_var_extreme_dt()
can be used to derive
LSTALVDT
:
# impute AE start and end date to first
ae_ext <- ae %>%
derive_vars_dt(
dtc = AESTDTC,
new_vars_prefix = "AEST",
highest_imputation = "M"
) %>%
derive_vars_dt(
dtc = AEENDTC,
new_vars_prefix = "AEEN",
highest_imputation = "M"
)
# impute LB date to first
lb_ext <- derive_vars_dt(
lb,
dtc = LBDTC,
new_vars_prefix = "LB",
highest_imputation = "M"
)
adsl <- adsl %>%
derive_var_extreme_dt(
new_var = LSTALVDT,
ae_start_date, ae_end_date, lb_date, trt_end_date,
source_datasets = list(ae = ae_ext, adsl = adsl, lb = lb_ext),
mode = "last"
)
Similarly to dthcaus_source()
, the traceability
variables can be added by specifying the traceability_vars
argument in date_source()
.
ae_start_date <- date_source(
dataset_name = "ae",
date = AESTDT,
traceability_vars = exprs(LALVDOM = "AE", LALVSEQ = AESEQ, LALVVAR = "AESTDTC")
)
ae_end_date <- date_source(
dataset_name = "ae",
date = AEENDT,
traceability_vars = exprs(LALVDOM = "AE", LALVSEQ = AESEQ, LALVVAR = "AEENDTC")
)
lb_date <- date_source(
dataset_name = "lb",
date = LBDT,
filter = !is.na(LBDT),
traceability_vars = exprs(LALVDOM = "LB", LALVSEQ = LBSEQ, LALVVAR = "LBDTC")
)
trt_end_date <- date_source(
dataset_name = "adsl",
date = TRTEDTM,
traceability_vars = exprs(LALVDOM = "ADSL", LALVSEQ = NA_integer_, LALVVAR = "TRTEDTM")
)
adsl <- adsl %>%
select(-LSTALVDT) %>% # created in the previous call
derive_var_extreme_dt(
new_var = LSTALVDT,
ae_start_date, ae_end_date, lb_date, trt_end_date,
source_datasets = list(ae = ae_ext, adsl = adsl, lb = lb_ext),
mode = "last"
)
Derive Groupings and Populations
Grouping (e.g. AGEGR1
or REGION1
)
Numeric and categorical variables (AGE
,
RACE
, COUNTRY
, etc.) may need to be grouped to
perform the required analysis. admiral does not
currently have functionality to assist with all
required groupings. So, the user will often need to create his/her own
function to meet his/her study requirement.
For example, if
-
AGEGR1
is required to categorizeAGE
into<18
,18-64
and>64
, or -
REGION1
is required to categorizeCOUNTRY
inNorth America
,Rest of the World
,
the user defined functions would look like the following:
format_agegr1 <- function(var_input) {
case_when(
var_input < 18 ~ "<18",
between(var_input, 18, 64) ~ "18-64",
var_input > 64 ~ ">64",
TRUE ~ "Missing"
)
}
format_region1 <- function(var_input) {
case_when(
var_input %in% c("CAN", "USA") ~ "North America",
!is.na(var_input) ~ "Rest of the World",
TRUE ~ "Missing"
)
}
These functions are then used in a mutate()
statement to
derive the required grouping variables:
Population Flags (e.g. SAFFL
)
Since the populations flags are mainly company/study specific no
dedicated functions are provided, but in most cases they can easily be
derived using derive_var_merged_exist_flag
.
An example of an implementation could be:
adsl <- adsl %>%
derive_var_merged_exist_flag(
dataset_add = ex,
by_vars = exprs(STUDYID, USUBJID),
new_var = SAFFL,
condition = (EXDOSE > 0 | (EXDOSE == 0 & str_detect(EXTRT, "PLACEBO")))
)
Derive Other Variables
The users can add specific code to cover their need for the analysis.
The following functions are helpful for many ADSL derivations:
-
derive_vars_merged()
- Merge Variables from a Dataset to the Input Dataset -
derive_var_merged_exist_flag()
- Merge an Existence Flag -
derive_var_merged_cat()
- Merge a Categorization Variable -
derive_var_merged_character()
- Merge a Character Variable -
derive_var_merged_summary()
- Merge a Summary Variable
See also Generic Functions.
Add Labels and Attributes
Adding labels and attributes for SAS transport files is supported by the following packages:
metacore: establish a common foundation for the use of metadata within an R session.
metatools: enable the use of metacore objects. Metatools can be used to build datasets or enhance columns in existing datasets as well as checking datasets against the metadata.
xportr: functionality to associate all metadata information to a local R data frame, perform data set level validation checks and convert into a transport v5 file(xpt).
NOTE: All these packages are in the experimental phase, but the vision is to have them associated with an End to End pipeline under the umbrella of the pharmaverse. An example of applying metadata and perform associated checks can be found at the pharmaverse E2E example.
Example Script
ADaM | Sample Code |
---|---|
ADSL | ad_adsl.R |