# Adding patient demographics

## 1.1 Introduction

The OMOP CDM is a person-centric model. The person table contains records that uniquely identify each individual along with some of their demographic information. Below we create a mock CDM reference which, as is standard, has a person table which contains fields which indicate an individual’s date of birth, gender, race, and ethnicity. Each of these, except for date of birth, are represented by a concept ID (and as the person table contains one record per person these fields are treated as time-invariant).

    library(PatientProfiles)
    library(duckdb)
    library(dplyr)

    cdm <- mockPatientProfiles(numberIndividuals = 10000, source = "duckdb")

    cdm$person |>
      dplyr::glimpse()

    ## Rows: ??
    ## Columns: 5
    ## Database: DuckDB 1.5.5 [root@Darwin 25.5.0:R 4.4.1/:memory:]
    ## $ person_id            <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15…
    ## $ gender_concept_id    <int> 8507, 8507, 8507, 8507, 8532, 8507, 8532, 8532, 8…
    ## $ year_of_birth        <int> 1946, 1929, 1928, 1975, 1924, 1931, 1929, 1917, 1…
    ## $ race_concept_id      <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
    ## $ ethnicity_concept_id <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…

As well as the person table, every CDM reference will include an observation period table. This table contains spans of times during which an individual is considered to being under observation. Individuals can have multiple observation periods, but they cannot overlap.

    cdm$observation_period |>
      dplyr::glimpse()

    ## Rows: ??
    ## Columns: 5
    ## Database: DuckDB 1.5.5 [root@Darwin 25.5.0:R 4.4.1/:memory:]
    ## $ person_id                     <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1…
    ## $ observation_period_start_date <date> 1946-01-01, 1929-01-01, 1928-01-01, 197…
    ## $ observation_period_end_date   <date> 1970-09-06, 1957-05-04, 1952-08-24, 201…
    ## $ period_type_concept_id        <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
    ## $ observation_period_id         <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1…

When performing analyses we will often be interested in working with the person and observation period tables to identify individuals’ characteristics on some date of interest. PatientProfiles provides a number of functions that can help us do this.

## 1.2 Adding characteristics to OMOP CDM tables

Let’s say we’re working with the condition occurrence table.

    cdm$condition_occurrence |>
      glimpse()

    ## Rows: ??
    ## Columns: 6
    ## Database: DuckDB 1.5.5 [root@Darwin 25.5.0:R 4.4.1/:memory:]
    ## $ person_id                 <int> 2996, 9184, 8318, 5248, 7079, 3122, 7554, 3,…
    ## $ condition_start_date      <date> 1943-11-23, 1951-01-27, 1929-03-08, 1907-03…
    ## $ condition_end_date        <date> 1956-11-21, 1952-11-01, 1933-07-17, 1912-05…
    ## $ condition_occurrence_id   <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1…
    ## $ condition_concept_id      <int> 440086, 40479768, 4043241, 4043241, 4059173,…
    ## $ condition_type_concept_id <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…

This table contains diagnoses of individuals and we might, for example, want to identify their age on their date of diagnosis. This involves linking back to the person table which contains their date of birth (split across three different columns). PatientProfiles provides a simple function for this. `addAge()` will add a new column to the table containing each patient’s age relative to the specified index date.

    cdm$condition_occurrence <- cdm$condition_occurrence |>
      addAge(indexDate = "condition_start_date")

    cdm$condition_occurrence |>
      glimpse()

    ## Rows: ??
    ## Columns: 7
    ## Database: DuckDB 1.5.5 [root@Darwin 25.5.0:R 4.4.1/:memory:]
    ## $ person_id                 <int> 3, 12, 52, 54, 57, 61, 67, 69, 70, 71, 96, 1…
    ## $ condition_start_date      <date> 1951-04-23, 1936-02-04, 1979-06-20, 1930-11…
    ## $ condition_end_date        <date> 1951-11-10, 1937-02-09, 1996-11-17, 1934-10…
    ## $ condition_occurrence_id   <int> 8, 838, 259, 430, 476, 590, 347, 676, 1077, …
    ## $ condition_concept_id      <int> 313217, 198199, 4218389, 4329847, 4218389, 4…
    ## $ condition_type_concept_id <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
    ## $ age                       <dbl> 23, 12, 8, 5, 9, 2, 20, 29, 7, 24, 18, 1, 2,…

As well as calculating age, we can also create age groups at the same time. Here we create three age groups: those aged 0 to 17, those 18 to 65, and those 66 or older.

    cdm$condition_occurrence <- cdm$condition_occurrence |>
      addAge(
        indexDate = "condition_start_date",
        ageGroup = list(
          "0 to 17" = c(0, 17),
          "18 to 65" = c(18, 65),
          ">= 66" = c(66, Inf)
        )
      )

    cdm$condition_occurrence |>
      glimpse()

    ## Rows: ??
    ## Columns: 8
    ## Database: DuckDB 1.5.5 [root@Darwin 25.5.0:R 4.4.1/:memory:]
    ## $ person_id                 <int> 3, 12, 52, 54, 57, 61, 67, 69, 70, 71, 96, 1…
    ## $ condition_start_date      <date> 1951-04-23, 1939-10-15, 1979-06-20, 1936-08…
    ## $ condition_end_date        <date> 1951-11-10, 1960-08-19, 1996-11-17, 1937-04…
    ## $ condition_occurrence_id   <int> 8, 720, 259, 282, 476, 590, 347, 676, 1077, …
    ## $ condition_concept_id      <int> 313217, 378419, 4218389, 4218389, 4218389, 4…
    ## $ condition_type_concept_id <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
    ## $ age                       <dbl> 23, 15, 8, 11, 9, 2, 20, 29, 7, 24, 18, 1, 2…
    ## $ age_group                 <chr> "18 to 65", "0 to 17", "0 to 17", "0 to 17",…

By default, when adding age the new column will have been called “age” and will have been calculated using all available information on date of birth contained in the person. We can though also alter these defaults. Here, for example, we impose that month of birth is January and day of birth is the 1st for all individuals.

    cdm$condition_occurrence <- cdm$condition_occurrence |>
      addAge(
        indexDate = "condition_start_date",
        ageName = "age_from_year_of_birth",
        ageMissingMonth = 1,
        ageMissingDay = 1,
        ageImposeMonth = TRUE,
        ageImposeDay = TRUE
      )

    cdm$condition_occurrence |>
      glimpse()

    ## Rows: ??
    ## Columns: 9
    ## Database: DuckDB 1.5.5 [root@Darwin 25.5.0:R 4.4.1/:memory:]
    ## $ person_id                 <int> 3, 12, 52, 54, 57, 61, 67, 69, 70, 71, 96, 1…
    ## $ condition_start_date      <date> 1951-04-23, 1936-02-04, 1979-06-20, 1930-11…
    ## $ condition_end_date        <date> 1951-11-10, 1937-02-09, 1996-11-17, 1934-10…
    ## $ condition_occurrence_id   <int> 8, 838, 259, 430, 476, 590, 347, 676, 1077, …
    ## $ condition_concept_id      <int> 313217, 198199, 4218389, 4329847, 4218389, 4…
    ## $ condition_type_concept_id <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
    ## $ age                       <dbl> 23, 12, 8, 5, 9, 2, 20, 29, 7, 24, 18, 1, 2,…
    ## $ age_group                 <chr> "18 to 65", "0 to 17", "0 to 17", "0 to 17",…
    ## $ age_from_year_of_birth    <dbl> 23, 12, 8, 5, 9, 2, 20, 29, 7, 24, 18, 1, 2,…

As well as age at diagnosis, we might also want identify patients’ sex. PatientProfiles provides the `addSex()` function that will add this for us. Because this is treated as time-invariant, we will not have to specify any index variable.

    cdm$condition_occurrence <- cdm$condition_occurrence |>
      addSex()

    cdm$condition_occurrence |>
      glimpse()

    ## Rows: ??
    ## Columns: 10
    ## Database: DuckDB 1.5.5 [root@Darwin 25.5.0:R 4.4.1/:memory:]
    ## $ person_id                 <int> 3, 12, 52, 54, 57, 61, 67, 69, 70, 71, 96, 1…
    ## $ condition_start_date      <date> 1951-04-23, 1939-10-15, 1979-06-20, 1936-08…
    ## $ condition_end_date        <date> 1951-11-10, 1960-08-19, 1996-11-17, 1937-04…
    ## $ condition_occurrence_id   <int> 8, 720, 259, 282, 476, 590, 347, 676, 1077, …
    ## $ condition_concept_id      <int> 313217, 378419, 4218389, 4218389, 4218389, 4…
    ## $ condition_type_concept_id <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
    ## $ age                       <dbl> 23, 15, 8, 11, 9, 2, 20, 29, 7, 24, 18, 1, 2…
    ## $ age_group                 <chr> "18 to 65", "0 to 17", "0 to 17", "0 to 17",…
    ## $ age_from_year_of_birth    <dbl> 23, 15, 8, 11, 9, 2, 20, 29, 7, 24, 18, 1, 2…
    ## $ sex                       <chr> "Male", "Female", "Female", "Female", "Femal…

Similarly, we could also identify whether an individual was in observation at the time of their diagnosis (i.e. had an observation period that overlaps with their diagnosis date), as well as identifying how much prior observation time they had on this date and how much they have following it.

    cdm$condition_occurrence <- cdm$condition_occurrence |>
      addInObservation(indexDate = "condition_start_date") |>
      addPriorObservation(indexDate = "condition_start_date") |>
      addFutureObservation(indexDate = "condition_start_date")

    cdm$condition_occurrence |>
      glimpse()

    ## Rows: ??
    ## Columns: 13
    ## Database: DuckDB 1.5.5 [root@Darwin 25.5.0:R 4.4.1/:memory:]
    ## $ person_id                 <int> 3, 12, 52, 54, 57, 61, 67, 69, 70, 71, 96, 1…
    ## $ condition_start_date      <date> 1951-04-23, 1939-10-15, 1979-06-20, 1936-08…
    ## $ condition_end_date        <date> 1951-11-10, 1960-08-19, 1996-11-17, 1937-04…
    ## $ condition_occurrence_id   <int> 8, 720, 259, 282, 476, 590, 347, 676, 1077, …
    ## $ condition_concept_id      <int> 313217, 378419, 4218389, 4218389, 4218389, 4…
    ## $ condition_type_concept_id <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
    ## $ age                       <dbl> 23, 15, 8, 11, 9, 2, 20, 29, 7, 24, 18, 1, 2…
    ## $ age_group                 <chr> "18 to 65", "0 to 17", "0 to 17", "0 to 17",…
    ## $ age_from_year_of_birth    <dbl> 23, 15, 8, 11, 9, 2, 20, 29, 7, 24, 18, 1, 2…
    ## $ sex                       <chr> "Male", "Female", "Female", "Female", "Femal…
    ## $ in_observation            <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,…
    ## $ prior_observation         <dbl> 8513, 5766, 3092, 4258, 3335, 1003, 7330, 10…
    ## $ future_observation        <dbl> 489, 8024, 6676, 1234, 794, 11894, 9007, 835…

For these functions which work with information from the observation table, it is important to note that the results will be based on the observation period during which the index date falls within. Moreover, if a patient is not under observation at the specified date, `addPriorObservation()` and `addFutureObservation()` functions will return NA.

When checking whether someone is in observation the default is that we are checking whether someone was in observation on the index date. We could though expand this and consider a window of time around this date. For example here we add a variable indicating whether someone was in observation from 180 days before the index date to 30 days following it.

    cdm$condition_occurrence |>
      addInObservation(
        indexDate = "condition_start_date",
        window = c(-180, 30)
      ) |>
      glimpse()

    ## Rows: ??
    ## Columns: 13
    ## Database: DuckDB 1.5.5 [root@Darwin 25.5.0:R 4.4.1/:memory:]
    ## $ person_id                 <int> 3, 12, 52, 54, 57, 61, 67, 69, 70, 71, 96, 1…
    ## $ condition_start_date      <date> 1951-04-23, 1939-10-15, 1979-06-20, 1936-08…
    ## $ condition_end_date        <date> 1951-11-10, 1960-08-19, 1996-11-17, 1937-04…
    ## $ condition_occurrence_id   <int> 8, 720, 259, 282, 476, 590, 347, 676, 1077, …
    ## $ condition_concept_id      <int> 313217, 378419, 4218389, 4218389, 4218389, 4…
    ## $ condition_type_concept_id <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
    ## $ age                       <dbl> 23, 15, 8, 11, 9, 2, 20, 29, 7, 24, 18, 1, 2…
    ## $ age_group                 <chr> "18 to 65", "0 to 17", "0 to 17", "0 to 17",…
    ## $ age_from_year_of_birth    <dbl> 23, 15, 8, 11, 9, 2, 20, 29, 7, 24, 18, 1, 2…
    ## $ sex                       <chr> "Male", "Female", "Female", "Female", "Femal…
    ## $ in_observation            <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,…
    ## $ prior_observation         <dbl> 8513, 5766, 3092, 4258, 3335, 1003, 7330, 10…
    ## $ future_observation        <dbl> 489, 8024, 6676, 1234, 794, 11894, 9007, 835…

We can also specify a window and require that an individual is present for only some days within it. Here we add a variable indicating whether the individual was in observation at least a year in the future,

    cdm$condition_occurrence |>
      addInObservation(
        indexDate = "condition_start_date",
        window = c(365, Inf),
        completeInterval = FALSE
      ) |>
      glimpse()

    ## Rows: ??
    ## Columns: 13
    ## Database: DuckDB 1.5.5 [root@Darwin 25.5.0:R 4.4.1/:memory:]
    ## $ person_id                 <int> 3, 12, 52, 54, 57, 61, 67, 69, 70, 71, 96, 1…
    ## $ condition_start_date      <date> 1951-04-23, 1939-10-15, 1979-06-20, 1936-08…
    ## $ condition_end_date        <date> 1951-11-10, 1960-08-19, 1996-11-17, 1937-04…
    ## $ condition_occurrence_id   <int> 8, 720, 259, 282, 476, 590, 347, 676, 1077, …
    ## $ condition_concept_id      <int> 313217, 378419, 4218389, 4218389, 4218389, 4…
    ## $ condition_type_concept_id <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
    ## $ age                       <dbl> 23, 15, 8, 11, 9, 2, 20, 29, 7, 24, 18, 1, 2…
    ## $ age_group                 <chr> "18 to 65", "0 to 17", "0 to 17", "0 to 17",…
    ## $ age_from_year_of_birth    <dbl> 23, 15, 8, 11, 9, 2, 20, 29, 7, 24, 18, 1, 2…
    ## $ sex                       <chr> "Male", "Female", "Female", "Female", "Femal…
    ## $ in_observation            <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,…
    ## $ prior_observation         <dbl> 8513, 5766, 3092, 4258, 3335, 1003, 7330, 10…
    ## $ future_observation        <dbl> 489, 8024, 6676, 1234, 794, 11894, 9007, 835…

## 1.3 Adding characteristics to a cohort tables

The above functions can be used on both standard OMOP CDM tables and cohort tables. Note as the default index date in the functions is “cohort_start_date” we can now omit this.

    cdm$cohort1 |>
      glimpse()

    ## Rows: ??
    ## Columns: 4
    ## Database: DuckDB 1.5.5 [root@Darwin 25.5.0:R 4.4.1/:memory:]
    ## $ cohort_definition_id <int> 1, 2, 2, 1, 2, 3, 1, 3, 2, 1, 1, 1, 1, 1, 2, 1, 2…
    ## $ subject_id           <int> 1594, 2289, 8814, 2563, 3350, 9826, 5314, 9293, 2…
    ## $ cohort_start_date    <date> 1999-01-23, 1915-06-30, 1993-08-21, 1947-10-31, …
    ## $ cohort_end_date      <date> 2000-10-17, 1920-06-14, 1994-08-30, 1950-12-28, …

    cdm$cohort1 <- cdm$cohort1 |>
      addAge(ageGroup = list(
        "0 to 17" = c(0, 17),
        "18 to 65" = c(18, 65),
        ">= 66" = c(66, Inf)
      )) |>
      addSex() |>
      addInObservation() |>
      addPriorObservation() |>
      addFutureObservation()

    cdm$cohort1 |>
      glimpse()

    ## Rows: ??
    ## Columns: 10
    ## Database: DuckDB 1.5.5 [root@Darwin 25.5.0:R 4.4.1/:memory:]
    ## $ cohort_definition_id <int> 1, 2, 2, 1, 2, 3, 1, 3, 2, 1, 1, 1, 1, 1, 2, 1, 2…
    ## $ subject_id           <int> 1594, 2289, 8814, 2563, 3350, 9826, 5314, 9293, 2…
    ## $ cohort_start_date    <date> 1999-01-23, 1915-06-30, 1993-08-21, 1947-10-31, …
    ## $ cohort_end_date      <date> 2000-10-17, 1920-06-14, 1994-08-30, 1950-12-28, …
    ## $ age                  <dbl> 23, 5, 17, 8, 0, 0, 29, 16, 32, 5, 5, 0, 28, 18, …
    ## $ age_group            <chr> "18 to 65", "0 to 17", "0 to 17", "0 to 17", "0 t…
    ## $ sex                  <chr> "Male", "Female", "Female", "Male", "Female", "Ma…
    ## $ in_observation       <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
    ## $ prior_observation    <dbl> 8423, 2006, 6442, 3225, 182, 141, 10698, 6138, 12…
    ## $ future_observation   <dbl> 676, 7465, 1015, 3758, 6017, 8194, 3312, 8503, 58…

## 1.4 Getting multiple characteristics at once

The above functions, which are chained together, each fetch the related information one by one. In the cases where we are interested in adding multiple characteristics, we can add these all at the same time using the more general `addDemographics()` functions. This will be more efficient that adding characteristics as it requires fewer joins between our table of interest and the person and observation period tables.

    cdm$cohort2 |>
      glimpse()

    ## Rows: ??
    ## Columns: 4
    ## Database: DuckDB 1.5.5 [root@Darwin 25.5.0:R 4.4.1/:memory:]
    ## $ cohort_definition_id <int> 2, 3, 3, 1, 2, 1, 1, 2, 3, 2, 1, 2, 1, 2, 1, 3, 3…
    ## $ subject_id           <int> 4016, 779, 3040, 5947, 4045, 3489, 8945, 3780, 18…
    ## $ cohort_start_date    <date> 1979-05-02, 1926-09-01, 1952-11-27, 1982-02-19, …
    ## $ cohort_end_date      <date> 2012-08-29, 1963-02-24, 1966-07-08, 2004-03-30, …

    tictoc::tic()
    cdm$cohort2 |>
      addAge(ageGroup = list(
        "0 to 17" = c(0, 17),
        "18 to 65" = c(18, 65),
        ">= 66" = c(66, Inf)
      )) |>
      addSex() |>
      addInObservation() |>
      addPriorObservation() |>
      addFutureObservation()

    ## # Source:   table<og_157_1787323324> [?? x 10]
    ## # Database: DuckDB 1.5.5 [root@Darwin 25.5.0:R 4.4.1/:memory:]
    ##    cohort_definition_id subject_id cohort_start_date cohort_end_date   age
    ##                   <int>      <int> <date>            <date>          <dbl>
    ##  1                    2       4016 1979-05-02        2012-08-29          2
    ##  2                    3        779 1926-09-01        1963-02-24          2
    ##  3                    3       3040 1952-11-27        1966-07-08         10
    ##  4                    1       5947 1982-02-19        2004-03-30         13
    ##  5                    2       4045 1985-04-08        1997-05-21         26
    ##  6                    1       3489 1944-03-11        1949-02-14          0
    ##  7                    1       8945 1933-08-02        1939-12-23          8
    ##  8                    2       3780 1947-02-24        1949-03-20         24
    ##  9                    3       1834 1937-07-18        1961-09-09          5
    ## 10                    2       7200 1979-10-20        1979-12-24         26
    ## # ℹ more rows
    ## # ℹ 5 more variables: age_group <chr>, sex <chr>, in_observation <dbl>,
    ## #   prior_observation <dbl>, future_observation <dbl>

    tictoc::toc()

    ## 0.76 sec elapsed

    tictoc::tic()
    cdm$cohort2 |>
      addDemographics(
        age = TRUE,
        ageName = "age",
        ageGroup = list(
          "0 to 17" = c(0, 17),
          "18 to 65" = c(18, 65),
          ">= 66" = c(66, Inf)
        ),
        sex = TRUE,
        sexName = "sex",
        priorObservation = TRUE,
        priorObservationName = "prior_observation",
        futureObservation = FALSE,
      ) |>
      glimpse()

    ## Rows: ??
    ## Columns: 8
    ## Database: DuckDB 1.5.5 [root@Darwin 25.5.0:R 4.4.1/:memory:]
    ## $ cohort_definition_id <int> 3, 2, 1, 2, 3, 1, 1, 1, 2, 2, 2, 1, 2, 3, 2, 3, 2…
    ## $ subject_id           <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15…
    ## $ cohort_start_date    <date> 1961-07-07, 1947-02-28, 1949-01-16, 2000-07-10, …
    ## $ cohort_end_date      <date> 1970-09-06, 1951-05-24, 1950-01-26, 2000-10-18, …
    ## $ age                  <dbl> 15, 18, 21, 25, 0, 28, 19, 12, 38, 4, 35, 5, 26, …
    ## $ age_group            <chr> "0 to 17", "18 to 65", "18 to 65", "18 to 65", "0…
    ## $ sex                  <chr> "Male", "Male", "Male", "Male", "Female", "Male",…
    ## $ prior_observation    <dbl> 5666, 6632, 7686, 9322, 323, 10313, 6940, 4605, 1…

    tictoc::toc()

    ## 0.287 sec elapsed

In our small mock dataset we see a small improvement in performance, but this difference will become much more noticeable when working with real data that will typically be far larger.