Skip to contents

Add the first or last date from multiple sources to the dataset, e.g., the last known alive date (LSTALVDT).

Usage

derive_var_extreme_dt(
  dataset,
  new_var,
  ...,
  source_datasets,
  mode,
  subject_keys = get_admiral_option("subject_keys")
)

Arguments

dataset

Input dataset

The variables specified by subject_keys are required.

new_var

Name of variable to create

...

Source(s) of dates. One or more date_source() objects are expected.

source_datasets

A named list containing datasets in which to search for the first or last date

mode

Selection mode (first or last)

If "first" is specified, the first date for each subject is selected. If "last" is specified, the last date for each subject is selected.

Permitted Values: "first", "last"

subject_keys

Variables to uniquely identify a subject

A list of expressions where the expressions are symbols as returned by exprs() is expected.

Value

The input dataset with the new variable added.

Details

The following steps are performed to create the output dataset:

  1. For each source dataset the observations as specified by the filter element are selected and observations where date is NA are removed. Then for each patient the first or last observation (with respect to date and mode) is selected.

  2. The new variable is set to the variable specified by the date element.

  3. The variables specified by the traceability_vars element are added.

  4. The selected observations of all source datasets are combined into a single dataset.

  5. For each patient the first or last observation (with respect to the new variable and mode) from the single dataset is selected and the new variable is merged to the input dataset.

  6. The time part is removed from the new variable.

Examples

library(dplyr, warn.conflicts = FALSE)
library(admiral.test)
data("admiral_dm")
data("admiral_ae")
data("admiral_lb")
data("admiral_adsl")

# derive last known alive date (LSTALVDT)
ae_start <- date_source(
  dataset_name = "ae",
  date = AESTDT
)
ae_end <- date_source(
  dataset_name = "ae",
  date = AEENDT
)

ae_ext <- admiral_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"
  )

lb_date <- date_source(
  dataset_name = "lb",
  date = LBDT,
  filter = !is.na(LBDT),
)

lb_ext <- derive_vars_dt(
  admiral_lb,
  dtc = LBDTC,
  new_vars_prefix = "LB"
)

adsl_date <- date_source(dataset_name = "adsl", date = TRTEDT)

admiral_dm %>%
  derive_var_extreme_dt(
    new_var = LSTALVDT,
    ae_start, ae_end, lb_date, adsl_date,
    source_datasets = list(
      adsl = admiral_adsl,
      ae = ae_ext,
      lb = lb_ext
    ),
    mode = "last"
  ) %>%
  select(USUBJID, LSTALVDT)
#> # A tibble: 306 x 2
#>    USUBJID     LSTALVDT  
#>    <chr>       <date>    
#>  1 01-701-1015 2014-07-02
#>  2 01-701-1023 2012-09-02
#>  3 01-701-1028 2014-01-14
#>  4 01-701-1033 2014-04-14
#>  5 01-701-1034 2014-12-30
#>  6 01-701-1047 2013-04-07
#>  7 01-701-1057 NA        
#>  8 01-701-1097 2014-07-09
#>  9 01-701-1111 2012-09-17
#> 10 01-701-1115 2013-01-23
#> # … with 296 more rows

# derive last alive date and traceability variables
ae_start <- date_source(
  dataset_name = "ae",
  date = AESTDT,
  traceability_vars = exprs(
    LALVDOM = "AE",
    LALVSEQ = AESEQ,
    LALVVAR = "AESTDTC"
  )
)

ae_end <- 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"
  )
)

adsl_date <- date_source(
  dataset_name = "adsl",
  date = TRTEDT,
  traceability_vars = exprs(
    LALVDOM = "ADSL",
    LALVSEQ = NA_integer_,
    LALVVAR = "TRTEDT"
  )
)

admiral_dm %>%
  derive_var_extreme_dt(
    new_var = LSTALVDT,
    ae_start, ae_end, lb_date, adsl_date,
    source_datasets = list(
      adsl = admiral_adsl,
      ae = ae_ext,
      lb = lb_ext
    ),
    mode = "last"
  ) %>%
  select(USUBJID, LSTALVDT, LALVDOM, LALVSEQ, LALVVAR)
#> # A tibble: 306 x 5
#>    USUBJID     LSTALVDT   LALVDOM LALVSEQ LALVVAR
#>    <chr>       <date>     <chr>     <dbl> <chr>  
#>  1 01-701-1015 2014-07-02 ADSL         NA TRTEDT 
#>  2 01-701-1023 2012-09-02 LB          107 LBDTC  
#>  3 01-701-1028 2014-01-14 ADSL         NA TRTEDT 
#>  4 01-701-1033 2014-04-14 LB          107 LBDTC  
#>  5 01-701-1034 2014-12-30 ADSL         NA TRTEDT 
#>  6 01-701-1047 2013-04-07 LB          134 LBDTC  
#>  7 01-701-1057 NA         NA           NA NA     
#>  8 01-701-1097 2014-07-09 ADSL         NA TRTEDT 
#>  9 01-701-1111 2012-09-17 LB           73 LBDTC  
#> 10 01-701-1115 2013-01-23 ADSL         NA TRTEDT 
#> # … with 296 more rows