Created
April 20, 2015 14:55
-
-
Save chrisknoll/64da3ee06b271763d1be to your computer and use it in GitHub Desktop.
This script builds drug eras from CDM v4 DRUG_EXPOSURE tables and returns a result in the form of the DRUG_ERA table. The logic of the script will roll up drug exposures into their ingredient, and then apply the drug era building logic based on each ingredient concept. The drug exposure gap is set to 30 days.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with cteDrugTarget (DRUG_EXPOSURE_ID, PERSON_ID, DRUG_CONCEPT_ID, DRUG_TYPE_CONCEPT_ID, DRUG_EXPOSURE_START_DATE, DRUG_EXPOSURE_END_DATE, INGREDIENT_CONCEPT_ID) 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, d.DRUG_TYPE_CONCEPT_ID, DRUG_EXPOSURE_START_DATE, | |
COALESCE(DRUG_EXPOSURE_END_DATE, DATEADD(day,DAYS_SUPPLY,DRUG_EXPOSURE_START_DATE), DATEADD(day,1,DRUG_EXPOSURE_START_DATE)) as DRUG_EXPOSURE_END_DATE, | |
c.CONCEPT_ID as INGREDIENT_CONCEPT_ID | |
FROM DRUG_EXPOSURE d | |
join CONCEPT_ANCESTOR ca on ca.DESCENDANT_CONCEPT_ID = d.DRUG_CONCEPT_ID | |
join CONCEPT c on ca.ANCESTOR_CONCEPT_ID = c.CONCEPT_ID | |
where c.VOCABULARY_ID = 8 | |
and c.CONCEPT_CLASS = 'Ingredient' | |
), | |
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_EXPOSURE_START_DATE AS EVENT_DATE, -1 as EVENT_TYPE, ROW_NUMBER() OVER (PARTITION BY PERSON_ID, DRUG_CONCEPT_ID ORDER BY DRUG_EXPOSURE_START_DATE) as START_ORDINAL | |
from cteDrugTarget | |
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_EXPOSURE_END_DATE), 1 as EVENT_TYPE, NULL | |
FROM cteDrugTarget | |
) RAWDATA | |
) E | |
WHERE (2 * E.START_ORDINAL) - E.OVERALL_ORD = 0 | |
), | |
cteDrugExposureEnds (PERSON_ID, DRUG_CONCEPT_ID, DRUG_TYPE_CONCEPT_ID, DRUG_EXPOSURE_START_DATE, DRUG_ERA_END_DATE) as | |
( | |
select | |
d.PERSON_ID, | |
d.INGREDIENT_CONCEPT_ID, | |
d.DRUG_TYPE_CONCEPT_ID, | |
d.DRUG_EXPOSURE_START_DATE, | |
MIN(e.END_DATE) as ERA_END_DATE | |
FROM cteDrugTarget d | |
JOIN cteEndDates e on d.PERSON_ID = e.PERSON_ID and d.INGREDIENT_CONCEPT_ID = e.INGREDIENT_CONCEPT_ID and e.END_DATE >= d.DRUG_EXPOSURE_START_DATE | |
GROUP BY d.DRUG_EXPOSURE_ID, | |
d.PERSON_ID, | |
d.INGREDIENT_CONCEPT_ID, | |
d.DRUG_TYPE_CONCEPT_ID, | |
d.DRUG_EXPOSURE_START_DATE | |
) | |
-- Add INSERT statement here | |
select person_id, drug_concept_id, drug_type_concept_id, min(DRUG_EXPOSURE_START_DATE) as DRUG_ERA_START_DATE, DRUG_ERA_END_DATE, COUNT(*) as DRUG_EXPOSURE_COUNT | |
from cteDrugExposureEnds | |
GROUP BY person_id, drug_concept_id, drug_type_concept_id, DRUG_ERA_END_DATE | |
order by person_id, drug_concept_id |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment