*===================================================================;
*  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  <<<
*
*===================================================================;
FILENAME CONTROL "C:\PATHNAME\CONTROL.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;
 OUTPUT OUT=&OUTFILP2.
        SUM (TPPS01-TPPS20) = TPPS01-TPPS20
        N   (TPPS01-TPPS20) = PPPS01-PPPS20
        MEAN(TPPS01-TPPS20) = OPPS01-OPPS20;
 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 (Numer)'
 TPPS06 = 'IATROGENIC PNEUMOTHORAX      (Numerator)'
 TPPS07 = 'INFECTION DUE TO MEDICAL CARE (Numeratr)'
 TPPS08 = 'POSTOPERATIVE HIP FRACTURE   (Numerator)'
 %IF &PRDAY. = 1 %THEN %DO;
 TPPS09 = 'POSTOP HEMORRHAGE OR HEMATOMA (Numeratr)'
 %END;
 %ELSE %DO;
 TPPS09 = 'POSTOP HEMOR OR HEMAT-NO PRDAY (Numertr)'
 %END;
 TPPS10 = 'POSTOP PHYSIO METABOL DERANGMNT (Numrtr)'
 TPPS11 = 'POSTOP RESPIRATORY FAILURE   (Numerator)'
 %IF &PRDAY. = 1 %THEN %DO;
 TPPS12 = 'POSTOPERATIVE PE OR DVT      (Numerator)'
 %END;
 %ELSE %DO;
 TPPS12 = 'POSTOP PE OR DVT-NO PRDAY    (Numerator)'
 %END;
 TPPS13 = 'POSTOPERATIVE SEPSIS         (Numerator)'
 TPPS14 = 'POSTOPERATIVE WOUND DEHISCENCE (Numertr)'
 TPPS15 = 'ACCIDENTAL PUNCTURE/LACERATION(Numeratr)'
 TPPS16 = 'TRANSFUSION REACTION         (Numerator)'
 TPPS17 = 'BIRTH TRAUMA                 (Numerator)'
 TPPS18 = 'OB TRAUMA - VAGINAL W INSTRUMENT (Numer)'
 TPPS19 = 'OB TRAUMA - VAGINAL WO INSTRUMENT (Numr)'
 TPPS20 = 'OB TRAUMA - C-SECTION        (Numerator)'
 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)'
 %IF &PRDAY. = 1 %THEN %DO;
 PPPS09 = 'POSTOP HEMORRHAGE OR HEMATOMA      (Pop)'
 %END;
 %ELSE %DO;
 PPPS09 = 'POSTOP HEMOR OR HEMAT-NO PRDAY     (Pop)'
 %END;
 PPPS10 = 'POSTOP PHYSIO METABOL DERANGMNT    (Pop)'
 PPPS11 = 'POSTOP RESPIRATORY FAILURE         (Pop)'
 %IF &PRDAY. = 1 %THEN %DO;
 PPPS12 = 'POSTOP PE OR DVT-NO PRDAY          (Pop)'
 %END;
 %ELSE %DO;
 PPPS12 = 'POSTOPERATIVE PE OR DVT            (Pop)'
 %END;
 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 WO INSTRUMENT  (Pop)'
 PPPS20 = 'OB TRAUMA - 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 (Obsrv)'
 OPPS06 = 'IATROGENIC PNEUMOTHORAX       (Observed)'
 OPPS07 = 'INFECTION DUE TO MEDICAL CARE (Observed)'
 OPPS08 = 'POSTOPERATIVE HIP FRACTURE    (Observed)'
 %IF &PRDAY. = 1 %THEN %DO;
 OPPS09 = 'POSTOP HEMORRHAGE OR HEMATOMA (Observed)'
 %END;
 %ELSE %DO;
 OPPS09 = 'POSTOP HEMOR OR HEMAT-NO PRDAY (Observd)'
 %END;
 OPPS10 = 'POSTOP PHYSIO METABOL DERANGMNT    (Obs)'
 OPPS11 = 'POSTOP RESPIRATORY FAILURE    (Observed)'
 %IF &PRDAY. = 1 %THEN %DO;
 OPPS12 = 'POSTOPERATIVE PE OR DVT       (Observed)'
 %END;
 %ELSE %DO;
 OPPS12 = 'POSTOP PE OR DVT-NO PRDAY     (Observed)'
 %END;
 OPPS13 = 'POSTOPERATIVE SEPSIS          (Observed)'
 OPPS14 = 'POSTOPERATIVE WOUND DEHISCENCE (Observd)'
 OPPS15 = 'ACCIDENTAL PUNCTURE/LACERATION(Observed)'
 OPPS16 = 'TRANSFUSION REACTION          (Observed)'
 OPPS17 = 'BIRTH TRAUMA                  (Observed)'
 OPPS18 = 'OB TRAUMA - VAGINAL W INSTRUMENT (Obsrv)'
 OPPS19 = 'OB TRAUMA - VAGINAL WO INSTRUMENT (Obsr)'
 OPPS20 = 'OB TRAUMA - C-SECTION         (Observed)'
 _TYPE_   ='STRATIFICATION LEVEL                    '
;

 %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);


 %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" ","
 "PPPS01" "," "PPPS02" "," "PPPS03" "," "PPPS04" ","
 "PPPS05" "," "PPPS06" "," "PPPS07" "," "PPPS08" ","
 "PPPS09" "," "PPPS10" "," "PPPS11" "," "PPPS12" ","
 "PPPS13" "," "PPPS14" "," "PPPS15" "," "PPPS16" ","
 "PPPS17" "," "PPPS18" "," "PPPS19" "," "PPPS20" ","
 "OPPS01" "," "OPPS02" "," "OPPS03" "," "OPPS04" ","
 "OPPS05" "," "OPPS06" "," "OPPS07" "," "OPPS08" ","
 "OPPS09" "," "OPPS10" "," "OPPS11" "," "OPPS12" ","
 "OPPS13" "," "OPPS14" "," "OPPS15" "," "OPPS16" ","
 "OPPS17" "," "OPPS18" "," "OPPS19" "," "OPPS20"
 ;

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

 %END;

 %MEND TEXTP2;

 %TEXTP2;
