Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

export(buildDrugEra)
export(buildRelease)
export(buildReleaseZip)
export(createDdl)
Expand Down
45 changes: 45 additions & 0 deletions R/buildDrugEra.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#' Build the DRUG_ERA table from the DRUG_EXPOSURE table
#'
#' Runs the drug era script that is shown on the "SQL Scripts" page of the CDM website
#' (\code{inst/sql/sql_server/OMOP_CDM_drug_era.sql}) on a database. The script strings together the
#' periods of time that a person is exposed to an active drug ingredient, allowing for gaps of up to 30
#' days, and inserts the resulting eras into the DRUG_ERA table.
#'
#' @details
#' The script reads DRUG_EXPOSURE, CONCEPT and CONCEPT_ANCESTOR (RxNorm ingredients) from the CDM schema and
#' inserts into DRUG_ERA without deleting existing rows, so DRUG_ERA is expected to be empty. It uses a
#' temp table, so on dialects that emulate temp tables (for example Oracle, BigQuery, Spark and Snowflake)
#' \code{tempEmulationSchema} has to be set.
#'
#' @param connectionDetails An object of class connectionDetails as created by the DatabaseConnector::createConnectionDetails function.
#' @param cdmDatabaseSchema The schema of the CDM instance that holds the DRUG_EXPOSURE, CONCEPT, CONCEPT_ANCESTOR and DRUG_ERA tables. For example, this would be "ohdsi.dbo" when testing on sql server.
#' @param tempEmulationSchema The schema used to emulate temp tables on dialects that do not support them, see SqlRender::translate.
#' @param ... Other arguments passed on to DatabaseConnector::executeSql. (This allows the user to set the path to errorReportFile.)
#' @return Called for its side effect of populating the DRUG_ERA table; returns NULL invisibly.
#' @export
#'
#' @examples
#' \dontrun{
#' buildDrugEra(connectionDetails = connectionDetails,
#' cdmDatabaseSchema = "myCdm")
#'}
buildDrugEra <- function(connectionDetails,
cdmDatabaseSchema,
tempEmulationSchema = getOption("sqlRenderTempEmulationSchema"),
...) {

sqlFile <- system.file("sql", "sql_server", "OMOP_CDM_drug_era.sql",
package = "CommonDataModel", mustWork = TRUE)

sql <- SqlRender::render(SqlRender::readSql(sqlFile), cdm_schema = cdmDatabaseSchema)
sql <- SqlRender::translate(sql,
targetDialect = connectionDetails$dbms,
tempEmulationSchema = tempEmulationSchema)

con <- DatabaseConnector::connect(connectionDetails = connectionDetails)
on.exit(DatabaseConnector::disconnect(con))

DatabaseConnector::executeSql(con, sql = sql, reportOverallTime = FALSE, progressBar = FALSE, ...)

invisible(NULL)
}
411 changes: 206 additions & 205 deletions docs/sqlScripts.html

Large diffs are not rendered by default.

204 changes: 204 additions & 0 deletions inst/sql/sql_server/OMOP_CDM_drug_era.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
-- Code taken from:
-- https://github.com/OHDSI/ETL-CMS/blob/master/SQL/create_CDMv5_drug_era_non_stockpile.sql

if object_id('tempdb..#tmp_de', 'U') is not null drop table #tmp_de;

WITH
ctePreDrugTarget(drug_exposure_id, person_id, ingredient_concept_id, drug_exposure_start_date, days_supply, drug_exposure_end_date) AS
(-- Normalize DRUG_EXPOSURE_END_DATE to either the existing drug exposure end date, or add days supply, or add 1 day to the start date
SELECT
d.drug_exposure_id,
d.person_id,
c.concept_id AS ingredient_concept_id,
d.drug_exposure_start_date AS drug_exposure_start_date,
d.days_supply AS days_supply,
COALESCE(
---NULLIF returns NULL if both values are the same, otherwise it returns the first parameter
NULLIF(drug_exposure_end_date, NULL),
---If drug_exposure_end_date != NULL, return drug_exposure_end_date, otherwise go to next case
NULLIF(dateadd(day,days_supply,drug_exposure_start_date), drug_exposure_start_date),
---If days_supply != NULL or 0, return drug_exposure_start_date + days_supply, otherwise go to next case
dateadd(day,1,drug_exposure_start_date)
---Add 1 day to the drug_exposure_start_date since there is no end_date or INTERVAL for the days_supply
) AS drug_exposure_end_date
FROM @cdm_schema.drug_exposure d
JOIN @cdm_schema.concept_ancestor ca ON ca.descendant_concept_id = d.drug_concept_id
JOIN @cdm_schema.concept c ON ca.ancestor_concept_id = c.concept_id
WHERE c.vocabulary_id = 'RxNorm' ---8 selects RxNorm from the vocabulary_id
AND c.concept_class_id = 'Ingredient'
AND d.drug_concept_id != 0 ---Our unmapped drug_concept_id's are set to 0, so we don't want different drugs wrapped up in the same era
AND coalesce(d.days_supply,0) >= 0 ---We have cases where days_supply is negative, and this can set the end_date before the start_date, which we don't want. So we're just looking over those rows. This is a data-quality issue.
)

, cteSubExposureEndDates (person_id, ingredient_concept_id, end_date) AS --- A preliminary sorting that groups all of the overlapping exposures into one exposure so that we don't double-count non-gap-days
(
SELECT
person_id,
ingredient_concept_id,
event_date AS end_date
FROM
(
SELECT
person_id,
ingredient_concept_id,
event_date,
event_type,
MAX(start_ordinal) OVER (PARTITION BY person_id, ingredient_concept_id
ORDER BY event_date, event_type ROWS unbounded preceding) AS start_ordinal,
-- this pulls the current START down from the prior rows so that the NULLs
-- from the END DATES will contain a value we can compare with
ROW_NUMBER() OVER (PARTITION BY person_id, ingredient_concept_id
ORDER BY event_date, event_type) AS overall_ord
-- this re-numbers the inner UNION so all rows are numbered ordered by the event date
FROM (
-- select the start dates, assigning a row number to each
SELECT
person_id,
ingredient_concept_id,
drug_exposure_start_date AS event_date,
-1 AS event_type,
ROW_NUMBER() OVER (PARTITION BY person_id, ingredient_concept_id
ORDER BY drug_exposure_start_date) AS start_ordinal
FROM ctePreDrugTarget

UNION ALL

SELECT
person_id,
ingredient_concept_id,
drug_exposure_end_date,
1 AS event_type,
NULL
FROM ctePreDrugTarget
) RAWDATA
) e
WHERE (2 * e.start_ordinal) - e.overall_ord = 0
)

, cteDrugExposureEnds (person_id, drug_concept_id, drug_exposure_start_date, drug_sub_exposure_end_date) AS
(
SELECT
dt.person_id,
dt.ingredient_concept_id,
dt.drug_exposure_start_date,
MIN(e.end_date) AS drug_sub_exposure_end_date
FROM ctePreDrugTarget dt
JOIN cteSubExposureEndDates e ON dt.person_id = e.person_id AND dt.ingredient_concept_id = e.ingredient_concept_id AND e.end_date >= dt.drug_exposure_start_date
GROUP BY
dt.drug_exposure_id
, dt.person_id
, dt.ingredient_concept_id
, dt.drug_exposure_start_date
)
--------------------------------------------------------------------------------------------------------------
, cteSubExposures(row_number, person_id, drug_concept_id, drug_sub_exposure_start_date, drug_sub_exposure_end_date, drug_exposure_count) AS
(
SELECT
ROW_NUMBER() OVER (PARTITION BY person_id, drug_concept_id, drug_sub_exposure_end_date ORDER BY person_id),
person_id,
drug_concept_id,
MIN(drug_exposure_start_date) AS drug_sub_exposure_start_date,
drug_sub_exposure_end_date,
COUNT(*) AS drug_exposure_count
FROM cteDrugExposureEnds
GROUP BY person_id, drug_concept_id, drug_sub_exposure_end_date
--ORDER BY person_id, drug_concept_id
)
--------------------------------------------------------------------------------------------------------------
/*Everything above grouped exposures into sub_exposures if there was overlap between exposures.
*So there was no persistence window. Now we can add the persistence window to calculate eras.
*/
--------------------------------------------------------------------------------------------------------------
, cteFinalTarget(row_number, person_id, ingredient_concept_id, drug_sub_exposure_start_date, drug_sub_exposure_end_date, drug_exposure_count, days_exposed) AS
(
SELECT
row_number,
person_id,
drug_concept_id,
drug_sub_exposure_start_date,
drug_sub_exposure_end_date,
drug_exposure_count,
datediff(day,drug_sub_exposure_start_date,drug_sub_exposure_end_date) AS days_exposed
FROM cteSubExposures
)
--------------------------------------------------------------------------------------------------------------
, cteEndDates (person_id, ingredient_concept_id, end_date) AS -- the magic
(
SELECT
person_id,
ingredient_concept_id,
dateadd(day,-30,event_date) AS end_date -- unpad the end date
FROM
(
SELECT
person_id,
ingredient_concept_id,
event_date,
event_type,
MAX(start_ordinal) OVER (PARTITION BY person_id, ingredient_concept_id
ORDER BY event_date, event_type ROWS UNBOUNDED PRECEDING) AS start_ordinal,
-- this pulls the current START down from the prior rows so that the NULLs
-- from the END DATES will contain a value we can compare with
ROW_NUMBER() OVER (PARTITION BY person_id, ingredient_concept_id
ORDER BY event_date, event_type) AS overall_ord
-- this re-numbers the inner UNION so all rows are numbered ordered by the event date
FROM (
-- select the start dates, assigning a row number to each
SELECT
person_id,
ingredient_concept_id,
drug_sub_exposure_start_date AS event_date,
-1 AS event_type,
ROW_NUMBER() OVER (PARTITION BY person_id, ingredient_concept_id
ORDER BY drug_sub_exposure_start_date) AS start_ordinal
FROM cteFinalTarget

UNION ALL

-- pad the end dates by 30 to allow a grace period for overlapping ranges.
SELECT
person_id,
ingredient_concept_id,
dateadd(day,30,drug_sub_exposure_end_date),
1 AS event_type,
NULL
FROM cteFinalTarget
) RAWDATA
) e
WHERE (2 * e.start_ordinal) - e.overall_ord = 0

)
, cteDrugEraEnds (person_id, drug_concept_id, drug_sub_exposure_start_date, drug_era_end_date, drug_exposure_count, days_exposed) AS
(
SELECT
ft.person_id,
ft.ingredient_concept_id,
ft.drug_sub_exposure_start_date,
MIN(e.end_date) AS era_end_date,
drug_exposure_count,
days_exposed
FROM cteFinalTarget ft
JOIN cteEndDates e ON ft.person_id = e.person_id AND ft.ingredient_concept_id = e.ingredient_concept_id AND e.end_date >= ft.drug_sub_exposure_start_date
GROUP BY
ft.person_id
, ft.ingredient_concept_id
, ft.drug_sub_exposure_start_date
, drug_exposure_count
, days_exposed
)
SELECT
row_number()over(order by person_id) drug_era_id,
person_id,
drug_concept_id,
MIN(drug_sub_exposure_start_date) AS drug_era_start_date,
drug_era_end_date,
SUM(drug_exposure_count) AS drug_exposure_count,
datediff(day,MIN(drug_sub_exposure_start_date),drug_era_end_date)-SUM(days_exposed) AS gap_days
INTO #tmp_de
FROM cteDrugEraEnds dee
GROUP BY person_id, drug_concept_id, drug_era_end_date;

INSERT INTO @cdm_schema.drug_era(drug_era_id,person_id, drug_concept_id, drug_era_start_date, drug_era_end_date, drug_exposure_count, gap_days)
SELECT
*
FROM #tmp_de;
43 changes: 43 additions & 0 deletions man/buildDrugEra.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading