## Select the First or Last Date from Several Sources

### Description

Select for each subject the first or last observation with respect to a date from a list of sources.

### Usage

filter_date_sources(
sources,
source_datasets,
by_vars,
create_datetime = FALSE,
subject_keys,
mode
)


### Arguments

 sources Sources A list of tte_source() objects is expected. source_datasets Source datasets A named list of datasets is expected. The dataset_name field of tte_source() refers to the dataset provided in the list. by_vars By variables If the parameter is specified, for each by group the observations are selected separately. create_datetime Create datetime variable? If set to TRUE, variables ADTM is created. Otherwise, variables ADT is created. subject_keys Variables to uniquely identify a subject A list of symbols created using vars() is expected. mode Selection mode (first or last) If "first" is specified, for each subject the first observation with respect to the date is included in the output dataset. If "last" is specified, the last observation is included in the output dataset. Permitted Values: "first", "last"

### 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. Then for each patient the first or last observation (with respect to date) is selected.

2. The ADT variable is set to the variable specified by the date element. If the date variable is a datetime variable, only the datepart is copied. If the source variable is a character variable, it is converted to a date. If the date is incomplete, it is imputed as the first possible date.

3. The CNSR is added and set to the value of the censor element.

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 ADT variable) from the single dataset is selected.

### Value

A dataset with one observation per subject as described in the "Details" section.

### Author(s)

Stefan Bundfuss

Source Specifications: assert_db_requirements(), assert_terms(), assert_valid_queries(), censor_source(), date_source(), death_event, derive_var_dthcaus(), event_source(), extend_source_datasets(), format.sdg_select(), format.smq_select(), list_tte_source_objects(), params(), query(), sdg_select(), smq_select(), tte_source(), validate_query(), validate_sdg_select(), validate_smq_select()

### Examples

library(tibble)
library(dplyr, warn.conflicts = FALSE)
library(lubridate)

~USUBJID, ~TRTSDT,           ~EOSDT,
"01",     ymd("2020-12-06"), ymd("2021-03-06"),
"02",     ymd("2021-01-16"), ymd("2021-02-03")
) %>%
mutate(STUDYID = "AB42")

ae <- tribble(
~USUBJID, ~AESTDTC,     ~AESEQ, ~AEDECOD,
"01",     "2021-01-03", 1,      "Flu",
"01",     "2021-03-04", 2,      "Cough",
"01",     "2021-01-01", 3,      "Flu"
) %>%
mutate(
STUDYID = "AB42",
AESTDT = ymd(AESTDTC)
)

ttae <- event_source(
dataset_name = "ae",
date = AESTDT,
set_values_to = vars(
EVNTDESC = "AE",
SRCDOM = "AE",
SRCVAR = "AESTDTC",
SRCSEQ = AESEQ
)
)

filter_date_sources(
sources = list(ttae),