*===================================================================;
*  Title:  PROGRAM P2  CALCULATE OBSERVED PROVIDER RATES FOR AHRQ
*          PATIENT SAFETY INDICATORS
*
*  Description:;
*         USES PROC SUMMARY TO CALCULATE OBSERVED RATES FOR
*         PATIENT SAFETY INDICATORS ACROSS STRATIFIERS.
*         PROGRAM USES HOSPID, AGECAT, SEXCAT, PAYCAT AND RACECAT.
*
*          >>>  VERSION 2.1 (revision 3), January 2005 <<<
*
*===================================================================;
FILENAME CONTROL "C:\PATHNAME\CONTROL_PSI.SAS"; *<==USER MUST modify;

%INCLUDE CONTROL;

 TITLE2 'PROGRAM: P2';
 TITLE3 'AHRQ PATIENT SAFETY INDICATORS: CALCULATE OBSERVED PROVIDER RATES';

 * -------------------------------------------------------------- ;
 * --- PROVIDER LEVEL MEANS ON INPATIENT DATA FILE  ------------- ;
 * -------------------------------------------------------------- ;

 PROC   SUMMARY DATA=INP2.&INFILEP2.;
 CLASS  HOSPID AGECAT SEXCAT PAYCAT RACECAT;
 VAR    TPPS01-TPPS20 TPPS27-TPPS29;
 OUTPUT OUT=&OUTFILP2.
        SUM (TPPS01-TPPS20 TPPS27-TPPS29) = TPPS01-TPPS20 TPPS27-TPPS29
        N   (TPPS01-TPPS20 TPPS27-TPPS29) = PPPS01-PPPS20 PPPS27-PPPS29
        MEAN(TPPS01-TPPS20 TPPS27-TPPS29) = OPPS01-OPPS20 OPPS27-OPPS29;
 RUN;

 PROC SORT DATA=&OUTFILP2.;
 BY HOSPID AGECAT SEXCAT PAYCAT RACECAT;
 RUN;

 DATA   OUTP2.&OUTFILP2.;
 SET         &OUTFILP2. (WHERE=(_TYPE_ &TYPELVLP))
;

 %MACRO LBL;

 LABEL
 TPPS01 = 'COMPLICATIONS OF ANESTHESIA (Numerator)'
 TPPS02 = 'DEATH IN LOW MORTALITY DRGS (Numerator)'
 TPPS03 = 'DECUBITUS ULCER (Numerator)'
 TPPS04 = 'FAILURE TO RESCUE (Numerator)'
 TPPS05 = 'FOREIGN BODY LEFT IN DURING PROC (Num)'
 TPPS06 = 'IATROGENIC PNEUMOTHORAX (Numerator)'
 TPPS07 = 'INFECTION DUE TO MEDICAL CARE (Num)'
 TPPS08 = 'POSTOPERATIVE HIP FRACTURE (Numerator)'
 TPPS09 = 'POSTOP HEMORRHAGE OR HEMATOMA (Num)'
 TPPS10 = 'POSTOP PHYSIO METABOL DERANGEMENT (Num)'
 TPPS11 = 'POSTOP RESPIRATORY FAILURE (Numerator)'
 TPPS12 = 'POSTOPERATIVE PE OR DVT (Numerator)'
 TPPS13 = 'POSTOPERATIVE SEPSIS (Numerator)'
 TPPS14 = 'POSTOPERATIVE WOUND DEHISCENCE (Num)'
 TPPS15 = 'ACCIDENTAL PUNCTURE/LACERATION(Num)'
 TPPS16 = 'TRANSFUSION REACTION (Numerator)'
 TPPS17 = 'BIRTH TRAUMA - INJURY TO NEONATE (Num)'
 TPPS18 = 'OB TRAUMA - VAGINAL W INSTRUMENT (Num)'
 TPPS19 = 'OB TRAUMA - VAGINAL W/O INSTRUMENT (Num)'
 TPPS20 = 'OB TRAUMA - C-SECTION (Numerator)'
 TPPS27 = 'OB TRAUMA W 3 DEG - VAGINAL W INST (Num)'
 TPPS28 = 'OB TRAUMA W 3 DEG - VAGINAL W/O INST (Num)'
 TPPS29 = 'OB TRAUMA W 3 DEG - C-SECTION (Num)'
 PPPS01 = 'COMPLICATIONS OF ANESTHESIA (Pop)'
 PPPS02 = 'DEATH IN LOW MORTALITY DRGS (Pop)'
 PPPS03 = 'DECUBITUS ULCER (Pop)'
 PPPS04 = 'FAILURE TO RESCUE (Pop)'
 PPPS05 = 'FOREIGN BODY LEFT IN DURING PROC (Pop)'
 PPPS06 = 'IATROGENIC PNEUMOTHORAX (Pop)'
 PPPS07 = 'INFECTION DUE TO MEDICAL CARE (Pop)'
 PPPS08 = 'POSTOPERATIVE HIP FRACTURE (Pop)'
 PPPS09 = 'POSTOP HEMORRHAGE OR HEMATOMA (Pop)'
 PPPS10 = 'POSTOP PHYSIO METABOL DERANGEMENT (Pop)'
 PPPS11 = 'POSTOP RESPIRATORY FAILURE (Pop)'
 PPPS12 = 'POSTOP PE OR DVT-NO PRDAY (Pop)'
 PPPS13 = 'POSTOPERATIVE SEPSIS (Pop)'
 PPPS14 = 'POSTOPERATIVE WOUND DEHISCENCE (Pop)'
 PPPS15 = 'ACCIDENTAL PUNCTURE/LACERATION (Pop)'
 PPPS16 = 'TRANSFUSION REACTION (Pop)'
 PPPS17 = 'BIRTH TRAUMA (Pop)'
 PPPS18 = 'OB TRAUMA - VAGINAL W INSTRUMENT (Pop)'
 PPPS19 = 'OB TRAUMA - VAGINAL W/O INSTRUMENT (Pop)'
 PPPS20 = 'OB TRAUMA - C-SECTION (Pop)'
 PPPS27 = 'OB TRAUMA W 3 DEG- VAGINAL W INST (Pop)'
 PPPS28 = 'OB TRAUMA W 3 DEG- VAGINAL W/O INST (Pop)'
 PPPS29 = 'OB TRAUMA W 3 DEG- C-SECTION (Pop)'
 OPPS01 = 'COMPLICATIONS OF ANESTHESIA (Observed)'
 OPPS02 = 'DEATH IN LOW MORTALITY DRGS (Observed)'
 OPPS03 = 'DECUBITUS ULCER (Observed)'
 OPPS04 = 'FAILURE TO RESCUE (Observed)'
 OPPS05 = 'FOREIGN BODY LEFT IN DURING PROC (Obs)'
 OPPS06 = 'IATROGENIC PNEUMOTHORAX (Observed)'
 OPPS07 = 'INFECTION DUE TO MEDICAL CARE (Observed)'
 OPPS08 = 'POSTOPERATIVE HIP FRACTURE (Observed)'
 OPPS09 = 'POSTOP HEMORRHAGE OR HEMATOMA (Observed)'
 OPPS10 = 'POSTOP PHYSIO METABOL DERANGEMENT (Obs)'
 OPPS11 = 'POSTOP RESPIRATORY FAILURE (Observed)'
 OPPS12 = 'POSTOPERATIVE PE OR DVT (Observed)'
 OPPS13 = 'POSTOPERATIVE SEPSIS (Observed)'
 OPPS14 = 'POSTOPERATIVE WOUND DEHISCENCE (Obs)'
 OPPS15 = 'ACCIDENTAL PUNCTURE/LACERATION(Observed)'
 OPPS16 = 'TRANSFUSION REACTION (Observed)'
 OPPS17 = 'BIRTH TRAUMA (Observed)'
 OPPS18 = 'OB TRAUMA - VAGINAL W INSTRUMENT (Obs)'
 OPPS19 = 'OB TRAUMA - VAGINAL WO INSTRUMENT (Obs)'
 OPPS20 = 'OB TRAUMA - C-SECTION (Observed)'
 OPPS27 = 'OB TRAUMA W 3 DEG- VAGINAL W INST (Obs)'
 OPPS28 = 'OB TRAUMA W 3 DEG- VAGINAL W/O INST (Obs)'
 OPPS29 = 'OB TRAUMA W 3 DEG- C-SECTION (Observed)'
 _TYPE_ = 'STRATIFICATION LEVEL '
 ;

 * -------------------------------------------------------------- ;
 * --- RE-LABEL DAY DEPENDENT INDICATORS ------------------------ ;
 * -------------------------------------------------------------- ;
 %IF &PRDAY. = 0 %THEN %DO;
 LABEL
   TPPS08 = 'POSTOP HIP FRACTURE-NO PRDAY (Num)'
   TPPS09 = 'POSTOP HEMOR OR HEMAT-NO PRDAY (Num)'
   TPPS10 = 'POSTOP PHYSIO METABO DE-NO PRDAY (Num)'
   TPPS11 = 'POSTOP RESP FAILURE-NO PRDAY (Numerator)'
   TPPS12 = 'POSTOP PE OR DVT-NO PRDAY (Numerator)'
   TPPS14 = 'POSTOP WOUND DEHISCENCE-NO PRDAY (Num)'
   PPPS08 = 'POSTOP HIP FRACTURE-NO PRDAY (Pop)'
   PPPS09 = 'POSTOP HEMOR OR HEMAT-NO PRDAY (Pop)'
   PPPS10 = 'POSTOP PHYSIO METABO DE-NO PRDAY (Pop)'
   PPPS11 = 'POSTOP RESP FAILURE-NO PRDAY (Pop)'
   PPPS12 = 'POSTOP PE OR DVT-NO PRDAY (Pop)'
   PPPS14 = 'POSTOP WOUND DEHISCENCE-NO PRDAY (Pop)'
   OPPS08 = 'POSTOP HIP FRACTURE-NO PRDAY (Observed)'
   OPPS09 = 'POSTOP HEMOR OR HEMAT-NO PRDAY (Observd)'
   OPPS10 = 'POSTOP PHYSIO METABO DE-NO PRDAY (Obs)'
   OPPS11 = 'POSTOP RESP FAILURE-NO PRDAY (Observed)'
   OPPS12 = 'POSTOP PE OR DVT-NO PRDAY (Observed)'
   OPPS14 = 'POSTOP WOUND DEHISCENCE-NO PRDAY (Obs)'
 ;
 %END;

 %MEND;

 %LBL;

 DROP _FREQ_ ;

 RUN;

 * -------------------------------------------------------------- ;
 * --- CONTENTS AND MEANS OF PROVIDER OBSERVED MEANS FILE ------- ;
 * -------------------------------------------------------------- ;

 PROC   CONTENTS DATA=OUTP2.&OUTFILP2. POSITION;
 RUN;

 PROC   MEANS DATA=OUTP2.&OUTFILP2. (WHERE=(_TYPE_ IN(16))) N NMISS MIN MAX MEAN SUM;
 TITLE4 "SUMMARY OF PROVIDER-LEVEL RATES (_TYPE_=16)";
 RUN;



 * -------------------------------------------------------------- ;
 * --- PRINT PROVIDER OBSERVED MEANS FILE ----------------------- ;
 * -------------------------------------------------------------- ;

  %MACRO PRT2;

 %IF &PRINT. = 1 %THEN %DO;

 %MACRO PRT(PS,TEXT);

 PROC   PRINT DATA=OUTP2.&OUTFILP2. LABEL SPLIT='*';
 VAR    HOSPID AGECAT SEXCAT PAYCAT RACECAT TPPS&PS. PPPS&PS. OPPS&PS. ;
 LABEL HOSPID  = "HOSPID"
       AGECAT  = "AGECAT"
       SEXCAT  = "SEXCAT"
       PAYCAT  = "PAYCAT"
       RACECAT = "RACECAT"
       TPPS&PS.   = "TPPS&PS.*(Numerator)"
       PPPS&PS.   = "PPPS&PS.*(Denominator)"
       OPPS&PS.   = "OPPS&PS.*(Observed)"
       ;
 FORMAT TPPS&PS. PPPS&PS. 13.0 OPPS&PS. 8.6;
 FORMAT AGECAT $AGECAT.
        SEXCAT $SEXCAT.
        PAYCAT $PAYCAT.
        RACECAT $RACECAT.
   ;
 TITLE4 "Indicator # &PS.: &TEXT";
 RUN;

 %MEND PRT;

 %PRT(01,COMPLICATIONS OF ANESTHESIA);
 %PRT(02,DEATH IN LOW MORTALITY DRGS);
 %PRT(03,DECUBITUS ULCER);
 %PRT(04,FAILURE TO RESCUE);
 %PRT(05,FOREIGN BODY LEFT IN DURING PROCEDURE);
 %PRT(06,IATROGENIC PNEUMOTHORAX);
 %PRT(07,INFECTION DUE TO MEDICAL CARE);
 %PRT(08,POST-OPERATIVE HIP FRACTURE);
 %IF &PRDAY. = 1 %THEN %DO;
 %PRT(09,POST-OPERATIVE HEMORRHAGE OR HEMATOMA);
 %END;
 %ELSE %DO;
 %PRT(09,POST-OPERATIVE HEMORRHAGE OR HEMATOMA-NO PRDAY);
 %END;
 %PRT(10,POST-OPERATIVE PHYSIOLOGIC AND METABOLIC DERANGEMENTS);
 %PRT(11,POST-OPERATIVE RESPIRATORY FAILURE);
 %IF &PRDAY. = 1 %THEN %DO;
 %PRT(12,POST-OPERATIVE PULMONARY EMBOLISM OR DEEP VEIN THROMBOSIS);
 %END;
 %ELSE %DO;
 %PRT(12,POST-OPERATIVE PULMONARY EMBOLISM OR DEEP VEIN THROMBOSIS-NO PRDAY);
 %END;
 %PRT(13,POST-OPERATIVE SEPSIS);
 %PRT(14,POST-OPERATIVE WOUND DEHISCENCE);
 %PRT(15,ACCIDENTAL PUNCTURE/LACERATION);
 %PRT(16,TRANSFUSION REACTION);
 %PRT(17,BIRTH TRAUMA);
 %PRT(18,OBSTETRIC TRAUMA - VAGINAL WITH INSTRUMENT);
 %PRT(19,OBSTETRIC TRAUMA - VAGINAL WITHOUT INSTRUMENT);
 %PRT(20,OBSTETRIC TRAUMA - CESAREAN SECTION);
 %PRT(27,OBSTETRIC TRAUMA W 3 DEG - VAGINAL WITH INSTRUMENT);
 %PRT(28,OBSTETRIC TRAUMA W 3 DEG  - VAGINAL WITHOUT INSTRUMENT);
 %PRT(29,OBSTETRIC TRAUMA W 3 DEG  - CESAREAN SECTION);

 %END;

 %MEND PRT2;

 %PRT2;



 * -------------------------------------------------------------- ;
 * --- WRITE SAS OUTPUT DATA SET TO COMMA-DELIMITED TEXT FILE --- ;
 * --- FOR EXPORT INTO SPREADSHEETS ----------------------------- ;
 * -------------------------------------------------------------- ;

 %MACRO TEXTP2;

 %IF &TEXTP2. = 1  %THEN %DO;

 DATA _NULL_;
   SET OUTP2.&OUTFILP2.;
 FILE PSTEXTP2 LRECL=1000;
 IF _N_=1 THEN PUT "Hosp Id"  "," "Age"  "," "Sex"  "," "Payer" "," "Race"  "," "Type" ","
 "TPPS01" "," "TPPS02" "," "TPPS03" "," "TPPS04" ","
 "TPPS05" "," "TPPS06" "," "TPPS07" "," "TPPS08" ","
 "TPPS09" "," "TPPS10" "," "TPPS11" "," "TPPS12" ","
 "TPPS13" "," "TPPS14" "," "TPPS15" "," "TPPS16" ","
 "TPPS17" "," "TPPS18" "," "TPPS19" "," "TPPS20" ","
 "TPPS27" "," "TPPS28" "," "TPPS29" "," 
 "PPPS01" "," "PPPS02" "," "PPPS03" "," "PPPS04" ","
 "PPPS05" "," "PPPS06" "," "PPPS07" "," "PPPS08" ","
 "PPPS09" "," "PPPS10" "," "PPPS11" "," "PPPS12" ","
 "PPPS13" "," "PPPS14" "," "PPPS15" "," "PPPS16" ","
 "PPPS17" "," "PPPS18" "," "PPPS19" "," "PPPS20" ","
 "PPPS27" "," "PPPS28" "," "PPPS29" "," 
 "OPPS01" "," "OPPS02" "," "OPPS03" "," "OPPS04" ","
 "OPPS05" "," "OPPS06" "," "OPPS07" "," "OPPS08" ","
 "OPPS09" "," "OPPS10" "," "OPPS11" "," "OPPS12" ","
 "OPPS13" "," "OPPS14" "," "OPPS15" "," "OPPS16" ","
 "OPPS17" "," "OPPS18" "," "OPPS19" "," "OPPS20" ","
 "OPPS27" "," "OPPS28" "," "OPPS29" 
 ;

 PUT HOSPID 13.  "," AGECAT $1.  "," SEXCAT $1. "," PAYCAT $1. "," RACECAT $1.  "," _TYPE_ 2.  ","
 (TPPS01-TPPS20 TPPS27-TPPS29) (7.0 ",")
  ","
 (PPPS01-PPPS20 PPPS27-PPPS29) (13.2 ",")
 ","
 (OPPS01-OPPS20 OPPS27-OPPS29) (12.10 ",")
 ;
 RUN;

 %END;

 %MEND TEXTP2;

 %TEXTP2;
