/* This is stata code for defining Chronic Pain 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 Tian et al J Am Med Inform Assoc 2013;0:1-6 1) Specify the file name for the hospitalization data Define the variable names for the ICD-9 codes as hosp_icd9_codeX (X indicating the diagnosis code position) 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_codeX (X indicating the diagnosis code position) 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_codeX (X indicating the diagnosis code position) 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 ACCS data Define the variable names for the ICD-9 codes as accs_icd9_codeX (X indicating the diagnosis code position) Define the maximum number of ICD-9 diagnosis code position as num_accs_icd9 Define the variable names for ICD-10 codes as accs_icd10_codeX (X indicating the diagnosis code position) Define the maximum number of ICD-10 diagnosis code position as num_accs_icd10 Define the variable name of the ACCS start date as accs_start_date 4) Specify the directory of the output dataset 5) Create 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 type = "diag_type" * File name 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" * File name and variable names for the ACCS dataset global data_accs = "G:\Open Data\ICDC 2013\ICDC Source\Constant files\accs97_2013.dta" global accs_icd9_code = "accs_icd9dx_code" global num_accs_icd9 = 6 global accs_icd10_code = "accs_icd10dx_code" global num_accs_icd10 = 10 global accs_start_date = "start_date" * Output dataset directory global out_source = "G:\Projects\Phoebe\MM30codes\kidtran" /* Sample program call ICDmm30_pain 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_pain program ICDmm30_pain 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_pain, replace * Chronic pain local ICD9_pain 30780 30789 3380 3382 3384 71941 71945 71946 71947 71949 7200 7202 7209 7210 7211 7212 7213 7214 7216 7218 7219 722 7230 7231 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 72470 72479 7248 7249 7290 7291 7292 7294 7295 local ICD10_pain F454 M081 M2550 M2551 M2555 M2556 M2557 M432 M433 M434 M435 M436 M45 M461 M463 M464 M469 M47 M480 M481 M488 M489 M508 M509 M51 M531 M532 M533 M538 M539 M54 M608 M609 M633 M790 M791 M792 M796 M797 M961 * All hospitalizations use "${data_hosp}" , clear keep `id' ${hosp_start_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_pain 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', ".", "",.) } gen type="" gen code="" local n=${num_hosp_icd9} foreach i in `ICD9_pain' { local len=strlen("`i'") forvalues dx=`n'(-1)1 { capture replace code="`i'" if substr(${hosp_icd9_code}`dx',1,`len') =="`i'" capture replace type=${type}`dx' if substr(${hosp_icd9_code}`dx',1,`len') =="`i'" } } local m=${num_hosp_icd10} foreach j in `ICD10_pain' { local len=strlen("`j'") forvalues dx=`m'(-1)1 { capture replace code = "`j'" if substr(${hosp_icd10_code}`dx',1,`len') == "`j'" capture replace type=${type}`dx' if substr(${hosp_icd10_code}`dx',1,`len')== "`j'" } } keep if code!="" keep `id' ${hosp_start_date} type code gen source="hosp" keep `id' ${hosp_start_date} type code source rename ${hosp_start_date} pain_date sort `id' pain_date, stable save "${out_source}\ICD_pain.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_pain 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', ".", "",.) } gen code="" local n=${num_claim_icd9} foreach i in `ICD9_pain' { local len=strlen("`i'") forvalues dx=`n'(-1)1 { capture replace code="`i'" if substr(${claim_icd9_code}`dx',1,`len') =="`i'" } } keep if code!="" sort `id' ${claim_start_date}, stable gen source="claim" keep `id' ${claim_start_date} code source rename ${claim_start_date} pain_date * Append hospitalizations with claims append using "${out_source}\ICD_pain.dta" sort `id' pain_date, stable save "${out_source}\ICD_pain.dta", replace * All ACCS use "${data_accs}",clear keep `id' ${accs_start_date} ${accs_icd9_code}1-${accs_icd9_code}${num_accs_icd9} ${accs_icd10_code}1-${accs_icd10_code}${num_accs_icd10} recast long ${accs_start_date}, force sort `id', stable merge n:1 `id' using cohort_pain keep if _merge==3 drop _merge keep if ${accs_start_date}>=`study_start_date' & ${accs_start_date}<=`study_end_date' local n=${num_accs_icd9} forvalues naccs9=1(1)`n' { capture replace ${accs_icd9_code}`naccs9'= subinstr(${accs_icd9_code}`nclaim9', ".", "",.) } local m=${num_accs_icd10} forvalues naccs10=1(1)`m' { capture replace ${accs_icd10_code}`naccs10'= subinstr(${accs_icd10_code}`naccs10', ".", "",.) } gen code="" local n=${num_accs_icd9} local m=${num_accs_icd10} foreach i in `ICD9_pain' { local len=strlen("`i'") forvalues dx=`n'(-1)1 { capture replace code="`i'" if substr(${accs_icd9_code}`dx',1,`len') =="`i'" & code=="" } } foreach i in `ICD10_pain' { local len=strlen("`i'") forvalues dx=`m'(-1)1 { capture replace code="`i'" if substr(${accs_icd10_code}`dx',1,`len') =="`i'" & code=="" } } keep if code!="" sort `id' ${accs_start_date}, stable gen source="accs" keep `id' ${accs_start_date} code source rename ${accs_start_date} pain_date * Append hospitalizations and claims with accs append using "${out_source}\ICD_pain.dta" save "${out_source}\ICD_pain.dta", replace sort `id' pain_date, stable bysort `id': drop if _N==1 keep `id' pain_date rename pain_date index_date sort `id' joinby `id' using "${out_source}\ICD_pain.dta" gen temp=1 if pain_date>=index_date+30 keep if temp==1 sort `id' index_date pain_date bysort `id' index_date: keep if _n==_N drop pain_date rename index_date pain_date * Any two or more hospitalization or claims or accs within 2 years sort `id' pain_date, stable gen pain_end_date=pain_date+round(365.25*2,1) gen flag=0 bysort `id': replace flag=1 if pain_end_date>pain_date[_n+1] gsort `id' -pain_date flag bysort `id': replace pain_end_date=pain_end_date[_n-1] if flag ==1 sort `id' pain_date pain_end_date bysort `id' pain_end_date: keep if _n==1 keep `id' pain_date pain_end_date gen pain=1 order `id' pain_date pain_end_date pain sort `id' pain_date pain_end_date save "${out_source}\ICD_pain.dta", replace erase cohort_pain.dta end