*========================= PROGRAM: PSI_HOSP_MEASURES.SAS ==========================;
*
*  DESCRIPTION:
*         Assigns the Patient Safety Outcome measure of interest and stratifiers
*         to inpatient records
*         Variables created by this program are TPPSXX, PPPSXX, stratifiers, and severity indicators.
*
*  VERSION: SAS QI v2020
*  RELEASE DATE: JULY 2020
*
*  USER NOTE1: Make sure you have created the format library
*              using PSI_FORMATS.SAS BEFORE running this program.
*
*  USER NOTE2: The AHRQ QI software does not support the calculation of weighted
*              estimates and standard errors using complex sampling designs.
*
*  USER NOTE3: See the AHRQ_PSI_SAS_v2020_ICD10_Release_Notes.txt file for software change notes.
*
*  USER NOTE4: Although some of the exclusion criteria for present on admission 
*              conditions were removed from v2020, we retained some of the original 
*              logic for potential use in future versions.
*
*===================================================================================;

 title2 'PSI_HOSP_MEASURES PROGRAM';
 title3 'AHRQ INPATIENT QUALITY INDICATORS: ASSIGN PSIs TO INPATIENT DATA';

 * ------------------------------------------------------------------ ;
 * --- CREATE A PERMANENT DATASET CONTAINING ALL RECORDS THAT     --- ;
 * --- WILL NOT BE INCLUDED IN ANALYSIS BECAUSE KEY VARIABLE      --- ;
 * --- VALUES ARE MISSING. REVIEW AFTER RUNNING PSI_HOSP_MEASURES. -- ;
 * ------------------------------------------------------------------ ;

 data   OUTMSR.&DELFILE.
     (keep=KEY HOSPID SEX AGE DX1 MDC YEAR DQTR);
 set     INMSR.&DISCHARGE.;
 if (AGE lt 0) or (AGE lt 18 and MDC notin (14)) or (SEX le 0) or
    (compress(DX1) in ('')) or (DQTR le .Z) or (YEAR le .Z) or (DRG = 999);
 run;

 * ------------------------------------------------------------------ ;
 * --- PATIENT SAFETY INDICATOR (PSI) NAMING CONVENTION:          --- ;
 * --- THE FIRST LETTER IDENTIFIES THE PATIENT SAFETY             --- ;
 * --- INDICATOR AS ONE OF THE FOLLOWING:                         --- ;
 *               (T) NUMERATOR ("TOP")                            --- ;
 *               (P) POPULATION ("POP") IS DENOMINATOR            --- ;
 * --- THE SECOND LETTER IDENTIFIES THE PSI AS A HOSPITAL (P)     --- ;
 * --- LEVEL INDICATOR. THE NEXT TWO CHARACTERS ARE               --- ;
 * --- ALWAYS 'PS'. THE LAST TWO DIGITS ARE THE INDICATOR         --- ;
 * --- NUMBER (WITHIN THAT SUBTYPE).                              --- ;
 * ------------------------------------------------------------------ ;

 data   OUTMSR.&OUTFILE_MEAS.
    (keep=KEY HOSPID DRG MDC MDRG YEAR DQTR AGE SEX
          AGECAT SEXCAT RACECAT PAYCAT LOS 
          CHF      VALVE    PULMCIRC PERIVASC
          HTN_C    PARA     NEURO    CHRNLUNG
          DM       DMCX     HYPOTHY  RENLFAIL LIVER
          ULCER    AIDS     LYMPH    METS     TUMOR
          ARTH     COAG     OBESE    WGHTLOSS LYTES
          BLDLOSS  ANEMDEF  ALCOHOL  DRUG     PSYCH
          DEPRESS
          TRNSFER
          XPPS04_DVT_PE_ANY XPPS04_DVT_PE_SEVERE
          XPPS04_PNEUMONIA_ANY XPPS04_PNEUMONIA_SEVERE
          XPPS04_SEPSIS_ANY XPPS04_SEPSIS_SEVERE
          XPPS04_SHOCK_ANY XPPS04_SHOCK_SEVERE
          XPPS04_GIHEMORRHAGE_ANY XPPS04_GIHEMORRHAGE_SEVERE

          TPPS02-TPPS04  TPPS04_DVT_PE TPPS04_PNEUMONIA TPPS04_SEPSIS TPPS04_SHOCK TPPS04_GIHEMORRHAGE
          TPPS05-TPPS14 TPPS14_OPEN TPPS14_NONOPEN TPPS15 TPPS18 TPPS19
          PPPS02-PPPS04  PPPS04_DVT_PE PPPS04_PNEUMONIA PPPS04_SEPSIS PPPS04_SHOCK PPPS04_GIHEMORRHAGE
          PPPS05-PPPS14 PPPS14_OPEN PPPS14_NONOPEN PPPS15 PPPS18 PPPS19);
 set   INMSR.&DISCHARGE.
      (keep=KEY HOSPID DRG MDC SEX AGE
            RACE YEAR DQTR PAY1 
            DISP LOS ASOURCE POINTOFORIGINUB04 ATYPE
            DX1-DX&NDX. PR1-PR&NPR. %ADDPRDAY
            DXPOA1-DXPOA&NDX.);

 label
 HOSPID   = 'Hospital Identification Number'
 SEX = 'Sex of the patient'
 key = 'Unique record identifier'
 ;

 * -------------------------------------------------------------------------- ;
 * -- DELETE RECORDS WITH MISSING VALUES FOR AGE, SEX, DX1, DQTR, OR YEAR --- ;
 * -- DELETE NON ADULT RECORDS OR DRG = 999 --------------------------------- ;
 * -------------------------------------------------------------------------- ;
 if SEX le 0 then delete;
 if AGE lt 0 then delete;
 if AGE lt 18 and MDC notin (14) then delete;
 if compress(DX1) in ('') then delete;
 if DQTR le .Z then delete;
 if YEAR le .Z then delete;
 if DRG = 999 then delete;

 * --------------------------------------------------------------------- ;
 * --- DEFINE MDC ------------------------------------------------------ ;
 * --------------------------------------------------------------------- ; 
 * --- The software assumes MDC is available on the input file.       -- ;
 * --- If MDC is not available, assign using the CMS MS-DRG Grouper.  -- ;
 * --------------------------------------------------------------------- ;
 attrib MDCNEW length=3
   label='RESPECIFIED MDC';

 if MDC notin (01,02,03,04,05,06,07,08,09,10,
               11,12,13,14,15,16,17,18,19,20,
               21,22,23,24,25)
 then do;
    MDCNEW = put(DRG,MDCF2T.);
    if MDCNEW in (01,02,03,04,05,06,07,08,09,10,
                  11,12,13,14,15,16,17,18,19,20,
                  21,22,23,24,25)
    then MDC=MDCNEW;
    else put "INVALID MDC KEY: " KEY " MDC " MDC " DRG " DRG ;
 end;

 * -------------------------------------------------------------- ;
 * --- DEFINE ICD-10-CM VERSION --------------------------------- ;
 * -------------------------------------------------------------- ;
 attrib ICDVER length=3
   label='ICD-10-CM VERSION';

 ICDVER = 0;
 if (YEAR in (2015) and DQTR in (4))          then ICDVER = 33;
 else if (YEAR in (2016) and DQTR in (1,2,3)) then ICDVER = 33;
 else if (YEAR in (2016) and DQTR in (4))     then ICDVER = 34;
 else if (YEAR in (2017) and DQTR in (1,2,3)) then ICDVER = 34;
 else if (YEAR in (2017) and DQTR in (4))     then ICDVER = 35;
 else if (YEAR in (2018) and DQTR in (1,2,3)) then ICDVER = 35;
 else if (YEAR in (2018) and DQTR in (4))     then ICDVER = 36;
 else if (YEAR in (2019) and DQTR in (1,2,3)) then ICDVER = 36;
 else if (YEAR in (2019) and DQTR in (4))     then ICDVER = 37;
 else if (YEAR in (2020) and DQTR in (1,2,3)) then ICDVER = 37;
 else ICDVER = 37; *Defaults to last version for discharges outside coding updates.;

 * -------------------------------------------------------------- ;
 * --- DEFINE MEDICAL DRGS -------------------------------------- ;
 * -------------------------------------------------------------- ;
 attrib MEDICDR length=3
   label='MEDICAL DRGS';

 if %MDR($MEDIC2R.) then MEDICDR = 1;
 else MEDICDR = 0;


 * -------------------------------------------------------------- ;
 * --- DEFINE SURGICAL DRGS ------------------------------------- ;
 * -------------------------------------------------------------- ;
 attrib SURGIDR length=3
   label='SURGICAL DRGS';

 if %MDR($SURGI2R.)
 then SURGIDR = 1;
 else SURGIDR = 0;


 * -------------------------------------------------------------- ;
 * --- DEFINE MODIFIED DRGS ------------------------------------- ;
 * -------------------------------------------------------------- ;
 attrib MDRG length=4
   label='Modified DRG';

 MDRG = put(put(DRG,Z3.),$DRGF2T.);


 * -------------------------------------------------------------- ;
 * --- DEFINE LOW MORT DRGS ------------------------------------- ;
 * -------------------------------------------------------------- ;
 attrib LOWMODR length=3
   label='LOW MORTALITY DRGS';

 LOWMODR = put(MDRG,LOWMODR.);


 * -------------------------------------------------------------- ;
 * --- DEFINE STRATIFIER: PAYER CATEGORY ------------------------ ;
 * -------------------------------------------------------------- ;
 attrib PAYCAT length=3
   label='Patient Primary Payer';

 select (PAY1);
   when (1)  PAYCAT = 1;
   when (2)  PAYCAT = 2;
   when (3)  PAYCAT = 3;
   when (4)  PAYCAT = 4;
   when (5)  PAYCAT = 5;
   otherwise PAYCAT = 6;
 end;


 * -------------------------------------------------------------- ;
 * --- DEFINE STRATIFIER: RACE CATEGORY ------------------------- ;
 * -------------------------------------------------------------- ;
 attrib RACECAT length=3
    label = 'Race Categories';

 select (RACE);
   when (1)  RACECAT = 1;
   when (2)  RACECAT = 2;
   when (3)  RACECAT = 3;
   when (4)  RACECAT = 4;
   when (5)  RACECAT = 5;
   otherwise RACECAT = 6;
 end;


 * -------------------------------------------------------------- ;
 * --- DEFINE STRATIFIER: AGE CATEGORY  ------------------------- ;
 * -------------------------------------------------------------- ;
 attrib AGECAT length=3
   label='Age Categories';

 select;
   when (      AGE < 18)  AGECAT = 0;
   when (18 <= AGE < 40)  AGECAT = 1;
   when (40 <= AGE < 65)  AGECAT = 2;
   when (65 <= AGE < 75)  AGECAT = 3;
   when (75 <= AGE     )  AGECAT = 4;
   otherwise AGECAT = 0;
 end;


 * -------------------------------------------------------------- ;
 * --- DEFINE STRATIFIER: SEX CATEGORY  ------------------------- ;
 * -------------------------------------------------------------- ;
 attrib SEXCAT length=3
   label  = 'Sex Categories';

 select (SEX);
   when (1)  SEXCAT = 1;
   when (2)  SEXCAT = 2;
   otherwise SEXCAT = 0;
 end;


 * -------------------------------------------------------------- ;
 * --- COUNT OR PROCEDURES AND IDENTIFY FIRST OR PROCEDURE ------ ;
 * -------------------------------------------------------------- ;
 attrib ORCNT length=8
   label='OR PROCEDURE COUNT';

 attrib ORDAY length=8
   label='OR PROCEDURE DAY';

%ORCNT;


 * -------------------------------------------------------------- ;
 * --- DEFINE HOSPITAL LEVEL INDICATORS ------------------------- ;
 * -------------------------------------------------------------- ;
 %macro label_qis(flag=, qi_num=, qi_name=);
   attrib TP&qi_num. length=8
   label = "&qi_name. (Numerator)"
   ;
   attrib PP&qi_num. length=8
   label = "&qi_name. (Population)"
   ;

   %IF %substr(&qi_num., 1, 4) = PS04 AND &qi_num. ne PS04 %THEN %DO;
    label 
    XP&qi_num._ANY    = "&qi_num. Regression Variable: Any Triggering Complication POA"
    XP&qi_num._SEVERE = "&qi_num. Regression Variable: Triggering Complication Severe"
    ;
   %END;


 * -------------------------------------------------------------- ;
 * --- RE-LABEL DAY DEPENDENT INDICATORS ------------------------ ;
 * -------------------------------------------------------------- ;
   %IF &PRDAY. = 0 AND &flag. = 1 %THEN %DO;
    label
    TP&qi_num. = "&qi_name.-NO PRDAY (Numerator)"
    ;
   %END;

 %mend label_qis;

 %label_qis(flag=0, qi_num=PS02,              qi_name=PSI 02 Death Rate in Low-Mortality Diagnosis Related Groups (DRGs));
 %label_qis(flag=1, qi_num=PS03,              qi_name=PSI 03 Pressure Ulcer Rate);
 %label_qis(flag=0, qi_num=PS04,              qi_name=PSI 04 Death Rate among Surgical Inpatients with Serious Treatable Complications);
 %label_qis(flag=0, qi_num=PS04_DVT_PE,       qi_name=PSI 04 Death Rate among Surgical Inpatients with Serious Treatable Complications Stratum: Deep Vein Thrombosis/Pulmonary Embolism (DVT/PE));
 %label_qis(flag=0, qi_num=PS04_PNEUMONIA,    qi_name=PSI 04 Death Rate among Surgical Inpatients with Serious Treatable Complications Stratum: Pneumonia);
 %label_qis(flag=0, qi_num=PS04_SEPSIS,       qi_name=PSI 04 Death Rate among Surgical Inpatients with Serious Treatable Complications Stratum: Sepsis);
 %label_qis(flag=0, qi_num=PS04_SHOCK,        qi_name=PSI 04 Death Rate among Surgical Inpatients with Serious Treatable Complications Stratum: Shock/Cardiac Arrest);
 %label_qis(flag=0, qi_num=PS04_GIHEMORRHAGE, qi_name=PSI 04 Death Rate among Surgical Inpatients with Serious Treatable Complications Stratum: Gastrointestinal (GI) Hemorrhage/Acute Ulcer);
 %label_qis(flag=0, qi_num=PS05,              qi_name=PSI 05 Retained Surgical Item or Unretrieved Device Fragment Count);
 %label_qis(flag=0, qi_num=PS06,              qi_name=PSI 06 Iatrogenic Pneumothorax Rate);
 %label_qis(flag=0, qi_num=PS07,              qi_name=PSI 07 Central Venous Catheter-Related Blood Stream Infection Rate);
 %label_qis(flag=1, qi_num=PS08,              qi_name=PSI 08 In Hospital Fall with Hip Fracture Rate);
 %label_qis(flag=1, qi_num=PS09,              qi_name=PSI 09 Perioperative Hemorrhage or Hematoma Rate);
 %label_qis(flag=1, qi_num=PS10,              qi_name=PSI 10 Postoperative Acute Kidney Injury Requiring Dialysis Rate);
 %label_qis(flag=1, qi_num=PS11,              qi_name=PSI 11 Postoperative Respiratory Failure Rate);
 %label_qis(flag=1, qi_num=PS12,              qi_name=PSI 12 Perioperative Pulmonary Embolism or Deep Vein Thrombosis Rate);
 %label_qis(flag=0, qi_num=PS13,              qi_name=PSI 13 Postoperative Sepsis Rate);
 %label_qis(flag=1, qi_num=PS14,              qi_name=PSI 14 Postoperative Wound Dehiscence Rate);
 %label_qis(flag=0, qi_num=PS14_OPEN,         qi_name=PSI 14 Postoperative Wound Dehiscence Rate Stratum: Open Approach);
 %label_qis(flag=0, qi_num=PS14_NONOPEN,      qi_name=PSI 14 Postoperative Wound Dehiscence Rate Stratum: Non-Open Approach);
 %label_qis(flag=0, qi_num=PS15,              qi_name=PSI 15 Abdominopelvic Accidental Puncture or Laceration Rate);
 %label_qis(flag=0, qi_num=PS18,              qi_name=PSI 18 Obstetric Trauma Rate - Vaginal Delivery With Instrument);
 %label_qis(flag=0, qi_num=PS19,              qi_name=PSI 19 Obstetric Trauma Rate - Vaginal Delivery Without Instrument);


 * -------------------------------------------------------------- ;
 * --- CONSTRUCT AHRQ COMORBIDITY ------------------------------- ;
 * -------------------------------------------------------------- ;

  *-- Assign Comorbidity flags for each discharge --* ;
 %include MacLib(PSI_HOSP_Comorb_Code_v2020.sas);

 * -------------------------------------------------------------- ;
 * --- CONSTRUCT HOSPITAL LEVEL INDICATORS ---------------------- ;
 * -------------------------------------------------------------- ;

 * ------------------------------------------------------------------------------ ;
 * ---   PSI 02 : DEATH RATE IN LOW-MORTALITY DIAGNOSIS RELATED GROUPS (DRGS) --- ;
 * ------------------------------------------------------------------------------ ;

   if LOWMODR then do;

      TPPS02 = 0;

      if DISP in (20) then TPPS02 = 1;

       *** Exclude Trauma, Immunocompromised state, and cancer;

      if %MDX($TRAUMID.) or %MDX($IMMUNID.) or
         %MPR($IMMUNIP.) or %MDX($CANCEID.)
      then TPPS02 = .;

      *** Exclude missing discharge disposition or transfer acute care facility ***;

      if DISP le .Z or DISP=2 then TPPS02 = .;

   end;


 * --------------------------------------------------- ;
 * ---    PSI 03 : PRESSURE ULCER RATE             --- ;
 * --------------------------------------------------- ;

   if (MEDICDR or SURGIDR) then do;

      TPPS03 = 0; QPPS03 = 0;

      if %MDX2($DECUBVD.) then TPPS03 = 1;

      *** Exclude principal diagnosis or numerator event without at least one DECUBEXD not POA ;

      if %MDX1($DECUBEXD.) then TPPS03 = .;
      if %MDX2Q2($DECUBEXD.) then QPPS03 = 1; /* at least one POA */
      if %MDX2Q1($DECUBEXD.) then QPPS03 = 0; /* at least one not POA */

      *** Exclude severe burns;

      if %MDX($BURNDX.) then TPPS03 = .;

      *** Exclude exfoliation;

      if %MDX($EXFOLIATXD.) then TPPS03 = .;

      *** Exclude MDC 14;

      if MDC in (14) then TPPS03 = .;

      *** Exclude LOS < 3 ;

      if LOS < 3 then TPPS03 = .;

      *** Determine numerator exclusion for secondary Dx POA;

      if TPPS03 = . then QPPS03 = .;

   end;


 * ------------------------------------------------------------------------------------------- ;
 * ---  PSI 04 : DEATH RATE AMONG SURGICAL INPATIENTS WITH SERIOUS TREATABLE COMPLICATIONS --- ;
 * ------------------------------------------------------------------------------------------- ;

%ORDAY($BLANK.); /* resulting ORDAY used twice in PSI 04 */

   if (SURGIDR and ORCNT > 0)   and
      (0 <= ORDAY <=2 or ATYPE in (3)) and
      (17 < AGE < 90 or MDC in (14))    then do;

       /**-- STRATUM_DVT_PE: DEEP VEIN THROMBOSIS/PULMONARY EMBOLISM (DVT/PE) --**/
   if (%MDX2($FTR2DXB.) and
       not (%MDX1($FTR2DXB.) or %MDX1($OBEMBOL.)))
      or
       /**-- STRATUM_PNEUMONIA: PNEUMONIA --**/
      (%MDX2($FTR3DX.) and
       not (%MDX1($FTR3DX.) or %MDX1($FTR3EXA.) or %MDX($FTR3EXB.) or MDC in (4) or %MPR($LUNGCIP.)))
      or
       /**-- STRATUM_SEPSIS: SEPSIS --**/
      (%MDX2($FTR4DX.) and
       not (%MDX1($FTR4DX.) or %MDX1($INFECID.)))
      or
       /**-- STRATUM_SHOCK: SHOCK/CARDIAC ARREST --**/
      ((%MDX2($FTR5DX.) or %MPR($FTR5PR.)) and
       not (%MDX1($FTR5DX.) or %MDX1($FTR5EX.) or %MDX1($HEMORID.) or %MDX1($TRAUMID.) or
            %MDX1($GASTRID.) or (%MDX2($FTR6GV.) and %MDX1($FTR6QD.)) or MDC in (4,5) ))
      or
       /**-- STRATUM_GIHEMORRHAGE: GASTROINTESTINAL (GI) HEMORRHAGE/ACUTE ULCER --**/
      (%MDX2($FTR6DX.) and
       not (%MDX1($FTR6DX.) or %MDX1($FTR6EX.) or %MDX1($TRAUMID.) or
            %MDX1($ALCHLSM.) or (%MDX2($FTR6GV.) and %MDX1($FTR6QD.)) or MDC in (6,7)))
   then do;

      TPPS04 = 0;

      if DISP in (20) then TPPS04 = 1;

      *** Exclude Transfers to an acute care facility and those admitted from hospice care;

      if POINTOFORIGINUB04 in ('F') or DISP le .Z or DISP=2 then TPPS04 = .;

   end;

   end;

 * ----------------------------------------------------------------------------------------------------------------------- ;
 * --- PSI 04 Stratum_DVT_PE       : DEATH AMONG SURGICAL INPATIENTS: DEEP VEIN THROMBOSIS/PULMONARY EMBOLISM (DVT/PE) --- ;
 * --- PSI 04 Stratum_PNEUMONIA    : DEATH AMONG SURGICAL INPATIENTS: PNEUMONIA ------------------------------------------ ;
 * --- PSI 04 Stratum_SEPSIS       : DEATH AMONG SURGICAL INPATIENTS: SEPSIS --------------------------------------------- ;
 * --- PSI 04 Stratum_SHOCK        : DEATH AMONG SURGICAL INPATIENTS: SHOCK/CARDIAC ARREST ------------------------------- ;
 * --- PSI 04 Stratum_GIHEMORRHAGE : DEATH AMONG SURGICAL INPATIENTS: GASTROINTESTINAL (GI) HEMORRHAGE/ACUTE ULCER ------- ;
 * ----------------------------------------------------------------------------------------------------------------------- ;
 
   if (SURGIDR and ORCNT > 0)           and
      (0 <= ORDAY <= 2 or ATYPE in (3)) and
      (17 < AGE < 90 or MDC in (14))    then do;

      /** STRATUM_DVT_PE: DEEP VEIN THROMBOSIS/PULMONARY EMBOLISM (DVT/PE) ***/

      if (%MDX2($FTR2DXB.)) then do;

          TPPS04_DVT_PE = 0;
          if DISP in (20) then TPPS04_DVT_PE = 1;

          if %MDX1($FTR2DXB.) then TPPS04_DVT_PE = .;
          if %MDX1($OBEMBOL.) then TPPS04_DVT_PE = .;

          *** Exclude Transfers to an acute care facility and those admitted from hospice care;

          if POINTOFORIGINUB04 in ('F') or DISP le .Z or DISP=2 then TPPS04_DVT_PE = .;

		  *** Define ANY and SEVERE risk adjustment variables ;

		  XPPS04_DVT_PE_ANY = 0;
		  XPPS04_DVT_PE_SEVERE=0;
				do i = 1 to &NDX.;
					if put(DX(i),$FTR2DXB.) = '1' then do;
						if DXPOA(i) in ('Y','W') then XPPS04_DVT_PE_ANY = 1;
						if put(DX(i),$SEVDPEDX.) = '1' then XPPS04_DVT_PE_SEVERE=1;
					end;
				end;

      end;

      /** STRATUM_PNEUMONIA: PNEUMONIA ***/

      if %MDX2($FTR3DX.) then do;

          TPPS04_PNEUMONIA = 0;
          if DISP in (20) then TPPS04_PNEUMONIA = 1;

          if %MDX1($FTR3DX.)   then TPPS04_PNEUMONIA = .;
          if %MDX1($FTR3EXA.)  then TPPS04_PNEUMONIA = .;
          if %MDX($FTR3EXB.)   then TPPS04_PNEUMONIA = .;
          if MDC in (4)        then TPPS04_PNEUMONIA = .;
          if %MPR($LUNGCIP.)   then TPPS04_PNEUMONIA = .;

          *** Exclude Transfers to an acute care facility and those admitted from hospice care;

          if POINTOFORIGINUB04 in ('F') or DISP le .Z or DISP=2 then TPPS04_PNEUMONIA = .;

		  *** Define ANY and SEVERE risk adjustment variables ;

		  XPPS04_PNEUMONIA_ANY = 0;
		  XPPS04_PNEUMONIA_SEVERE=0;
				do i = 1 to &NDX.;
					if put(DX(i),$FTR3DX.) = '1' then do;
						if DXPOA(i) in ('Y','W') then XPPS04_PNEUMONIA_ANY = 1;
						if put(DX(i),$SEVPNEUDX.) = '1' then XPPS04_PNEUMONIA_SEVERE=1;
					end;
				end;

      end;


      /** STRATUM_SEPSIS: SEPSIS ***/

      if %MDX2($FTR4DX.) then do;

          TPPS04_SEPSIS = 0;
          if DISP in (20) then TPPS04_SEPSIS = 1;

          if %MDX1($FTR4DX.) then TPPS04_SEPSIS = .;
          if %MDX1($INFECID.) then TPPS04_SEPSIS = .;

           *** Exclude Transfers to an acute care facility and those admitted from hospice care;

          if POINTOFORIGINUB04 in ('F') or DISP le .Z or DISP=2 then TPPS04_SEPSIS = .;

		  *** Define ANY and SEVERE risk adjustment variables ;

		  XPPS04_SEPSIS_ANY = 0;
		  XPPS04_SEPSIS_SEVERE=0;
				do i = 1 to &NDX.;
					if put(DX(i),$FTR4DX.) = '1' then do;
						if DXPOA(i) in ('Y','W') then XPPS04_SEPSIS_ANY = 1;
						if put(DX(i),$SEVSEPDX.) = '1' then XPPS04_SEPSIS_SEVERE=1;
					end;
				end;

      end;


      /** STRATUM_SHOCK: SHOCK/CARDIAC ARREST ***/

      if %MDX2($FTR5DX.) or %MPR($FTR5PR.) then do;

          TPPS04_SHOCK = 0;
         if DISP in (20) then TPPS04_SHOCK = 1;

          if %MDX1($FTR5DX.)  then TPPS04_SHOCK = .;
          if %MDX1($TRAUMID.) then TPPS04_SHOCK = .;
          if %MDX1($HEMORID.) then TPPS04_SHOCK = .;
          if %MDX1($GASTRID.) then TPPS04_SHOCK = .;
          if %MDX1($FTR5EX.)  then TPPS04_SHOCK = .;
          if %MDX2($FTR6GV.) and %MDX1($FTR6QD.) then TPPS04_SHOCK = .;
          if MDC in (4,5)     then TPPS04_SHOCK = .;

          *** Exclude Transfers to an acute care facility and those admitted from hospice care;

          if POINTOFORIGINUB04 in ('F') or DISP le .Z or DISP=2 then TPPS04_SHOCK = .;

		  *** Define ANY and SEVERE risk adjustment variables ;

	    XPPS04_SHOCK_ANY = 0;
		  XPPS04_SHOCK_SEVERE=0;
				do i = 1 to &NDX.;
					if put(DX(i),$FTR5DX.) = '1' then do;
						if DXPOA(i) in ('Y','W') then XPPS04_SHOCK_ANY = 1;
						if put(DX(i),$SEVSCKDX.) = '1' then XPPS04_SHOCK_SEVERE=1;
					end;
				end;
				do i = 1 to &NPR. until (XPPS04_SHOCK_SEVERE=1);
					if put(PR(i),$FTR5PR.) = '1' then XPPS04_SHOCK_SEVERE=1;
				end;

      end;


      /** STRATUM_GIHEMORRHAGE: GASTROINTESTINAL (GI) HEMORRHAGE/ACUTE ULCER ***/

      if %MDX2($FTR6DX.) then do;

          TPPS04_GIHEMORRHAGE = 0;
          if DISP in (20) then TPPS04_GIHEMORRHAGE = 1;

          if %MDX1($FTR6DX.)  then TPPS04_GIHEMORRHAGE = .;
          if %MDX1($FTR6EX.)  then TPPS04_GIHEMORRHAGE = .;
          if %MDX1($TRAUMID.) then TPPS04_GIHEMORRHAGE = .;
          if %MDX1($ALCHLSM.) then TPPS04_GIHEMORRHAGE = .;
          if %MDX2($FTR6GV.) and %MDX1($FTR6QD.) then TPPS04_GIHEMORRHAGE = .;
          if MDC in (6,7)     then TPPS04_GIHEMORRHAGE = .;

          *** Exclude Transfers to an acute care facility and those admitted from hospice care;

          if POINTOFORIGINUB04 in ('F') or DISP le .Z or DISP=2 then TPPS04_GIHEMORRHAGE = .;

		  *** Define ANY and SEVERE risk adjustment variables ;

		  XPPS04_GIHEMORRHAGE_ANY = 0;
		  XPPS04_GIHEMORRHAGE_SEVERE=0;
				do i = 1 to &NDX.;
					if put(DX(i),$FTR6DX.) = '1' then do;
						if DXPOA(i) in ('Y','W') then XPPS04_GIHEMORRHAGE_ANY = 1;
						if put(DX(i),$SEVGIHDX.) = '1' then XPPS04_GIHEMORRHAGE_SEVERE=1;
					end;
				end;

      end;


 *--------------------------------------------------------------- ;
 *--- MAKE STRATA MUTUALLY EXCLUSIVE AND EXHAUSTIVE ------------- ;
 *--------------------------------------------------------------- ;
 
       *** Assign discharges with multiple conditions to stratum with highest risk of the outcome;

       if TPPS04_SHOCK ne . then do;
			           TPPS04_SEPSIS = .;      XPPS04_SEPSIS_ANY = .;      XPPS04_SEPSIS_SEVERE = .;
			           TPPS04_PNEUMONIA = .;   XPPS04_PNEUMONIA_ANY = .;   XPPS04_PNEUMONIA_SEVERE = .;
			           TPPS04_DVT_PE = .;      XPPS04_DVT_PE_ANY = .;      XPPS04_DVT_PE_SEVERE = .;
			           TPPS04_GIHEMORRHAGE = .;XPPS04_GIHEMORRHAGE_ANY = .;XPPS04_GIHEMORRHAGE_SEVERE = .;
       end;

       else if TPPS04_SEPSIS ne . then do;
			           TPPS04_SHOCK = .;       XPPS04_SHOCK_ANY = .;       XPPS04_SHOCK_SEVERE = .;
			           TPPS04_PNEUMONIA = .;   XPPS04_PNEUMONIA_ANY = .;   XPPS04_PNEUMONIA_SEVERE = .;
			           TPPS04_DVT_PE = .;      XPPS04_DVT_PE_ANY = .;      XPPS04_DVT_PE_SEVERE = .;
			           TPPS04_GIHEMORRHAGE = .;XPPS04_GIHEMORRHAGE_ANY = .;XPPS04_GIHEMORRHAGE_SEVERE = .;
       end;

       else if TPPS04_PNEUMONIA ne . then do;
			           TPPS04_SHOCK = .;       XPPS04_SHOCK_ANY = .;       XPPS04_SHOCK_SEVERE = .;
			           TPPS04_SEPSIS = .;      XPPS04_SEPSIS_ANY = .;      XPPS04_SEPSIS_SEVERE = .;
			           TPPS04_DVT_PE = .;      XPPS04_DVT_PE_ANY = .;      XPPS04_DVT_PE_SEVERE = .;
			           TPPS04_GIHEMORRHAGE = .;XPPS04_GIHEMORRHAGE_ANY = .;XPPS04_GIHEMORRHAGE_SEVERE = .;
       end;

       else if TPPS04_DVT_PE ne . then do;
			           TPPS04_SHOCK = .;       XPPS04_SHOCK_ANY = .;       XPPS04_SHOCK_SEVERE = .;
			           TPPS04_SEPSIS = .;      XPPS04_SEPSIS_ANY = .;      XPPS04_SEPSIS_SEVERE = .;
			           TPPS04_PNEUMONIA = .;   XPPS04_PNEUMONIA_ANY = .;   XPPS04_PNEUMONIA_SEVERE = .;
			           TPPS04_GIHEMORRHAGE = .;XPPS04_GIHEMORRHAGE_ANY = .;XPPS04_GIHEMORRHAGE_SEVERE = .;
       end;

       else if TPPS04_GIHEMORRHAGE ne . then do;
			           TPPS04_SHOCK = .;    XPPS04_SHOCK_ANY = .;    XPPS04_SHOCK_SEVERE = .;
			           TPPS04_SEPSIS = .;   XPPS04_SEPSIS_ANY = .;   XPPS04_SEPSIS_SEVERE = .;
			           TPPS04_PNEUMONIA = .;XPPS04_PNEUMONIA_ANY = .;XPPS04_PNEUMONIA_SEVERE = .;
			           TPPS04_DVT_PE = .;   XPPS04_DVT_PE_ANY = .;   XPPS04_DVT_PE_SEVERE = .;
       end;

     end;


 * ----------------------------------------------------------------------------- ;
 * ---  PSI 05 : RETAINED SURGICAL ITEM OR UNRETRIEVED DEVICE FRAGMENT COUNT --- ;
 * ----------------------------------------------------------------------------- ;

   if (MEDICDR or SURGIDR) then do;

      TPPS05 = .; QPPS05 = .;

      if %MDX2($FOREIID.) then TPPS05 = 1;
      if %MDX2Q1($FOREIID.) then QPPS05 = 0;

      *** Exclude principal diagnosis;

      if %MDX1($FOREIID.) then TPPS05 = .;
      if %MDX2Q2($FOREIID.) then QPPS05 = 1;

      *** Determine numerator exclusion for secondary Dx POA;

      if TPPS05 = . then QPPS05 = .;

   end;


 * ------------------------------------------------- ;
 * ---  PSI 06 : IATROGENIC PNEUMOTHORAX RATE    --- ;
 * ------------------------------------------------- ;

   if (MEDICDR or SURGIDR) then do;

      TPPS06 = 0; QPPS06 = 0;

      if %MDX2($IATROID.) then TPPS06 = 1;

      *** Exclude principal diagnosis;

      if %MDX1($IATROID.) then TPPS06 = .;
      if %MDX2Q2($IATROID.) then QPPS06 = 1;

      *** Exclude Chest Trauma, Pleural effusion or MDC 14;

      if %MDX($CTRAUMD.) or %MDX($PLEURAD.) or MDC in (14)
      then TPPS06 = .;

      *** Exclude Thoracic surgery or Cardiac surgery;

      if %MPR($THORAIP.) or %MPR($CARDSIP.)
      then TPPS06 = .;

      *** Determine numerator exclusion for secondary Dx POA;

      if TPPS06 = . then QPPS06 = .;

  end;


 * ---------------------------------------------------------------------------- ;
 * --- PSI 07 : CENTRAL VENOUS CATHETER-RELATED BLOOD STREAM INFECTION RATE --- ;
 * ---------------------------------------------------------------------------- ;
   if (MEDICDR or SURGIDR) then do;

      TPPS07 = 0; QPPS07 = 0;

      if %MDX2($IDTMC3D.) then TPPS07 = 1;

      *** Exclude principal diagnosis;

      if %MDX1($IDTMC3D.) then TPPS07= .;
      if %MDX2Q2($IDTMC3D.) then QPPS07= 1;

      *** Exclude Immunocompromised state and Cancer;

      if %MDX($IMMUNID.) or %MPR($IMMUNIP.) or %MDX($CANCEID.)
      then TPPS07 = .;

      *** Exclude LOS < 2;

      if LOS < 2 then TPPS07 = .;

      *** Determine numerator exclusion for secondary Dx POA;

      if TPPS07 = . then QPPS07 = .;

   end;


 * -------------------------------------------------------- ;
 * --- PSI 08 : IN HOSPITAL FALL WITH HIP FRACTURE RATE --- ;
 * -------------------------------------------------------- ;

   if (SURGIDR and ORCNT > 0) or MEDICDR then do;

      TPPS08 = 0; QPPS08 = 0;

      if %MDX2($HIPFXID.) then TPPS08 = 1;

      *** Exclude principal diagnosis;

      if %MDX1($HIPFXID.) then TPPS08 = .;
      if %MDX2Q2($HIPFXID.) then QPPS08 = 1;

      *** Excludes Seizure, Syncope, Stroke, Coma, Cardiac arrest,
          Poisoning, Trauma, Delirium and other psychoses, Anoxic
          brain injury;

      if %MDX1($SEIZUID.) or %MDX1($SYNCOID.) or %MDX1($STROKID.) or
         %MDX1($COMAID.)  or %MDX1($CARDIID.) or %MDX1($POISOID.) or
         %MDX1($TRAUMID.) or %MDX1($DELIRID.) or %MDX1($ANOXIID.)
      then TPPS08 = .;

      *** Excludes Metastatic cancer, Lymphoid malignancy,
          Bone malignancy;

      if %MDX($METACID.) or %MDX($LYMPHID.) or %MDX($BONEMID.) or %MDX($PROSFXID.)
      then TPPS08 = .;

      *** Exclude MDC 14;

      if MDC in (14) then TPPS08 = .;

      *** Determine numerator exclusion for secondary Dx POA;

      if TPPS08 = . then QPPS08 = .;

   end;


 * ---------------------------------------------------------- ;
 * --- PSI 09 : PERIOPERATIVE HEMORRHAGE OR HEMATOMA RATE --- ;
 * ---------------------------------------------------------- ;

 %MACRO PS9;

  if (&PRDAY. = 1 and ORDAY ne . and MPRDAY ne .) then do;

      if %MDX2($POHMRI2D.) and MPRDAY < ORDAY
      then TPPS09 = .;

  end;

 %MEND;

   if SURGIDR and ORCNT > 0 then do;

      TPPS09 = 0; QPPS09 = 0;

      if %MDX2($POHMRI2D.) and
          (%MPR($HEMOTH2P.))
      then TPPS09 = 1;

      *** Exclude principal diagnosis;

      if %MDX1($POHMRI2D.) then TPPS09 = .;
      if %MDX2Q2($POHMRI2D.) then QPPS09 = 1;

      *** Exclude if control of post-operative hemorrhage or Miscellaneous hemorrhage
          hematoma-related procedure are the only OR procedures;

      %MPRCNT($HEMOTH2P.);
      if ORCNT = MPRCNT then TPPS09 = .;

      *** Exclude if control of post-operative hemorrhage or Miscellaneous Hemorrhage or
          hematoma-related procedure occurs before the first OR procedure;

      %ORDAY($HEMOTH2P.);
       %MPRDAY($HEMOTH2P.);
      %PS9;

      *** Exclude MDC 14;

      if MDC in (14) then TPPS09 = .;

      *** Exclude Coagulation Disorders;

      if %MDX($COAGDID.) then TPPS09 = . ;

      *** Determine numerator exclusion for secondary Dx POA;

      if TPPS09 = . then QPPS09 = .;

   end;


 * -------------------------------------------------------------------------- ;
 * --- PSI 10 : POSTOPERATIVE ACUTE KIDNEY INJURY REQUIRING DIALYSIS RATE --- ;
 * -------------------------------------------------------------------------- ;

 %MACRO PS10;

  if (&PRDAY. = 1  and ORDAY ne . and MPRDAY ne .) then do;

      if MPRDAY <= ORDAY then TPPS10 = .;

  end;

 %MEND;

   if SURGIDR and ORCNT > 0 and ATYPE in (3) then do;

      TPPS10 = 0; QPPS10 = 0;

      if (%MDX2($PHYSIDB.) and %MPR($DIALYIP.)) then TPPS10 = 1;

      *** Exclude principal Dx or secondary Dx POA of acute renal failure or UT Obstruction as principle Dx;

      if %MDX1($PHYSIDB.) or %MDX1($URINARYOBSID.) then TPPS10 = .;
      if %MDX2Q2($PHYSIDB.) then QPPS10 = 1;

      *** Exclude if any dialysis procedure occurs before or on same day as the first OR procedure;

      %ORDAY($BLANK.);
      %MPRDAY($DIALYIP.);
      %PS10;

      *** Exclude if any dialysis access procedure occurs before or on same day as the first OR procedure;

      %ORDAY($BLANK.);
      %MPRDAY($DIALY2P.);
      %PS10;

      *** Exclude Cardiac arrest, Cardiac dysrhythmia, Acute renal failure and Shock, or Chronic renal failure if either principal Dx or secondary Dx POA;

      if (%MDX1($CARDIID.) or %MDX1($CARDRID.) or %MDX1($SHOCKID.) or %MDX1($CRENLFD.))
      then TPPS10 = .;

      if %MDX2Q2($CARDIID.) then CARDIID_cd = 1;
      if %MDX2Q2($CARDRID.) then CARDRID_cd = 1;
      if %MDX2Q2($SHOCKID.) then SHOCKID_cd = 1;
      if %MDX2Q2($CRENLFD.) then CRENLFD_cd = 1;
      if CARDIID_cd or CARDRID_cd or SHOCKID_cd or CRENLFD_cd then QPPS10 = 1;

      *** Exclude any DX POA of solitary kidney disease and any partial nephrectomy procedure;

      if %MDXAQ2($SOLKIDD.) and %MPR($PNEPHREP.) then TPPS10 = .;

      *** Exclude MDC 14;

      if MDC in (14) then TPPS10 = .;

      *** Determine numerator exclusion for secondary Dx POA;

      if TPPS10 = . then QPPS10 = .;

   end;


 * ------------------------------------------------------- ;
 * --- PSI 11 : PERIOPERATIVE RESPIRATORY FAILURE RATE --- ;
 * ------------------------------------------------------- ;

 %MACRO PS11N(FMT,DAYS);

    %MPRDAY(&FMT.);

    if (&PRDAY. = 1  and ORDAY ne . and MPRDAY ne .) then do;

       if MPRDAY >= ORDAY + &DAYS. then TPPS11 = 1;

    end;
    else do;

       if %MPR2(&FMT.) then TPPS11 = 1;

    end;

 %MEND;

 %MACRO PS11;

    if (&PRDAY. = 1  and ORDAY ne . and MPRDAY ne .) then do;

       if MPRDAY < ORDAY then TPPS11 = .;

    end;

 %MEND;

   if SURGIDR and ORCNT > 0 and ATYPE in (3) then do;

      TPPS11 = 0; QPPS11 = 0;

      if %MDX2($ACURF2D.)   then TPPS11 = 1;

      *** Include in numerator if reintubation procedure occurs on the same day or
          # days after the first OR procedure;

      %ORDAY($BLANK.);
      %PS11N($PR9604P.,1);
      %PS11N($PR9671P.,2);
      %PS11N($PR9672P.,0);

      *** Exclude principal diagnosis;

      if %MDX1($ACURF3D.) then TPPS11 = .;
      if %MDX2Q2($ACURF3D.) then QPPS11 = 1;

      *** Exclude if tracheostomy procedure is the only OR procedure;

      %MPRCNT($TRACHIP.);
      if ORCNT = MPRCNT then TPPS11 = .;

      *** Exclude if tracheostomy procedure occurs before the first or procedure;

      %ORDAY($TRACHIP.);
      %MPRDAY($TRACHIP.);
      %PS11;

      *** Exclude Malignant Hyperthermia;

      if %MDX($MALHYPD.) then TPPS11 = .;

      *** Exclude Neuromuscular disorders;

      if %MDX($NEUROMD.) then TPPS11 = .;

      *** Exclude Tracheostomy;

      if %MDXAQ2($TRACHID.) then TPPS11 = .;

      *** Exclude Craniofacial anomalies;

      if %MPR($NUCRANP.) then TPPS11 = .;

      *** Exclude Esophageal resection procedure;

      if %MPR($PRESOPP.) then TPPS11 = .;

      *** Exclude Lung Cancer Procedure;

      if %MPR($LUNGCIP.) then TPPS11 = .;

      *** Exclude diagnosis of degenerative neurological disorder;

      if %MDX($DGNEUID.) then TPPS11 = .;

      *** Exclude Hospitalizations involving lung transplantations;

      if %MPR($LUNGTRANSP.) then TPPS11 = .;

      *** Exclude MDC 4, 5, 14 ;

      if MDC in (4,5,14) then TPPS11 = .;

      *** Determine numerator exclusion for secondary Dx POA;

      if TPPS11 = . then QPPS11 = .;

   end;


 * ------------------------------------------------------------------------------ ;
 * --- PSI 12 : PERIOPERATIVE PULMONARY EMBOLISM OR DEEP VEIN THROMBOSIS RATE --- ;
 * ------------------------------------------------------------------------------ ;

 %MACRO PS12;

    if (&PRDAY. = 1  and ORDAY ne . and MPRDAY ne .) then do;

        if MPRDAY <= ORDAY then TPPS12 = .;

    end;

 %MEND;

   if SURGIDR and ORCNT > 0 then do;

      TPPS12 = 0; QPPS12 = 0;

      if %MDX2($DEEPVIB.) or %MDX2($PULMOID.) then TPPS12 = 1;

      *** Exclude principal diagnosis;

      if %MDX1($DEEPVIB.) or %MDX1($PULMOID.) then TPPS12 = .;

      if %MDX2Q2($DEEPVIB.) then DEEPVIB_cd = 1;
      if %MDX2Q2($PULMOID.) then PULMOID_cd = 1;
      if DEEPVIB_cd or PULMOID_cd then QPPS12 = 1;

      *** Exclude if interruption of vena cava occurs before or on the
          same day as the first OR procedure;

      %ORDAY($VENACIP.);
      %MPRDAY($VENACIP.);
      %PS12;

      *** Exclude if pulmonary arterial thrombectomy before or on the
          same day as the first OR procedure;

      %ORDAY($THROMP.);
      %MPRDAY($THROMP.);
      %PS12;

      *** Exclude if only operating room procedure was for interruption of vena cava or pulmonary arterial thrombectomy;

      %MPRCNT($VENACIP.);
      if ORCNT = MPRCNT then TPPS12 = .;
      MPRCNTV = MPRCNT;

      %MPRCNT($THROMP.);
      if ORCNT = MPRCNT then TPPS12 = .;

      *** Exclude if the combined count equals ORCNT - although unlikely;

      if ORCNT = MPRCNT + MPRCNTV then TPPS12 = .;

      *** Exclude hospitalizations with neurotrauma POA;

      if %MDXAQ2($NEURTRAD.) then TPPS12 = .;

      *** Exclude any extracorporeal membrane oxygenation procedure;

      if %MPR($ECMOP.) then TPPS12 = . ;

      *** Exclude MDC 14;

      if MDC in (14) then TPPS12 = .;

      *** Determine numerator exclusion for secondary Dx POA;

      if TPPS12 = . then QPPS12 = .;

   end;


 * ------------------------------------------------- ;
 * --- PSI 13 : POSTOPERATIVE SEPSIS RATE        --- ;
 * ------------------------------------------------- ;

   if SURGIDR and ORCNT > 0 and ATYPE in (3)  then do;

      TPPS13 = 0; QPPS13 = 0;

      if (%MDX2($SEPTI2D.)) then TPPS13 = 1;

      *** Exclude principal diagnosis;

      if (%MDX1($SEPTI2D.)) then TPPS13 = .;
      if %MDX2Q2($SEPTI2D.) then QPPS13 = 1;

      *** Exclude Infection;

      if %MDX1($INFECID.) then TPPS13 = .;
      if %MDX2Q2($INFECID.) then QPPS13 = 1;

      *** Exclude MDC 14;

      if MDC in (14) then TPPS13 = .;

      *** Determine numerator exclusion for secondary Dx POA;

      if TPPS13 = . then QPPS13 = .;

   end;


 * ---------------------------------------------------- ;
 * --- PSI 14 : POSTOPERATIVE WOUND DEHISCENCE RATE --- ;
 * ---------------------------------------------------- ;

 %MACRO PS14;

   if (&PRDAY. = 1 and (MPRDAYA ne . or MPRDAYA2 ne .) and MPRDAYB ne .) then do;
     if (MPRDAYB <= MPRDAYA or MPRDAYA eq .) and (MPRDAYB <= MPRDAYA2 or MPRDAYA2 eq .) then TPPS14 = .;
   end;

 %MEND;

   if (%MPR($ABDOMIPOPEN.) or %MPR($ABDOMIPOTHER.)) then do;

      TPPS14 = 0;

      if %MPR($RECLOIP.) and %MDX($ABWALLCD.) then TPPS14 = 1;

      *** Exclude if qualifying wound reclosure occurs before or on the same
          day as the first abdominopelvic procedure;

      MPRDAYA=.; MPRDAYA2=.; MPRDAYB=.;
      %MPRDAY($ABDOMIPOPEN.); MPRDAYA = MPRDAY;
      %MPRDAY($ABDOMIPOTHER.); MPRDAYA2 = MPRDAY;
      %MPRDAY($RECLOIP.);
      if MPRDAY > . and %MDX($ABWALLCD.) then MPRDAYB = MPRDAY;
      %PS14;

      *** Exclude Primary Dx or secondary DX POA of Disruption of Internal Operation Wound;

      if %MDX2Q2($ABWALLCD.) then ABWALLCD_cd = 1;
      if %MDX1($ABWALLCD.) or ABWALLCD_cd = 1 then TPPS14 = .;

      *** Exclude Immunocompromised state;

      if %MDX($IMMUNID.) or %MPR($IMMUNIP.)
      then TPPS14 = .;

      *** Exclude LOS < 2;

      if LOS < 2 then TPPS14 = .;

      *** Exclude MDC 14;

      if MDC in (14) then TPPS14 = .;

      *** identify Stratum_OPEN   : OPEN APPROACH
	               Stratum_NONOPEN: NON-OPEN APPROACH
          Among discharges otherwise qualifying for the denominator ;

      if TPPS14 ne . then do;
         if %MPR($ABDOMIPOPEN.) then TPPS14_OPEN = TPPS14;
         else if %MPR($ABDOMIPOTHER.) then TPPS14_NONOPEN = TPPS14;
      end;

   end;


 * ---------------------------------------------------------------------- ;
 * --- PSI 15 : ABDOMINOPELVIC ACCIDENTAL PUNCTURE OR LACERATION RATE --- ;
 * ---------------------------------------------------------------------- ;

  %MACRO PS15(FMT1,FMT2);
    PR1DAY = .;
    PR2DAY = .;
    PR1INDX = 0;
    MPRDAYCD = 0;
    %DO I = 1 %TO &NPR.;
       if put(PR&I.,&FMT1.) = '1' and (PRDAY&I. gt .Z) then do;
          if PR1DAY le .Z then do;
             PR1DAY = PRDAY&I;
             PR1INDX = &I;
          end;
          else if PR1DAY > PRDAY&I. then do;
             PR1DAY = PRDAY&I;
             PR1INDX = &I;
         end;
       end;
    %END;

    %DO I = 1 %TO &NPR.;
       if put(PR&I.,&FMT2.) = '1' and (PRDAY&I. gt .Z) and (&I ne PR1INDX) then do;
          if PR2DAY le .Z then do;
             PR2DAY = PRDAY&I;
          end;
          else if PR2DAY < PRDAY&I. then do;
             PR2DAY = PRDAY&I;
          end;
       end;
    %END;

    if (PR2DAY gt .Z) and (PR1DAY gt .Z) and (PR2DAY > PR1DAY) then MPRDAYCD = 1;

  %MEND;

   if (MEDICDR or SURGIDR) and %MPR($ABDOMI15P.) then do;

      TPPS15 = 0; QPPS15 = 0;

      if %MDX2($TECHNI15D.) then do;
        %PS15($ABDOMI15P.,$ABDOMI15P.);
        if MPRDAYCD = 1 then TPPS15 = 1;
      end;

      *** Exclude principal diagnosis;

      if %MDX1($TECHNI15D.) then TPPS15 = .;
      if %MDX2Q2($TECHNI15D.) then QPPS15 = 1;

      *** Exclude MDC 14;

      if MDC IN (14) then TPPS15 = .;

      *** Determine numerator exclusion for secondary Dx POA;

      if TPPS15 = . then QPPS15 = .;

   end;


 * ------------------------------------------------------------------------- ;
 * --- PSI 18 : OBSTETRIC TRAUMA RATE - VAGINAL DELIVERY WITH INSTRUMENT --- ;
 * ------------------------------------------------------------------------- ;

   if %MDX($DELOCMD.) and %MPR($VAGDELP.) and %MPR($INSTRIP.)
   then do;

      TPPS18 = 0;

      if %MDX($OBTRAID.) then TPPS18 = 1;

   end;


 * ---------------------------------------------------------------------------- ;
 * --- PSI 19 : OBSTETRIC TRAUMA RATE - VAGINAL DELIVERY WITHOUT INSTRUMENT --- ;
 * ---------------------------------------------------------------------------- ;

   if %MDX($DELOCMD.) and %MPR($VAGDELP.) and not %MPR($INSTRIP.)
   then do;

      TPPS19 = 0;

      if %MDX($OBTRAID.) then TPPS19 = 1;

   end;


 * -------------------------------------------------------------- ;
 * --- IDENTIFY TRANSFERS --------------------------------------- ;
 * -------------------------------------------------------------- ;

   * --- Flag transfers from another acute care hospital---------- ;
   if ASOURCE in (2) or POINTOFORIGINUB04 in ('4') then TRNSFER = 1;
   else  TRNSFER = 0;

 * -------------------------------------------------------------- ;
 * --- LABELS --------------------------------------------------- ;
 * -------------------------------------------------------------- ;
   label
    TRNSFER  = 'Transfer from Another Acute Care Hospital';


 * --- Sync QPPS, TPPS and PPPS values ---------- ;


    if QPPS03 in (.,1) then TPPS03 = .;
    if QPPS05 in (.,1) then TPPS05 = .;
    if QPPS06 in (.,1) then TPPS06 = .;
    if QPPS07 in (.,1) then TPPS07 = .;
    if QPPS08 in (.,1) then TPPS08 = .;
    if QPPS09 in (.,1) then TPPS09 = .;
    if QPPS10 in (.,1) then TPPS10 = .;
    if QPPS11 in (.,1) then TPPS11 = .;
    if QPPS12 in (.,1) then TPPS12 = .;
    if QPPS13 in (.,1) then TPPS13 = .;
    if QPPS15 in (.,1) then TPPS15 = .;

 * ---------------------------------------------------------------------------------- ;
 *---- ASSIGN DENOMINATORS FOR HOSPITAL_LEVEL PSIs BASED ON NON-MISSING NUMERATOR --- ;
 * ---------------------------------------------------------------------------------- ;

 array TPPS TPPS02-TPPS04 TPPS04_DVT_PE TPPS04_PNEUMONIA TPPS04_SEPSIS TPPS04_SHOCK TPPS04_GIHEMORRHAGE TPPS05-TPPS14 TPPS14_OPEN TPPS14_NONOPEN TPPS15 TPPS18 TPPS19;
 array PPPS 3 PPPS02-PPPS04 PPPS04_DVT_PE PPPS04_PNEUMONIA PPPS04_SEPSIS PPPS04_SHOCK PPPS04_GIHEMORRHAGE PPPS05-PPPS14 PPPS14_OPEN PPPS14_NONOPEN PPPS15 PPPS18 PPPS19;

 do over TPPS;
    if TPPS ge 0 then PPPS = 1;
    else PPPS = .;
 end;

run;

 * ------------------------------------------------------- ;
 * --- CONTENTS AND MEANS OF MEASURES OUTPUT FILE      --- ;
 * ------------------------------------------------------- ;

proc contents data=OUTMSR.&OUTFILE_MEAS. position;
run;

proc print data=OUTMSR.&OUTFILE_MEAS. (obs=24);
title4 "FIRST 24 RECORDS IN OUTPUT data set &OUTFILE_MEAS.";
run;

*----- TO PRINT VARIABLE LABELS COMMENT (DELETE) "NOLABELS" FROM proc means STATEMENTS --- ;

proc means data = OUTMSR.&OUTFILE_MEAS.(keep = DRG MDC MDRG YEAR DQTR
         AGECAT SEXCAT RACECAT PAYCAT LOS) n nmiss min max nolabels ;
     title4 "CATEGORICAL VARIABLES AND RANGES OF VALUES";
run; quit;

proc means data = OUTMSR.&OUTFILE_MEAS. n nmiss sum mean  nolabels ;
     var TPPS02-TPPS04  TPPS04_DVT_PE TPPS04_PNEUMONIA TPPS04_SEPSIS TPPS04_SHOCK TPPS04_GIHEMORRHAGE TPPS05-TPPS14 TPPS14_OPEN TPPS14_NONOPEN TPPS15 TPPS18 TPPS19;
    * OUTPUT OUT=STATS2;
     title "PATIENT SAFETY INDICATOR NUMERATOR FOR HOSPITAL LEVEL INDICATORS (=SUM),DENOMINATOR (=N), AND OBSERVED RATE (MEAN) ";
run; quit;

proc means data = OUTMSR.&OUTFILE_MEAS. n nmiss sum mean nolabels ;
     var TPPS02-TPPS04  TPPS04_DVT_PE TPPS04_PNEUMONIA TPPS04_SEPSIS TPPS04_SHOCK TPPS04_GIHEMORRHAGE TPPS05-TPPS14 TPPS14_OPEN TPPS14_NONOPEN TPPS15 ;
    * OUTPUT OUT=STATS3;
     title1 "PATIENT SAFETY INDICATOR NUMERATOR FOR HOSPITAL LEVEL INDICATORS (=SUM),DENOMINATORS (=N), AND OBSERVED RATE (MEAN) ";
     title2 "WITH POA MODIFIERS";
run; quit;

proc freq data = OUTMSR.&OUTFILE_MEAS.;
     tables TPPS04_DVT_PE*TPPS04_PNEUMONIA*TPPS04_SEPSIS*TPPS04_SHOCK*TPPS04_GIHEMORRHAGE
            TPPS04_DVT_PE*XPPS04_DVT_PE_ANY*XPPS04_DVT_PE_SEVERE
	          TPPS04_PNEUMONIA*XPPS04_PNEUMONIA_ANY*XPPS04_PNEUMONIA_SEVERE
	          TPPS04_SEPSIS*XPPS04_SEPSIS_ANY*XPPS04_SEPSIS_SEVERE
	          TPPS04_SHOCK*XPPS04_SHOCK_ANY*XPPS04_SHOCK_SEVERE
	          TPPS04_GIHEMORRHAGE*XPPS04_GIHEMORRHAGE_ANY*XPPS04_GIHEMORRHAGE_SEVERE/missing list;
run;
