/*
		+---------------------------------------------------------------------------------------------------------------------------------------------+ 
		| Name:			Prepare HAC Scores.sas																					  					  |		
		|																																			  |		
		| Description:	Prepares flags for hospital acquired complications risk factors. Currently uses ICD-10-AM 12th edition codes.		          |
        |                                                                                                                                             |                              
		|																																			  |
		| Version:		1.1.0																														  |
		|																																			  |		
		| Authors:		Jada Ching									                                                                              	  |
		|				Matthew Hughes																												  |
		| 				Amanda Lieu																													  |
		| 				Lily Baweja																													  |
		|				Independent Hospital Pricing Authority																						  |	
		|																																			  |	
		| Date:			Oct 2025																													  |	
		+---------------------------------------------------------------------------------------------------------------------------------------------+
*/

/******************************************************************************************************
	This program sets up the variables required to calculate a complexity score
	These are:
		-	Sex category
		-	Age (5 year age bands)
		-	Charlson Comorbidity Conditions
		-	drg10/11/12 type (Medical or Intervention)
		- 	MDC	
		-	Admission Transfer flag (0 or 1)
		-	ICU hours flag (0 or 1)
		-	Emergency election status terms

******************************************************************************************************/

%let CHARLSON_DIAGS = (	'I21','I22','I50',
						'I110', 'I130', 'I132',  'I70', 'I71', 'I73','I60',
						'I61','I62','I63','I65','I66',  
						'I64', 'I670','I671','I672',
						'I673', 'I674','I675','I676','I677','I678',
						'I679', 'I680', 'I681','I682','I688','I69',
						'F00','F01', 'F03', 'U791', 'J40','J41',
						'J42','J44','J43','J45','J46','J47',
						'J67','J60','J61','J62','J63','J66',
						'J64','J65',  'M05', 'M06', 'M30', 'M31', 'M32','M33',  
						'M34', 'M35', 'M36', 
						'K25','K26','K27','K28', 'K700', 'K701', 'K702',
						'K703', 'K709', 'K710', 'K712', 'K713', 'K714', 'K715','K716',
						'K717', 'K718', 'K719', 'K720', 'K73','K74', 							
						'K75', 'K760', 'K761', 'K762', 'K763', 'K764', 
						'K768', 'K769', 'B18','E100', 'E101', 'E102','E103' ,'E104','E105' ,'E106', 'E107', 'E110',
						'E111' ,'E112' ,'E113' ,'E114' ,'E115' ,'E116', 'E117', 'E130',
						'E131' ,'E132' ,'E133' ,'E134' ,'E135' ,'E136', 'E137', 'E140',
						'E141' , 'E142'  ,'E143' ,'E144' ,'E145' , 'E146', 'E147','E149', 'G81',
						'G820','G821','G822', 'N03','N052',
						'N053','N054','N055','N056','N072',
						'N073','N074','N01',   													
						'N183', 'N184', 'N185', 'N189', 'N19',
						'N25', 'I120', 'I131', 'Z490', 'Z491', 'Z492', 
						'C0','C1','C2','C3','C40',
						'C41','C43','C45','C46','C47','C48',
						'C49','C5','C6','C70','C71','C72',
						'C73','C74','C75','C76','C80','C81',
						'C82','C83','C84','C85', 'C86', 'C880', 'C882','C883',
						'C884','C887', 'C889','C900','C901','C902', 'C903',
						'C911','C913', 'C914', 'C915', 'C916', 'C917', 'C918', 'C919',
						'C92','C930', 'C931', 'C933', 'C937', 'C939',
						'C940','C942','C943','C944','C946', 'C947',
						'C950', 'C951', 'C957', 'C959', 'C77', 'C78','C79',
						'K704', 'K711', 'K721', 'K729', 'K765', 'K766',
						'K767', 'Z944', 'B20','B21','B22',
						'B23','B24', 'R75', 'Z21',
						'D46', 'Z3551','Z356', 'O328','O640','O68');

%let CHARLSON_DIAGS_LITERAL = ('U822','U831', 'U832', 'U833', 'U834', 'U861', 'U863',
								'E108', 'E109', 'E118', 'E119', 'E138', 'E139', 'E148', 'U871','U843',
								'D45' ); 
proc import datafile="&LOCATION.\u_ddx_13map.csv"
out=u_ddx_13map dbms=csv replace; getnames=yes;guessingrows=MAX;run;								
proc import datafile="&LOCATION.\u_ddx_12map.csv"
out=u_ddx_12map dbms=csv replace; getnames=yes;guessingrows=MAX;run;
proc import datafile="&LOCATION.\u_ddx_11map.csv"
out=u_ddx_11map dbms=csv replace; getnames=yes;guessingrows=MAX;run;
proc import datafile="&LOCATION.\u_ddx_10map.csv"
out=u_ddx_10map dbms=csv replace; getnames=yes;guessingrows=MAX;run;


data drg&drg_version._MASTERLIST_RA (index=(_DRG&drg_version.)); 
	set CALCREF.DRG&drg_version._MASTERLIST (rename=(	DRG&drg_version. = _DRG&drg_version. 
														mdc&drg_version. = an&drg_version.0mdc_ra));
	if _DRG&drg_version. in ("801A", "801B", "801C") then an&drg_version.0mdc_ra="23";
run;
%global achi;
%macro icd;
	%if &icd10am_edition.*1 = 7 or &icd10am_edition.*1 = 8 or &icd10am_edition.*1 = 9 %then
		%let achi = "9047002"	,	"9047004"	,	"9046800"	,	"9046801"	,	"9046802"
				,	"9046803"	,	"9046804"	,	"9046805"	,	"9046900"	,	"9046901";
	%else %if &icd10am_edition.*1 = 10 or &icd10am_edition.*1 = 11 or &icd10am_edition.*1 = 12 or &icd10am_edition.*1 = 13 %then
		%let achi = "9047002"	,	"9047004"	,	"9046800"	,	"9046801"	,	"9046802"
				,	"9046803"	,	"9046804"	,	"9046805"	,	"9046900"	,	"9046901"	,	"9046806";
%mend icd;
%icd;

data &output. (drop = _drg&drg_version.);
	set &input. (rename=(&sex. = sex_cat));
	/* MH - NEP26 Extra Rfs - Add a hash join to the extra chronic condition codes. */
	if _n_=1 then do;	
		dcl hash cc(dataset: "u_ddx_&ICD10AM_Edition.map ");
		cc.definekey('icd_code');
		cc.definedata('u_group');
		cc.definedone();
		
		dcl hash su(dataset: "u_ddx_&ICD10AM_Edition.map ");
		su.definekey('u_code');						 
		su.definedata('u_group');						   
		su.definedone();

		dcl hash p_nep26 (dataset: "CALCREF.nep26_aa_price_weights(rename=(drg = &DRG.))");
		p_nep26.definekey("&DRG.");
		p_nep26.definedata("drg_inlier_lb");
		p_nep26.definedone();
	end;

	sex=sex_cat;
	if sex_cat in ('3','9') then do;
		sex_cat = 1;
	end;

	/*Create drg type*/
	_drg&drg_version. = &drg.;
	set drg&drg_version._MASTERLIST_RA (keep = _drg&drg_version.
									drg&drg_version._type 
									an&drg_version.0mdc_ra) 
							key = _drg&drg_version. / unique;
	if _error_ ne 0 then do;
		_error_ = 0; 
		drg&drg_version._type = "";
		an&drg_version.0mdc_ra = "";
	end;

	/*Age group*/
	agegroup = floor(min(_pat_age_years,99)/5);
	if _pat_age_years = . then agegroup = .;

	/*Age group 15*/
	if _pat_age_years <= 15 then 		age_15g = 1;
	else if _pat_age_years >= 35 then 	age_15g = 3;
	else 								age_15g = 2;
	
	/*Flag ICU hours*/
	flag_ICUHours = (&ICU_HOURS. gt 0);

	/*Flag admission transfer*/
	flag_AdmTransfer = (&admmode. eq 1);	*SET TO 1 IF THE ADMISSION WAS A TRANSFER FROM ANOTHER HOSPITAL;

	/*set up arrays*/
	array SRGARRAY &PROC_PREFIX:;
	array ONSETARRAY &ONSET_PREFIX:;
	array ddxarray &DIAG_PREFIX:;

	MAXDDXVAR = dim(DDXARRAY) - cmiss(of  &DIAG_PREFIX:);
	MAXSRGVAR = max(1,dim(SRGARRAY) - cmiss(of  &PROC_PREFIX:));

	do i = 1 to MAXSRGVAR;
		if missing(SRGARRAY[i]) = 1 then MAXSRGVAR = dim(SRGARRAY); 
	end;

			flag_instrument_use = 0;
			flag_primiparity = 0;
			flag_PPOP = 0;
			flag_foetal_distress = 0;

	do i = 1 to MAXSRGVAR;
		srg = SRGARRAY[i];
	
*HAC15 risk flag;
	if srg in: ( &achi. )
			then flag_instrument_use = 1;
	end;

	do i=1 to MAXDDXVAR;
		if missing(DDXARRAY[i]) = 1 then MAXDDXVAR = dim(DDXARRAY); 
	end;
			
	mental_health=0;
	homeless=0;
	post_transplant=0;
	pacemaker=0;
	ventilator=0;
	asthma=0;
	obesity=0;
	malnutrition=0;
	drug_use=0;
	parkinson_disease = 0;
	dystocia = 0 ;
	cc_Acute_myocardial_function = 0;
	cc_Congestive_heart_failure = 0;
	cc_Peripheral_vascular_disease = 0;
	cc_Cerebral_vascular_accident = 0;
	_cc_Dementia = 0;
	cc_Pulmonary_disease = 0;
	cc_Connective_tissue_disorder = 0;
	cc_Peptic_ulcer = 0;
	cc_Liver_disease = 0;
	cc_Diabetes = 0;
	cc_Diabetes_complications = 0;
	cc_Paraplegia = 0;
	cc_Renal_disease = 0;
	cc_Cancer = 0;
	cc_Metastatic_cancer = 0;
	cc_Severe_liver_disease = 0;
	cc_HIV = 0;
	flag_primiparity = 0;
	flag_PPOP = 0;
	flag_foetal_distress = 0;

	do i = 1 to MAXDDXVAR;

		onset = ONSETARRAY[i]; 
		ddx = DDXARRAY[i];

		%if &ICD10AM_EDITION.*1 <= 12 %then %do;
			if substr(ddx,1,1)="F" or ddx in ("R4581") or ddx in: ('U79') then 											mental_health = 1;
		%end;
		%else %do;
			if substr(ddx,1,1)="F" or ddx in ("R4581", "R4582") or ddx in: ('U79') then 								mental_health = 1;
		%end;
		/* The in: identifier is the equivalent to checking the initial DDX substring is equal to the in set. */
		/* Handle changing codes for ICD-10-AM 13th edition. */
		%if &ICD10AM_EDITION.*1 <= 12 %then %do;
			if DDX in: ('F10', 'F11', 'F12', 'F13', 'F14', 'F15', 'F16', 'F17', 'F18', 'F19', 'Z642', 'Z722') then 		drug_use = 1;
		%end;
		%else %do;
			if DDX in: ('F10', 'F11', 'F12', 'F13', 'F14', 'F15', 'F16', 'F17', 'F18', 'F19', 'Z72', 'Z722') then 		drug_use = 1;
		%end;
		if ddx in: ("Z590") then																						homeless = 1;
		if DDX in: ("Z94") then																							post_transplant = 1;
		if DDX in ("Z950") then 																						pacemaker = 1;
		if ddx in ("Z991") then																							ventilator = 1;
		if DDX in: ("J45","J46") or DDX in ("U833") then																asthma = 1;
		
		/* MH - NEP25 - Updated to search for codes related to E661 and E662, since these became more granular in ICD-10-AM 12th edition. */
		if ddx in ("E6690","E6691","E6692","E6693") or ddx in: ("E661", "E662") then 									obesity = 1;
		if ddx in ("E40","E41","E42","E43","E440","E441","E45","E46") then												malnutrition = 1;
		
		/* New additions to standalone categories. */
		if DDX in: ("G21", "G22") then 																					parkinson_disease = 1;
		if DDX in ("P031", "O660") then 																				dystocia = 1;

	 		if DDX in: 		('I21'  ,'I22'  )																											then cc_Acute_myocardial_function = 1;
			else if DDX in: 	('I50', 'I110', 'I130', 'I132')
				or DDX in 		('U822')																												then cc_Congestive_heart_failure = 1;
			else if DDX in: ('I70', 'I71', 'I73'  )																										then cc_Peripheral_vascular_disease = 1;
			else if DDX in: ('I60'  ,'I61'  ,'I62'  ,'I63'  ,'I65'  ,'I66'  , 
							'I64'  ,'I670' ,'I671' ,'I672', 'I673' ,'I674' ,'I675' ,'I676' ,'I677' ,'I678' ,
							 'I679', 'I680' ,'I681' ,'I682' ,'I688' ,'I69'  )																			then cc_Cerebral_vascular_accident = 1;
			else if DDX in: ('F00'  ,'F01'  , 'F03', 'U791' )																							then _cc_Dementia = 1;
			else if DDX in: 	('J40'  ,'J41'  ,'J42'  ,'J44'  ,'J43'  ,'J45'  ,'J46'  ,'J47'  ,'J67'  ,'J60'  ,'J61'  ,
							 'J62'  ,'J63'  ,'J66'  ,'J64'  ,'J65')
			or 	DDX in 		('U831', 'U832', 'U833', 'U834'  )																							then cc_Pulmonary_disease = 1;
			else if DDX in: 	('M30', 'M31', 'M32', 'M33', 'M34', 'M35', 'M05', 'M06', 'M36')
			or 	DDX in 		('U861', 'U863')																											then cc_Connective_tissue_disorder = 1;
			else if DDX in: ('K25'  ,'K26'  ,'K27'  ,'K28'  )																							then cc_Peptic_ulcer = 1;
			else if DDX in: 	('K700', 'K701', 'K702' ,'K703', 'K709', 'K71', 'K720', 'K73', 'K74', 'K75', 'K760', 'K761',
								'K762', 'K763', 'K764', 'K768', 'K769', 'B18')				
				and	DDX not in ('K711')  																												then cc_Liver_disease = 1;
			
			else if DDX in	 	('E108', 'E109', 'E118', 'E119', 'E138', 'E139', 'E148') or DDX in: ('E149')											then cc_Diabetes = 1;
			else if DDX in: ('E100', 'E101', 'E102','E103' ,'E104','E105' ,'E106', 'E107', 'E110',
							'E111' ,'E112' ,'E113' ,'E114' ,'E115' ,'E116', 'E117', 'E130',
							'E131' ,'E132' ,'E133' ,'E134' ,'E135' ,'E136', 'E137', 'E140',
							'E141' , 'E142'  ,'E143' ,'E144' ,'E145' , 'E146', 'E147')																	then cc_Diabetes_complications = 1; /*20231031 - MH - NEP24 66 SQ ARDRG disaggregate  risk flags - Set risk score weight to 1*/
			else if DDX in: ('G81'  , 'G820' ,'G821' ,'G822' )																							then cc_Paraplegia = 1; /*20231031 - MH - NEP24 66 SQ ARDRG disaggregate  risk flags - Set risk score weight to 1*/
			else if DDX in: ('N03'  ,'N052' ,'N053' ,'N054' ,'N055' ,'N056' ,'N072' ,'N073' ,'N074' ,'N01'  ,
							'N183', 'N184', 'N185', 'N189'
							 'N19'  ,'N25', 'I120', 'I131', 'Z490', 'Z491', 'Z492'  )		
							or DDX in  	('U871') 																										then cc_Renal_disease = 1; /*20231031 - MH - NEP24 66 SQ ARDRG disaggregate  risk flags - Set risk score weight to 1*/
			else if DDX in: ('C0'   ,'C1'   ,'C2'   ,'C3'   ,'C40'  ,'C41'  ,'C43'  ,'C45'  ,'C46'  ,'C47'  ,'C48'  ,
							 'C49'  ,'C5'   ,'C6'   ,'C70'  ,'C71'  ,'C72'  ,'C73'  ,'C74'  ,'C75'  ,'C76'  ,'C80'  ,
							 'C81'  ,'C82'  ,'C83'  ,'C84'  ,'C85', 'C86', 'C880', 'C882'  ,'C883', 'C884' ,'C887' ,'C889' ,'C900' ,'C901'  , 'C902',
							 'C903', 'C911', 'C913', 'C914', 'C915', 'C916', 'C917', 'C918', 'C919', 'C92', 
							 'C930', 'C931', 'C933', 'C937', 'C939', 'C940' ,'C942' , 'C943', 'C944', 'C946',
							 'C947', 'C950', 'C951', 'C957', 'C959','D46'  )																					
					or DDX in ('D45') 																														then cc_Cancer = 1; /*20231031 - MH - NEP24 66 SQ ARDRG disaggregate  risk flags - Set risk score weight to 1*/
			else if DDX in: ('C77'  ,'C78'  ,'C79'	)																									then cc_Metastatic_cancer = 1; /*20231031 - MH - NEP24 66 SQ ARDRG disaggregate  risk flags - Set risk score weight to 1*/
			else if DDX in: 	('K704','K711','K721','K729' ,'K765','K766' ,'K767' , 'Z944' )
			or	DDX in 		('U843')																													then cc_Severe_liver_disease = 1; /*20231031 - MH - NEP24 66 SQ ARDRG disaggregate  risk flags - Set risk score weight to 1*/
			else if DDX in: ('B20'  ,'B21'  ,'B22'  ,'B23'  ,'B24', 'R75', 'Z21'  )																		then cc_HIV = 1; /*20231031 - MH - NEP24 66 SQ ARDRG disaggregate  risk flags - Set risk score weight to 1*/
	*More HAC15 risk factors;	
			else if DDX in: ('Z3551','Z356' )																											then flag_primiparity = 1;
			else if DDX in: ('O328'	,'O640'	)  																											then flag_PPOP = 1;
			else if DDX in: ('O68'	)																													then flag_foetal_distress = 1;
	end;

	array charlson{*} 	cc_Acute_myocardial_function
						cc_Congestive_heart_failure
						cc_Peripheral_vascular_disease
						cc_Cerebral_vascular_accident
						cc_Dementia
						cc_Pulmonary_disease
						cc_Connective_tissue_disorder
						cc_Peptic_ulcer
						cc_Liver_disease
						cc_Diabetes
						cc_Diabetes_complications
						cc_Paraplegia
						cc_Renal_disease
						cc_Cancer
						cc_Metastatic_cancer
						cc_Severe_liver_disease
						cc_HIV;

	charlson_score = 0;
	do k=1 to 17;
		charlson[k] = sum(0, charlson[k]);
		charlson_score = sum(charlson_score, charlson[k]);
	end;
	charlson_score = min(charlson_score,16);
	drop k i; 
	
	length 	cc_Obesity
			cc_Cystic_fibrosis
			cc_Dementia
			cc_Schizophrenia
			cc_Depression
			cc_Disorder_of_intellectual
			cc_Parkinsons_disease
			cc_Multiple_sclerosis
			cc_Epilepsy
			cc_Cerebral_palsy
			cc_Tetplg_prplg_dplg_hmplg_dt
			cc_Ischaemic_heart_disease
			cc_Chronic_heart_failure
			cc_Hypertension
			cc_Emphysema_without_COPD
			cc_COPD
			cc_Asthma_without_COPD
			cc_Bronchiectasis_without_CF
			cc_Chronic_respiratory_failure
			cc_Crohns_disease
			cc_Ulcerative_colitis
			cc_Chronic_liver_failure
			cc_Rheumatoid_arthritis
			cc_Arthritis_and_osteoarthritis
			cc_Systemic_lupus_erythematosus
			cc_Osteoporosis
			cc_Chronic_kidney_disease
			cc_Spina_bifida
			cc_Downs_syndrome
			low_los_flag
			u_group 3.
			an&drg_version.0mdc_ra $2.;					   
		
	array chronic_conditions cc_Obesity
		cc_Cystic_fibrosis
		cc_Dementia
		cc_Schizophrenia
		cc_Depression
		cc_Disorder_of_intellectual
		cc_Parkinsons_disease
		cc_Multiple_sclerosis
		cc_Epilepsy
		cc_Cerebral_palsy
		cc_Tetplg_prplg_dplg_hmplg_dt
		cc_Ischaemic_heart_disease
		cc_Chronic_heart_failure
		cc_Hypertension
		cc_Emphysema_without_COPD
		cc_COPD
		cc_Asthma_without_COPD
		cc_Bronchiectasis_without_CF
		cc_Chronic_respiratory_failure
		cc_Crohns_disease
		cc_Ulcerative_colitis
		cc_Chronic_liver_failure
		cc_Rheumatoid_arthritis
		cc_Arthritis_and_osteoarthritis
		cc_Systemic_lupus_erythematosus
		cc_Osteoporosis
		cc_Chronic_kidney_disease
		cc_Spina_bifida
		cc_Downs_syndrome ;


	do i=1 to dim(chronic_conditions);
		chronic_conditions[i] = 0;
	end;

	do i=1 to MAXDDXVAR;
		icd_code = DDXARRAY[i];
			
		if (icd_code not in: &CHARLSON_DIAGS. and icd_code not in &CHARLSON_DIAGS_LITERAL.) then do;
			
								 
			rc3=cc.find();

				if rc3 ne 0 then do;
					u_code = icd_code;
					rc4=su.find();

					if rc4 ne 0 then u_group=0;
											   
			 
					else chronic_conditions[u_group] = 1;
				end;
				else chronic_conditions[u_group] = 1;
		end;
	end;
																	   
	low_los_flag = 0;
	rc6 = p_nep26.find();
	if rc6 ne 0 then do;
		drg_inlier_lb=.;
	end;
	else do;
		if los < drg_inlier_lb then low_los_flag = 1;
	end;

	cc_obesity = max(obesity,cc_obesity);
	cc_Parkinsons_disease = max(parkinson_disease, cc_Parkinsons_disease);
	cc_Dementia = max(_cc_Dementia,cc_Dementia);
	cc_paraplegia = max(cc_Tetplg_prplg_dplg_hmplg_dt, cc_paraplegia);

	
	*Create flag for Emergency, Treat Emergency, Not assigned and Not Known as Emergency;
	attrib flag_emergency length = 3.;
	if &urgadm. = '2' then flag_emergency = 0;
	else flag_emergency = 1;

run;