Skip to contents

Searches for specific drug codes (ATC or product names) in Swedish prescription registry data and adds corresponding boolean variables to the skeleton based on prescription periods and duration of treatment.

Usage

add_rx(
  skeleton,
  lmed,
  id_name = "lopnr",
  codes = list(rx_hormones_pubblock = c("L02AE", "H01CA")),
  source = "atc",
  rxs = NULL
)

Arguments

skeleton

A data.table containing the main skeleton structure created by create_skeleton

lmed

A data.table containing prescription registry data (LMED). Must have columns for person ID, prescription date (edatum), treatment duration (fddd), and drug codes (atc) or product names (produkt)

id_name

Character string specifying the name of the ID variable (default: "lopnr")

codes

Named list of drug code patterns to search for. Names become variable names in skeleton.

source

Character string specifying search field and matching strategy:

  • "atc" (default) - Prefix matching in ATC codes (e.g., "N06A" matches "N06AB06"). Uses startsWith() for fast C-level matching.

  • "produkt" - Exact matching on product names (e.g., "Delestrogen" matches only "Delestrogen", not "Delestrogen Extra"). Uses %chin% for fast lookup.

rxs

Deprecated. Use codes instead. Default includes hormone therapy codes for puberty blockers (L02AE, H01CA). Common patterns include:

  • Antidepressants: "N06A"

  • Hormone therapy: "G03", "L02AE", "H01CA"

  • Cardiovascular drugs: "C07", "C08", "C09"

Value

The skeleton data.table is modified by reference with prescription variables added. Variables are TRUE during periods when the prescription is active based on start/stop dates calculated from prescription date + treatment duration

See also

create_skeleton for creating the skeleton structure, add_diagnoses for diagnosis codes, add_operations for surgical procedures, make_lowercase_names for data preprocessing

Other data_integration: add_annual(), add_cods(), add_diagnoses(), add_icdo3s(), add_onetime(), add_operations(), add_snomed3s(), add_snomedo10s()

Examples

# Load fake data
data("fake_person_ids", package = "swereg")
data("fake_prescriptions", package = "swereg")
swereg::make_lowercase_names(fake_prescriptions, date_columns = "edatum")

# Create skeleton
skeleton <- create_skeleton(fake_person_ids[1:10], "2020-01-01", "2020-12-31")

# Add prescription data
rx_patterns <- list(
  "antidepressants" = c("N06A"),
  "hormones" = c("G03", "L02AE")
)
add_rx(skeleton, fake_prescriptions, "p444_lopnr_personnr", rx_patterns, "atc")