Skip to contents

The examples use invented discharges.

Match diagnosis and procedure codes

nis <- nis_open(nis_example(years = 2019:2020)) |>
  nis_flag_dx(
    heart_failure = list(regex = "I50[0-9]+", exclude = "I509")
  ) |>
  nis_flag_dx(principal_hf = "I50", position = "principal") |>
  nis_flag_pr(pci = "0270")

nis |> count(heart_failure, principal_hf, pci) |> collect()
#> # A tibble: 4 × 4
#>   heart_failure principal_hf pci       n
#>   <lgl>         <lgl>        <lgl> <dbl>
#> 1 TRUE          TRUE         TRUE     54
#> 2 FALSE         FALSE        FALSE    42
#> 3 TRUE          TRUE         FALSE    54
#> 4 FALSE         FALSE        TRUE     42

Each code slot is a numbered field. The default checks every slot. Use position = "principal" for slot 1. A string such as "I50" matches a prefix and a regex matches the whole code. Either can set the flag. Excluding a code leaves other slots eligible to match.

mixed <- nis_open(nis_example(years = 2014:2016)) |>
  mutate(DX1 = if_else(
    (YEAR == 2014 | (YEAR == 2015 & DQTR < 4)) & HOSP_NIS %% 2L == 0L,
    "4280", DX1
  )) |>
  nis_flag_dx(hf = list(icd9 = "428", icd10 = "I50"), system = "both")

mixed |> filter(YEAR == 2015) |> count(DQTR, hf) |> arrange(DQTR, hf) |> collect()
#> # A tibble: 8 × 3
#>    DQTR hf        n
#>   <int> <lgl> <dbl>
#> 1     1 FALSE    12
#> 2     1 TRUE     12
#> 3     2 FALSE    12
#> 4     2 TRUE     12
#> 5     3 FALSE    12
#> 6     3 TRUE     12
#> 7     4 FALSE     6
#> 8     4 TRUE     18
nis_attrition(mixed, adult = AGE >= 18, hf = hf, by_year = TRUE, .design = TRUE)
#> # A tibble: 9 × 8
#>    YEAR  step criterion             n_unweighted n_dropped_unweighted n_weighted
#>   <int> <int> <chr>                        <int>                <int>      <dbl>
#> 1  2014     0 Valid design dischar…           96                   NA      114. 
#> 2  2014     1 adult                           96                    0      114. 
#> 3  2014     2 hf                              48                   48       57.6
#> 4  2015     0 Valid design dischar…           96                   NA      114. 
#> 5  2015     1 adult                           96                    0      114. 
#> 6  2015     2 hf                              54                   42       64  
#> 7  2016     0 Valid design dischar…           96                   NA      114. 
#> 8  2016     1 adult                           96                    0      114. 
#> 9  2016     2 hf                              54                   42       62.4
#> # ℹ 2 more variables: n_dropped_weighted <dbl>, pct_retained <dbl>

Supply separate definitions for each system when the source contains both slot sets. The example adds invented ICD-9 matches so you can see both systems in the quarter audit and follow eligibility counts across all three years. Check how your own code sets behave around the transition. Years and coding eras

Use CCSR categories or procedure days

The Clinical Classifications Software Refined (CCSR) groups ICD-10 codes into clinical categories.

nis |>
  nis_add_ccsr(position = "default", version = "2025.1") |>
  count(ccsr) |>
  collect()
#> # A tibble: 2 × 2
#>   ccsr       n
#>   <chr>  <dbl>
#> 1 CIR019   108
#> 2 RSP002    84

timing <- nis |> mutate(PRDAY1 = 0L, PRDAY2 = 1L)
nis_audit_prday(timing, pr = "0270")
#> # A tibble: 50 × 8
#>     YEAR  slot matched_slots known_day missing_day sentinel_invalid_day
#>    <int> <int>         <int>     <int>       <int>                <int>
#>  1  2019     1            48        48           0                    0
#>  2  2019     2             0         0           0                    0
#>  3  2019     3             0         0           0                    0
#>  4  2019     4             0         0           0                    0
#>  5  2019     5             0         0           0                    0
#>  6  2019     6             0         0           0                    0
#>  7  2019     7             0         0           0                    0
#>  8  2019     8             0         0           0                    0
#>  9  2019     9             0         0           0                    0
#> 10  2019    10             0         0           0                    0
#> # ℹ 40 more rows
#> # ℹ 2 more variables: absent_prday_column <int>, pct_known <dbl>
timing |>
  nis_flag_pr_timing(
    index_pr = list(prefix = "0270", exclude = "02703DZ"),
    outcome_pr = list(regex = "02L73.*"),
    relation = "after", missing_day = "indeterminate"
  ) |>
  count(outcome_post_index) |>
  collect()
#> # A tibble: 2 × 2
#>   outcome_post_index     n
#>   <lgl>              <dbl>
#> 1 FALSE                168
#> 2 TRUE                  24

The bundled v2025.1 mappings can keep the same definitions across study years. position = "default" returns one inpatient category for the principal diagnosis. The example supplies invented procedure days. Timing uses the earliest known index day and accepts the same prefixes, full-code regular expressions, and exact exclusions as nis_flag_pr(). For system = "both", supply named icd9 and icd10 definitions for each procedure. Timing compares slots within the same code system. Audit procedure-day availability before interpreting an order.

Count eligibility steps and save the cohort

flow <- nis_attrition(
  nis, adult = AGE >= 18, hf = heart_failure, by_year = TRUE, .design = TRUE
)
flow |>
  select(YEAR, criterion, n_unweighted, n_dropped_unweighted) |>
  gt::gt() |>
  gt::cols_label(
    YEAR = "Year", criterion = "Eligibility step",
    n_unweighted = "Remaining", n_dropped_unweighted = "Dropped"
  ) |>
  gt::sub_missing(missing_text = "—")
Year Eligibility step Remaining Dropped
2020 Valid design discharges 96 —
2020 adult 96 0
2020 hf 54 42
2019 Valid design discharges 96 —
2019 adult 96 0
2019 hf 54 42

eligible <- nis_attrition_cohort(flow)
export <- nis |> mutate(stayDays = LOS)
path <- nis_write_cohort(export, !!eligible, tempfile(), keep = c(AGE, stayDays))
design <- nis_design(nis_open(path), keep = stayDays)
summarise(design, total = survey_total(), mean_stay = survey_mean(stayDays))
#> # A tibble: 1 × 4
#>   total total_se mean_stay mean_stay_se
#>   <dbl>    <dbl>     <dbl>        <dbl>
#> 1  125.     17.3      5.66        0.229

The flow counts remaining discharges after each criterion. The saved Parquet cohort retains the hospital structure used for variance. Open it without supplying a new cohort to nis_design(). Derived names such as stayDays keep their spelling when you reopen the saved cohort. Keep the code that defined your flags with the study.