/* This is stata code for defining Severe Constipation using administrative data The algorithm is published in Tonelli et al BMC Med Inform Dec Making 2015;15:31 and based on the work in Sands et al Pharmacoepidemiol Drug Saf 2006;15:47-56 1) Specify the file name for the hospitalization data Define the variable names for the ICD-9 codes as hosp_icd9_code Define the maximum number of ICD-9 diagnosis code position as num_hosp_icd9 Define the variable name for ICD-10 codes as hosp_icd10_code Define the maximum number of ICD-10 diagnosis code position as num_hosp_icd10 Define the variable name of the hospitalization start date as hosp_start_date Define the variable names of the types of hospitalization as type 2) Specify the file name for the physician claims data Define the variable names for the ICD-9 codes as claim_icd9_code Define the maximum number of ICD-9 diagnosis code position as num_claim_icd9 Define the variable name of the physician claims date as claim_start_date 3) Specify the file name for the surgery codes and dates 4) Specify the directory of the output dataset 5) Create a study cohort which includes the unique patient identifier, the study start and end dates (e.g., when a patient turns 18 until they die or out-migrate) */ * File name and variable names for the hospitalizations dataset global data_hosp = "G:\Open Data\ICDC 2013\ICDC Source\Constant files\hosp94_2013.dta" global hosp_icd9_code = "hosp_icd9dx_code" global num_hosp_icd9 = 16 global hosp_icd10_code = "hosp_icd10dx_code" global num_hosp_icd10 = 25 global hosp_start_date = "start_date" global hosp_end_date = "end_date" global type = "diag_type" * File names and variable names for the physician claims dataset global data_claim = "G:\Open Data\ICDC 2013\ICDC Source\claims_94_13.dta" global claim_icd9_code = "hlth_dx_icd9x_code_" global num_claim_icd9 = 3 global claim_start_date = "start_date" global ccpx_code = "hlth_srvc_ccpx_code" * Output dataset directory global out_source = "G:\Projects\Phoebe\MM30codes\kidtran" * File names for surgeries global data_surgery = "G:\Projects\Phoebe\MM30codes\kidtran\surgeries.dta" /* Sample program call ICDmm30_constipation cohort akdnid study_start_date study_end_date 'cohort' is the file name of the study cohort 'id' is the unique patient identifier 'study_start_date' is the study start date 'study_end_date' is the study end date */ version 13.1 capture program drop ICDmm30_constipation program ICDmm30_constipation set more off gettoken cohort 0:0 gettoken id 0:0 gettoken study_start_date 0:0 gettoken study_end_date 0:0 use `cohort', clear duplicates drop sort `id', stable save cohort_constipation, replace * Constipation local ICD9_constipation_in 5601 56030 56039 5609 5640 56983 56989 local ICD10_constipation_in K558 K560 K564 K566 K590 K631 K634 K6381 K6388 K9280 K9288 local ICD9_constipation_ex 152 153 154 158 179 18 1975 1976 2352 2390 555 556 5680 6146 local ICD10_constipation_ex C17 C18 C19 C20 C21 C451 C48 C51 C52 C53 C54 C55 C56 C57 C58 C60 C61 C62 C63 C64 C65 C66 C67 C68 C785 C786 D017 D019 D371 D372 D373 D374 D375 K50 K51 K660 N736 N994 local ICD9_constipation_exif1 5609 local ICD10_constipation_exif1 K566 local ICD9_constipation_exif 78901 78902 78906 local ICD10_constipation_exif R101 * All hospitalization use "${data_hosp}" , clear keep `id' ${hosp_start_date} ${hosp_end_date} ${hosp_icd9_code}1-${hosp_icd9_code}${num_hosp_icd9} ${hosp_icd10_code}1-${hosp_icd10_code}${num_hosp_icd10} /// ${type}1- ${type}16 ${type}17- ${type}${num_hosp_icd10} recast long ${hosp_start_date}, force sort `id', stable merge n:1 `id' using cohort_constipation keep if _merge==3 drop _merge keep if ${hosp_start_date}>=study_start_date & ${hosp_start_date}<=study_end_date local n=${num_hosp_icd9} forvalues nhos9=1(1)`n' { capture replace ${hosp_icd9_code}`nhos9'= subinstr(${hosp_icd9_code}`nhos9', ".", "",.) } local m=${num_hosp_icd10} forvalues nhos10=1(1)`m' { capture replace ${hosp_icd10_code}`nhos10'= subinstr(${hosp_icd10_code}`nhos10', ".", "",.) } local n=${num_hosp_icd9} foreach in in in ex exif exif1{ gen type_`in'="" gen code_`in'="" foreach i in `ICD9_constipation_`in'' { local len=strlen("`i'") forvalues dx=`n'(-1)1 { capture replace code_`in'="`i'" if substr(${hosp_icd9_code}`dx',1,`len') =="`i'" capture replace type_`in'=${type}`dx' if substr(${hosp_icd9_code}`dx',1,`len') =="`i'" } } local m=${num_hosp_icd10} foreach j in `ICD10_constipation_`in'' { local len=strlen("`j'") forvalues dx=1(1)${num_hosp_icd10} { capture replace code_`in'="`j'" if substr(${hosp_icd10_code}`dx',1,`len')== "`j'" capture replace type_`in'=${type}`dx' if substr(${hosp_icd10_code}`dx',1,`len')== "`j'" } } } keep `id' ${hosp_start_date} ${hosp_end_date} type_in- code_exif1 gen source="hosp" keep if code_in!="" | code_ex!="" | code_exif!="" | code_exif1!="" rename ${hosp_start_date} constipation_date rename ${hosp_end_date} constipation_end_date sort `id' constipation_date, stable save "${out_source}\ICD_constipation.dta", replace * All claims use "${data_claim}",clear keep `id' ${claim_start_date} ${claim_icd9_code}1-${claim_icd9_code}${num_claim_icd9} recast long ${claim_start_date}, force sort `id', stable merge n:1 `id' using cohort_constipation keep if _merge==3 drop _merge keep if ${claim_start_date}>=`study_start_date' & ${claim_start_date}<=`study_end_date' local n=${num_claim_icd9} forvalues nclaim9=1(1)`n' { capture replace ${claim_icd9_code}`nclaim9'= subinstr(${claim_icd9_code}`nclaim9', ".", "",.) } local n=${num_claim_icd9} foreach in in in ex exif exif1{ gen code_`in'="" foreach i in `ICD9_constipation_`in'' { local len=strlen("`i'") forvalues dx=`n'(-1)1 { capture replace code_`in'="`i'" if substr(${claim_icd9_code}`dx',1,`len') =="`i'" } } } keep `id' ${claim_start_date} code_in code_ex code_exif code_exif1 rename ${claim_start_date} constipation_date gen source="claim" keep if code_in!="" | code_ex!="" | code_exif!="" | code_exif1!="" * Append hospitalizations with claims append using "${out_source}\ICD_constipation.dta" sort `id' constipation_date constipation_end_date save "${out_source}\ICD_constipation.dta", replace * Exclude some claims and hospitalization met exclusion criteria use "${data_surgery}",clear keep if code_ex!="" | code_exif1!="" keep `id' constipation_date code_ex code_exif code_exif1 drop if code_exif1!="" & code_exif=="" & code_ex=="" drop if code_exif!="" & code_exif1=="" & code_ex=="" keep `id' constipation_date bysort `id' constipation_date: keep if _n==1 rename constipation_date exclude_date joinby `id' using "${out_source}\ICD_constipation.dta" keep if exclude_date== constipation_date |(exclude_date>=constipation_date & /// exclude_date<=constipation_end_date & constipation_end_date<.) keep `id' constipation_date bysort `id' constipation_date: keep if _n==1 sort `id' constipation_date merge `id' constipation_date using "${out_source}\ICD_constipation.dta" drop if _merge==3 keep if code_in!="" sort `id' constipation_date constipation_end_date drop _merge save "${out_source}\ICD_constipation.dta", replace * Drop if surgery on the day of claim or during hospitalizaton use "${data_claim}",clear keep `id' ${claim_start_date} ${ccpx_code} recast long ${claim_start_date}, force sort `id', stable merge n:1 `id' using cohort_constipation keep if _merge==3 drop _merge replace ${ccpx_code}=substr(${ccpx_code},1,4) rename ${ccpx_code} ccpx_code rename ${claim_start_date} ccpx_date sort ccpx_code, stable merge n:1 ccpx_code using surgeries keep if _merge==3 drop _merge duplicates drop akdnid ccpx_date, force drop study_start_date study_end_date sort `id' joinby `id' using "${out_source}\ICD_constipation.dta" keep if ccpx_date>=constipation_date & ccpx_date<=constipation_end_date & constipation_end_date<. keep `id' constipation_date constipation_end_date sort `id' constipation_date constipation_end_date merge `id' constipation_date constipation_end_date using "${out_source}\ICD_constipation.dta" drop if _merge==3 sort `id' source constipation_date, stable bysort `id' source: drop if _N==1 & source=="claim" bysort `id' source: gen claim=1 if constipation_date[_n+1]<=(constipation_date+round(365.25*2,1)) & source=="claim" drop if claim>=. & source=="claim" drop constipation_end_date sort `id' constipation_date, stable gen constipation_end_date=constipation_date+round(365.25*2,1) gen flag=0 bysort `id' : replace flag=1 if constipation_end_date>constipation_date[_n+1] gsort `id' -constipation_date flag bysort `id': replace constipation_end_date=constipation_end_date[_n-1] if flag ==1 sort `id' constipation_date constipation_end_date bysort `id' constipation_end_date: keep if _n==1 keep `id' constipation_date constipation_end_date gen constipation=1 order `id' constipation_date constipation_end_date sort `id' constipation_date constipation_end_date format constipation_end_date %td save "${out_source}\ICD_constipation.dta", replace erase cohort_constipation.dta end