**************************************************************************;
**************************************************************************;
** SAS CODE TO CALCULATE AVOIDABLE, PREVENTABLE AND TREATABLE MORTALITY **;
** Code written by Jeremy Herring, Epidemiologist Lead, Analytic        **;
** Services Unit, Public Health Ontario - jeremy.herring@oahpp.ca       **;
** April 4, 2014                                                        **;
**                                                                      **;
** NOTE: This code assumes you have a row level dataset with the full   **;
** ICD-10 code for primary cause of death (named 'ICD_CODE'), sex as M  **;
** and F (named 'SEX') and age in years (named 'AGE')                   **;
**************************************************************************;
**************************************************************************;


************************************************;
* Define Preventable Deaths using ICD-10 Codes *;
************************************************;
data PREVENTABLE_DEATHS; length CAUSE $20 PREV_DEATH TREAT_DEATH $1. WEIGHT PYLL 8; set input.ON_MORT_2009; 

* Cases where cause of death is attributed both as preventable and treatable are weighted 0.5;
format WEIGHT PYLL 8.1; WEIGHT = 0; PYLL = 0; PREV_DEATH = 'N'; TREAT_DEATH = 'N'; 

* Infections;
* Preventable;
if icd_code in: ('A00','A01','A02','A03','A04','A05','A06','A07','A08','A09','A35','A36','A37','A39','A403','A413','A492','A80',
'B01','B05','B06','J09','J10','J11','J13','J14','G000','G001','A50','A51','A52','A53','A54','A55','A56','A57',
'A58','A59','A60','A63','A64','B15','B16','B17','B18','B19','B20','B21','B22','B23','B24')
then do; CAUSE = 'INFECTIONS'; PREV_DEATH = 'Y'; WEIGHT = 1; end;

* Neoplasms;
* Preventable;
if icd_code in: ('C00','C01','C02','C03','C04','C05','C06','C07','C08','C09','C10','C11','C12','C13','C14','C15',
'C16','C22','C33','C34','C43','C44')
then do; CAUSE = 'NEOPLASMS'; PREV_DEATH = 'Y'; WEIGHT = 1; end;

* Diseases of the Circulatory System;
* Preventable;
if icd_code in: ('I01','I02','I05','I06','I07','I08','I09','I71','I26','I80','I829')
then do; CAUSE = 'CIRCULATORY'; PREV_DEATH = 'Y'; WEIGHT = 1; end;
if icd_code in: ('I60','I61','I62','I63','I64','I67','I69','I20','I21','I22','I23','I24','I25','I70','I739')
then do; CAUSE = 'CIRCULATORY'; PREV_DEATH = 'Y'; WEIGHT = 0.5; end;

* Diseases of the Respiratory System;
* Preventable;
if icd_code in: ('J40','J41','J42','J43','J44','C45','J60','J61','J62','J63','J64','J66','J67','J68','J69','J70','J82','J92')
then do; CAUSE = 'RESPIRATORY'; PREV_DEATH = 'Y'; WEIGHT = 1; end;

* Diseases of the Digestive System;
* Preventable;
if icd_code in: ('K73','K740','K741','K742','K746') 
then do; CAUSE = 'DIGESTIVE'; PREV_DEATH = 'Y'; WEIGHT = 1; end;

* Diseases of the Genitourinary System
* Preventable -- no preventable causes;

* Infant and Maternal Causes;
* Preventable;
if icd_code in: ('A33') 
then do; CAUSE = 'INFANTMATERNAL'; PREV_DEATH = 'Y'; WEIGHT = 1; end;

* Injuries;
* Preventable;
if icd_code in: ('V','W','X','Y0','Y1','Y2','Y30','Y31','Y32','Y33','Y34','Y870','Y871') 
then do; CAUSE = 'INJURIES'; PREV_DEATH = 'Y'; WEIGHT = 1; end;

* Alcohol and Drug Use Disorders;
* Preventable;
if icd_code in: ('F10','G312','G621','I426','K292','K70','K852','K860','F11','F12','F13','F14','F15','F16','F18','F19') 
then do; CAUSE = 'ALCOHOLDRUG'; PREV_DEATH = 'Y'; WEIGHT = 1; end;

* Nutritional, Endocrine and Metabolic Disorders;
* Preventable;
if icd_code in: ('D50','D51','D52','D53') 
then do; CAUSE = 'NUTRITIONAL'; PREV_DEATH = 'Y'; WEIGHT = 1; end;
if icd_code in: ('E10','E11','E12','E13','E14') 
then do; CAUSE = 'NUTRITIONAL'; PREV_DEATH = 'Y'; WEIGHT = 0.5; end;

* Neurological Disorders;
* Preventable -- no preventable causes;

* Disorders of Musculoskeletal System;
* Preventable -- no preventable causes;

* Adverse Effects of Medical and Surgical Care;
* Preventable;
if icd_code in: ('Y4','Y5','Y6','Y7','Y80','Y81','Y82','Y83','Y84') 
then do; CAUSE = 'ADVERSEMEDICAL'; PREV_DEATH = 'Y'; WEIGHT = 1; end;

* Calculate PYLL;
PYLL = (75 - age) * weight;

* Remove Non-Preventable Deaths and those 75 and older;
if PREV_DEATH = 'N' then delete; if age ge 75 then delete;
run;




**********************************************;
* Define Treatable Deaths using ICD-10 Codes *;
**********************************************;
data TREATABLE_DEATHS; length CAUSE $20 PREV_DEATH TREAT_DEATH $1. WEIGHT PYLL 8; set input.ON_MORT_2009; 

* Cases where cause of death is attributed both as preventable and treatable are weighted 0.5;
format WEIGHT PYLL 8.1; WEIGHT = 0; PYLL = 0; TREAT_DEATH = 'N'; PREV_DEATH = 'N';

* Infections;
* Treatable;
if icd_code in: ('A16','A17','A18','A19','B90','J65','A38','A481','A491','A400','A401','A402','A404','A405','A406','A407','A408','A409',
'A410','A411','A412','A414','A415','A416','A417','A418','A419','B50','B51','B52','B53','B54','G002','G003','G008','G009','A46','L03',
'J12','J15','J16','J18')
then do; CAUSE = 'INFECTIONS'; TREAT_DEATH = 'Y'; WEIGHT = 1; end;

* Neoplasms;
* Treatable;
if icd_code in: ('C18','C19','C20','C21','C53','C54','C55','C62','C67','C73','C81','D1','D2','D30','D31','D32','D33','D34','D35','D36')
or (icd_code in: ('C50') and SEX = 'F') or (icd_code in: ('C910','C911','C921') and AGE < 45) 
then do; CAUSE = 'NEOPLASMS'; TREAT_DEATH = 'Y'; WEIGHT = 1; end;

* Diseases of the Circulatory System;
* Treatable;
if icd_code in: ('I10','I11','I12','I13','I15')
then do; CAUSE = 'CIRCULATORY'; TREAT_DEATH = 'Y'; WEIGHT = 1; end;
if icd_code in: ('I60','I61','I62','I63','I64','I67','I69','I20','I21','I22','I23','I24','I25','I70','I739')
then do; CAUSE = 'CIRCULATORY'; TREAT_DEATH = 'Y'; WEIGHT = 0.5; end;

* Diseases of the Respiratory System;
* Treatable;
if icd_code in: ('J45','J47','J20','J22','J00','J01','J02','J03','J04','J05','J06','J3','J80','J81','J85','J86','J90','J93','J94','J98')
then do; CAUSE = 'RESPIRATORY'; TREAT_DEATH = 'Y'; WEIGHT = 1; end;

* Diseases of the Digestive System;
* Treatable;
if icd_code in: ('K25','K26','K27','K28','K35','K36','K37','K38','K40','K41','K42','K43','K44','K45','K46','K80','K81','K82','K83',
'K850','K851','K853','K858','K859','K861','K862','K863','K868','K869') 
then do; CAUSE = 'DIGESTIVE'; TREAT_DEATH = 'Y'; WEIGHT = 1; end;

* Diseases of the Genitourinary System
* Treatable;
if icd_code in: ('N00','N01','N02','N03','N04','N05','N06','N07','N17','N18','N19','N13','N20','N21','N23','N35','N40','N341',
'N70','N71','N72','N73','N750','N751','N764','N766','N25') 
then do; CAUSE = 'GENITOURINARY'; TREAT_DEATH = 'Y'; WEIGHT = 1; end;

* Infant and Maternal Causes;
* Treatable;
if icd_code in: ('H311','P0','P1','P2','P3','P4','P5','P6','P7','P8','P90','P91','P92','P93','P94','P95','P96','Q','O') 
then do; CAUSE = 'INFANTMATERNAL'; TREAT_DEATH = 'Y'; WEIGHT = 1; end;

* Injuries;
* Treatable - no treatable causes;

* Alcohol and Drug Use Disorders;
* Treatable - no treatable causes;

* Nutritional, Endocrine and Metabolic Disorders;
* Treatable;
if icd_code in: ('E00','E01','E02','E03','E04','E05','E06','E07','E24','E25','E27','E740','E742') 
then do; CAUSE = 'NUTRITIONAL'; TREAT_DEATH = 'Y'; WEIGHT = 1; end;
if icd_code in: ('E10','E11','E12','E13','E14') 
then do; CAUSE = 'NUTRITIONAL'; TREAT_DEATH = 'Y'; WEIGHT = 0.5; end;

* Neurological Disorders;
* Treatable;
if icd_code in: ('G40','G41') 
then do; CAUSE = 'NEUROLOGICAL'; TREAT_DEATH = 'Y'; WEIGHT = 1; end;


* Disorders of Musculoskeletal System;
* Treatable;
if icd_code in: ('M86') 
then do; CAUSE = 'MUSCULOSKELETAL'; TREAT_DEATH = 'Y'; WEIGHT = 1; end;


* Adverse Effects of Medical and Surgical Care;
* Treatable - no treatable causes;

* Calculate PYLL;
PYLL = (75 - age) * weight;

* Remove Non-Treatable Deaths and those 75 and older;
if TREAT_DEATH = 'N' then delete; if age ge 75 then delete;
run;




******************************************************;
* Define Avoidable Deaths as Preventable + Treatable *;
******************************************************;
* Note: this dataset contains duplicate records for those deaths attributed 
  both as preventable and treatable, weighted as 0.5 of a death;
data AVOIDABLE_DEATHS; 
set PREVENTABLE_DEATHS TREATABLE_DEATHS; run;





********************************************************************************;
* Calculate Deaths and PYLL for Avoidable, Preventable and Treatable Mortality *;
********************************************************************************;
* Calculate number of preventable deaths by PHU;
proc freq data=AVOIDABLE_DEATHS noprint; tables IH_PHU / out=phu_avoid_deaths; 
where IH_PHU not in ('OUT OF PROVINCE', 'UNKNOWN'); weight WEIGHT; run;
proc freq data=AVOIDABLE_DEATHS noprint; tables IH_PHU / out=phu_prev_deaths; 
where IH_PHU not in ('OUT OF PROVINCE', 'UNKNOWN') and PREV_DEATH = 'Y'; weight WEIGHT; run;
proc freq data=AVOIDABLE_DEATHS noprint; tables IH_PHU / out=phu_treat_deaths; 
where IH_PHU not in ('OUT OF PROVINCE', 'UNKNOWN') and TREAT_DEATH = 'Y'; weight WEIGHT; run;

* Calculate number of PYLL deaths by PHU;
proc freq data=AVOIDABLE_DEATHS noprint; tables IH_PHU / out=phu_avoid_pyll; 
where IH_PHU not in ('OUT OF PROVINCE', 'UNKNOWN'); weight PYLL; run;
proc freq data=AVOIDABLE_DEATHS noprint; tables IH_PHU / out=phu_prev_pyll; 
where IH_PHU not in ('OUT OF PROVINCE', 'UNKNOWN') and PREV_DEATH = 'Y'; weight PYLL; run;
proc freq data=AVOIDABLE_DEATHS noprint; tables IH_PHU / out=phu_treat_pyll; 
where IH_PHU not in ('OUT OF PROVINCE', 'UNKNOWN') and TREAT_DEATH = 'Y'; weight PYLL; run;


* Calculate number of preventable deaths by PHU and CAUSE;
proc freq data=AVOIDABLE_DEATHS noprint; tables IH_PHU*CAUSE / out=cause_avoid_deaths; 
where IH_PHU not in ('OUT OF PROVINCE', 'UNKNOWN'); weight WEIGHT; run;
proc freq data=AVOIDABLE_DEATHS noprint; tables IH_PHU*CAUSE / out=cause_prev_deaths; 
where IH_PHU not in ('OUT OF PROVINCE', 'UNKNOWN') and PREV_DEATH = 'Y'; weight WEIGHT; run;
proc freq data=AVOIDABLE_DEATHS noprint; tables IH_PHU*CAUSE / out=cause_treat_deaths; 
where IH_PHU not in ('OUT OF PROVINCE', 'UNKNOWN') and TREAT_DEATH = 'Y'; weight WEIGHT; run;

* Calculate number of PYLL deaths by PHU and CAUSE;
proc freq data=AVOIDABLE_DEATHS noprint; tables IH_PHU*CAUSE / out=cause_avoid_pyll; 
where IH_PHU not in ('OUT OF PROVINCE', 'UNKNOWN'); weight PYLL; run;
proc freq data=AVOIDABLE_DEATHS noprint; tables IH_PHU*CAUSE / out=cause_prev_pyll; 
where IH_PHU not in ('OUT OF PROVINCE', 'UNKNOWN') and PREV_DEATH = 'Y'; weight PYLL; run;
proc freq data=AVOIDABLE_DEATHS noprint; tables IH_PHU*CAUSE / out=cause_treat_pyll; 
where IH_PHU not in ('OUT OF PROVINCE', 'UNKNOWN') and TREAT_DEATH = 'Y'; weight PYLL; run;