*===================================================================;
*  Title:  PROGRAM P2 CALCULATES 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 4.2, September 2010 <<<
*
*===================================================================;
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  ------------- ;
 * -------------------------------------------------------------- ;

 DATA   TEMP0;
 SET    IN1.&INFILE1.;
 
 IF NOPOA IN (0) THEN DO;

    IF QPPS03 IN (.,1) THEN TPPS03 = .;
    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 QPPS14 IN (.,1) THEN TPPS14 = .;
    IF QPPS15 IN (.,1) THEN TPPS15 = .;
    IF QPPS16 IN (.,1) THEN TPPS16 = .;

 END;

    IF QPPS05 IN (.,1) THEN TPPS05 = .;

 RUN;

 PROC   SUMMARY DATA=TEMP0;
 CLASS  HOSPID AGECAT SEXCAT PAYCAT RACECAT;
 VAR    TPPS02-TPPS16 TPPS18 TPPS19;
 OUTPUT OUT=&OUTFILP2.
        SUM (TPPS02-TPPS16 TPPS18 TPPS19) = TPPS02-TPPS16 TPPS18 TPPS19
        SUMWGT(TPPS02-TPPS16 TPPS18 TPPS19) = PPPS02-PPPS16 PPPS18 PPPS19
        MEAN(TPPS02-TPPS16 TPPS18 TPPS19) = OPPS02-OPPS16 OPPS18 OPPS19;
 WEIGHT DISCWT;
 RUN;

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

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

 %MACRO LBL;

 LABEL
 TPPS02 = 'DEATH IN LOW MORTALITY DRGS (Numerator)'
 TPPS03 = 'PRESSURE ULCER (Numerator)'
 TPPS04 = 'DEATH AMONG SURGICAL (Numerator)'
 TPPS05 = 'FOREIGN BODY LEFT IN DURING PROC (Num)'
 TPPS06 = 'IATROGENIC PNEUMOTHORAX (Numerator)'
 TPPS07 = 'CENTRAL LINE ASSOCIATED BSI (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)'
 TPPS18 = 'OB TRAUMA - VAGINAL W INSTRUMENT (Num)'
 TPPS19 = 'OB TRAUMA - VAGINAL W/O INSTRUMENT (Num)'
 PPPS02 = 'DEATH IN LOW MORTALITY DRGS (Pop)'
 PPPS03 = 'PRESSURE ULCER (Pop)'
 PPPS04 = 'DEATH AMONG SURGICAL (Pop)'
 PPPS05 = 'FOREIGN BODY LEFT IN DURING PROC (Pop)'
 PPPS06 = 'IATROGENIC PNEUMOTHORAX (Pop)'
 PPPS07 = 'CENTRAL LINE ASSOCIATED BSI (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)'
 PPPS18 = 'OB TRAUMA - VAGINAL W INSTRUMENT (Pop)'
 PPPS19 = 'OB TRAUMA - VAGINAL W/O INSTRUMENT (Pop)'
 OPPS02 = 'DEATH IN LOW MORTALITY DRGS (Observed)'
 OPPS03 = 'PRESSURE ULCER (Observed)'
 OPPS04 = 'DEATH AMONG SURGICAL (Observed)'
 OPPS05 = 'FOREIGN BODY LEFT IN DURING PROC (Obs)'
 OPPS06 = 'IATROGENIC PNEUMOTHORAX (Observed)'
 OPPS07 = 'CENTRAL LINE ASSOCIATED BSI (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)'
 OPPS18 = 'OB TRAUMA - VAGINAL W INSTRUMENT (Obs)'
 OPPS19 = 'OB TRAUMA - VAGINAL WO INSTRUMENT (Obs)'
 _TYPE_ = 'STRATIFICATION LEVEL '
 ;

 * -------------------------------------------------------------- ;
 * --- RE-LABEL DAY DEPENDENT INDICATORS ------------------------ ;
 * -------------------------------------------------------------- ;
 %IF &PRDAY. = 0 %THEN %DO;
 LABEL
   TPPS03 = 'PRESSURE ULCER-NO PRDAY (Numerator)'
   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)'
   PPPS03 = 'PRESSURE ULCER-NO PRDAY (Pop)'
   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)'
   OPPS03 = 'PRESSURE ULCER-NO PRDAY (Observed)'
   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 AGECAT AGECAT.
        SEXCAT SEXCAT.
        PAYCAT PAYCAT.
        RACECAT RACECAT.
        TPPS&PS. PPPS&PS. 13.0 OPPS&PS. 8.6;
 TITLE4 "Indicator # &PS.: &TEXT";
 RUN;

 %MEND PRT;

 %PRT(02,DEATH IN LOW MORTALITY DRGS);
 %PRT(03,PRESSURE ULCER);
 %PRT(04,DEATH AMONG SURGICAL);
 %PRT(05,FOREIGN BODY LEFT IN DURING PROCEDURE);
 %PRT(06,IATROGENIC PNEUMOTHORAX);
 %PRT(07,CENTRAL LINE ASSOCIATED BSI);
 %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(18,OBSTETRIC TRAUMA - VAGINAL WITH INSTRUMENT);
 %PRT(19,OBSTETRIC TRAUMA - VAGINAL WITHOUT INSTRUMENT);

 %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" ","
 "TPPS02" "," "TPPS03" "," "TPPS04" ","
 "TPPS05" "," "TPPS06" "," "TPPS07" "," "TPPS08" ","
 "TPPS09" "," "TPPS10" "," "TPPS11" "," "TPPS12" ","
 "TPPS13" "," "TPPS14" "," "TPPS15" "," "TPPS16" ","
 "TPPS18" "," "TPPS19" "," 
 "PPPS02" "," "PPPS03" "," "PPPS04" ","
 "PPPS05" "," "PPPS06" "," "PPPS07" "," "PPPS08" ","
 "PPPS09" "," "PPPS10" "," "PPPS11" "," "PPPS12" ","
 "PPPS13" "," "PPPS14" "," "PPPS15" "," "PPPS16" ","
 "PPPS18" "," "PPPS19" "," 
 "OPPS02" "," "OPPS03" "," "OPPS04" ","
 "OPPS05" "," "OPPS06" "," "OPPS07" "," "OPPS08" ","
 "OPPS09" "," "OPPS10" "," "OPPS11" "," "OPPS12" ","
 "OPPS13" "," "OPPS14" "," "OPPS15" "," "OPPS16" ","
 "OPPS18" "," "OPPS19" 
 ;

 PUT HOSPID 13. "," AGECAT 3. "," SEXCAT 3. "," PAYCAT 3. "," RACECAT 3. "," _TYPE_ 2.  ","
 (TPPS02-TPPS16 TPPS18 TPPS19) (7.0 ",")
  ","
 (PPPS02-PPPS16 PPPS18 PPPS19) (13.2 ",")
 ","
 (OPPS02-OPPS16 OPPS18 OPPS19) (12.10 ",")
 ;
 RUN;

 %END;

 %MEND TEXTP2;

 %TEXTP2;
