*===================================================================;
*  Title:  PROGRAM P3:  CALCULATES RISK-ADJUSTED PROVIDER
*          RATES FOR AHRQ PATIENT SAFETY INDICATORS
*
*  Description:
*         USES MCMC TO CALCULATE RISK-ADJUSTED RATES FOR
*         PATIENT SAFETY INDICATORS.
*         ADJUSTS FOR: SEX, AGE, DRG, MDC AND COMORBIDITY
*
*          >>>  VERSION 4.2, September 2010  <<<
*
*===================================================================;
FILENAME CONTROL "C:\PATHNAME\CONTROL_PSI.SAS"; *<==USER MUST modify;

%INCLUDE CONTROL;


TITLE2 'PROGRAM P3 PART I';
TITLE3 'AHRQ PATIENT SAFETY INDICATORS: CALCULATE RISK-ADJUSTED PROVIDER RATES';

* -------------------------------------------------------------- ;
* --- PSI ADJUSTED RATES  -------------------------------------- ;
* -------------------------------------------------------------- ;
* --- THIS STEP SELECTS THE OBSERVATIONS FROM THE PROGRAM 1    - ;
* --- OUTPUT FOR EACH PATIENT SAFETY INDICATOR IN TURN.        - ;
* --- EACH ITERATION PASSES VARIABLES THAT CONTROL THE         - ;
* --- COVARIATES FOR THAT PATIENT SAFETY INDICATOR:            - ;
* --- N - OBSERVATION NUMBER FROM THE COVAR FILE               - ;
* ---     ALSO IDENTIFIES THE FORMAT USED TO INDEX COVARIATES  - ;
* --- PS - THE PATIENT SAFETY INDICATOR NAME WITHOUT THE       - ;
* ---      PREFIX (P)                                          - ;
* --- Z,X - NUMBER OF COVARIATES                               - ;
* -------------------------------------------------------------- ;

 %LET COMOB = CHF      VALVE    PULMCIRC PERIVASC HTN_C
	  	  PARA     NEURO    CHRNLUNG DM       DMCX 
		  HYPOTHY  RENLFAIL LIVER    ULCER    AIDS 
		  LYMPH    METS     TUMOR    ARTH     OBESE
	  	  WGHTLOSS BLDLOSS  ANEMDEF  ALCOHOL  DRUG 
		  PSYCH    DEPRESS
 ;

 %LET XOMOB = XHF      XALVE    XULMCIRC XERIVASC XTN_C
              XARA     XEURO    XHRNLUNG XM       XMCX
              XYPOTHY  XENLFAIL XIVER    XLCER    XIDS
              XYMPH    XETS     XUMOR    XRTH     XBESE
              XGHTLOSS XLDLOSS  XNEMDEF  XLCOHOL  XRUG
              XSYCH    XEPRESS 
 ;

 %MACRO MOD3(N,PS,Z,X);

 * --- THIS SET CREATES TEMP1 WHICH CONTAINS THE DEPENDENT      - ;
 * --- VARIABLE (TPS) AND INDEPENDENT VARIABLES USED IN         - ;
 * --- REGRESSION                                               - ;

 DATA TEMP1
   (KEEP=KEY HOSPID AGECAT SEXCAT PAYCAT RACECAT 
        T&PS. YT&PS. P1 ZCV1-ZCV&Z. XCV1-XCV&X. DISCWT);
 LENGTH ZCV1-ZCV&Z. XCV1-XCV&X. P1 
        SIDX AIDX DIDX MIDX 3;
 SET IN1.&INFILE1.
   (KEEP=KEY HOSPID MDRG MDC
         POPCAT AGECAT SEXCAT PAYCAT RACECAT 
         T&PS. Q&PS. DISCWT &COMOB. &XOMOB.
         NOPOA NOPRDAY TRNSFER NOPOUB04);

 * --- KEEP ONLY DISCHARGES NEEDED FOR THIS PS ------------------ ;
 IF T&PS. NE .;

 * --- INITIALIZE COVARIATE DUMMIES TO ZERO --------------------- ;
 ARRAY ZCV{&Z.} ZCV1-ZCV&Z.;
 ARRAY XCV{&X.} XCV1-XCV&X.;
  
 DO I = 1 TO &Z.;
   ZCV(I) = 0;
 END;

 DO I = 1 TO &X.;
   XCV(I) = .;
   IF NOPOA IN (0) THEN XCV(I) = 0; 
 END;

 * --- SET INDEX FOR SEX, AGE, DRG AND MDC                   --- ;
 * --- CATEGORIES VARY BY PATIENT SAFETY INDICATOR AND ARE   --- ;
 * --- DETERMINED BY THE FORMAT CORRESPONDING TO N           --- ;

 SIDX = PUT(SEXCAT,S&N.PSFMT.);
 AIDX = PUT(POPCAT,A&N.PSFMT.);
 DIDX = PUT(MDRG,D&N.PSFMT.);
 MIDX = PUT(MDC,M&N.PSFMT.);

 * --- SET APPROPRIATE COVARIATE DUMMY TO 1 FOR THIS DISCHARGE  - ;
 IF SIDX > 0 THEN ZCV(SIDX) = 1; 
 IF AIDX > 0 THEN ZCV(AIDX) = 1; 
 IF DIDX > 0 THEN ZCV(DIDX) = 1; 
 IF DIDX = -1 AND MIDX > 0 THEN ZCV(MIDX) = 1; 

 IF NOPOA IN (0) THEN DO;
    IF SIDX > 0 THEN XCV(SIDX) = 1; 
    IF AIDX > 0 THEN XCV(AIDX) = 1; 
    IF DIDX > 0 THEN XCV(DIDX) = 1; 
    IF DIDX = -1 AND MIDX > 0 THEN XCV(MIDX) = 1; 
 END;

 ***************************************************************** ;

 IF &N. = 2 THEN DO;
    IF TRNSFER THEN ZCV15 = 1;
    IF NOPRDAY THEN ZCV16= 1;
    IF CHF THEN ZCV17 = 1;
    IF NEURO THEN ZCV18 = 1;
    IF CHRNLUNG THEN ZCV19 = 1;
    IF HYPOTHY THEN ZCV20 = 1;
    IF RENLFAIL THEN ZCV21 = 1;
    IF OBESE THEN ZCV22 = 1;
    IF ANEMDEF THEN ZCV23 = 1;
 END;

 IF &N. = 3 THEN DO;
    IF NOPOUB04 THEN ZCV110 = 1;
    IF NOPRDAY THEN ZCV111 = 1;
    IF HTN_C THEN ZCV112 = 1;
    IF NEURO THEN ZCV113 = 1;
    IF CHRNLUNG THEN ZCV114 = 1;
    IF DM THEN ZCV115 = 1;
    IF DMCX THEN ZCV116 = 1;
    IF HYPOTHY THEN ZCV117 = 1;
    IF RENLFAIL THEN ZCV118 = 1;
    IF AIDS THEN ZCV119 = 1;
    IF TUMOR THEN ZCV120 = 1;
    IF WGHTLOSS THEN ZCV121 = 1;
    IF ANEMDEF THEN ZCV122 = 1;
    IF DRUG THEN ZCV123 = 1;
    IF PSYCH THEN ZCV124 = 1;
    IF DEPRESS THEN ZCV125 = 1;
 END;

 IF &N. = 4 THEN DO;
    IF TRNSFER THEN ZCV71 = 1;
    IF CHF THEN ZCV72 = 1;
    IF PULMCIRC THEN ZCV73 = 1;
    IF PERIVASC THEN ZCV74 = 1;
    IF HTN_C THEN ZCV75 = 1;
    IF PARA THEN ZCV76 = 1;
    IF DMCX THEN ZCV77 = 1;
    IF HYPOTHY THEN ZCV78 = 1;
    IF RENLFAIL THEN ZCV79 = 1;
    IF LIVER THEN ZCV80 = 1;
    IF METS THEN ZCV81 = 1;
    IF WGHTLOSS THEN ZCV82 = 1;
    IF BLDLOSS THEN ZCV83 = 1;
    IF ANEMDEF THEN ZCV84 = 1;
    IF DRUG THEN ZCV85 = 1;
    IF PSYCH THEN ZCV86 = 1;
    IF DEPRESS THEN ZCV87 = 1; 
END;

 IF &N. = 6 THEN DO;
    IF NOPRDAY THEN ZCV15 = 1;
    IF HTN_C THEN ZCV16 = 1;
    IF NEURO THEN ZCV17 = 1;
    IF CHRNLUNG THEN ZCV18 = 1;
    IF DM THEN ZCV19 = 1;
    IF DMCX THEN ZCV20 = 1;
    IF METS THEN ZCV21 = 1;
    IF OBESE THEN ZCV22 = 1;
    IF WGHTLOSS THEN ZCV23 = 1;
    IF DRUG THEN ZCV24 = 1; 
END;

 IF &N. = 7 THEN DO;
    IF TRNSFER THEN ZCV76 = 1;
    IF NOPOUB04 THEN ZCV77 = 1;
    IF NOPRDAY THEN ZCV78 = 1;
    IF CHF THEN ZCV79 = 1;
    IF VALVE THEN ZCV80 = 1;
    IF HTN_C THEN ZCV81 = 1;
    IF PARA THEN ZCV82 = 1;
    IF NEURO THEN ZCV83 = 1;
    IF CHRNLUNG THEN ZCV84 = 1;
    IF RENLFAIL THEN ZCV85 = 1;
    IF LIVER THEN ZCV86 = 1;
    IF ARTH THEN ZCV87 = 1;
    IF OBESE THEN ZCV88 = 1;
    IF WGHTLOSS THEN ZCV89 = 1;
    IF BLDLOSS THEN ZCV90 = 1;
    IF ANEMDEF THEN ZCV91 = 1;
    IF DRUG THEN ZCV92 = 1; 
END;

 IF &N. = 8 THEN DO;
    ZCV2 = 0;
 END;

 IF &N. = 9 THEN DO;
    IF TRNSFER THEN ZCV61 = 1;
    IF NOPOUB04 THEN ZCV62 = 1;
    IF CHF THEN ZCV63 = 1;
    IF VALVE THEN ZCV64 = 1;
    IF PERIVASC THEN ZCV65 = 1;
    IF HTN_C THEN ZCV66 = 1;
    IF DM THEN ZCV67 = 1;
    IF RENLFAIL THEN ZCV68 = 1;
    IF LIVER THEN ZCV69 = 1;
    IF TUMOR THEN ZCV70 = 1;
    IF ANEMDEF THEN ZCV71 = 1;
    IF DRUG THEN ZCV72 = 1; 
END;

 IF &N. = 10 THEN DO;
    IF VALVE THEN ZCV11 = 1;
    IF OBESE THEN ZCV12 = 1;
    IF RENLFAIL THEN ZCV13 = 1;
    IF PERIVASC THEN ZCV14 = 1;
    IF CHF THEN ZCV15 = 1;
    IF LIVER THEN ZCV16 = 1;
    IF DMCX THEN ZCV17 = 1;
    IF HTN_C THEN ZCV18 = 1; 
END;

 IF &N. = 11 THEN DO;
    IF TRNSFER THEN ZCV67 = 1;
    IF NOPOUB04 THEN ZCV68 = 1;
    IF NOPRDAY THEN ZCV69 = 1;
    IF CHF THEN ZCV70 = 1;
    IF VALVE THEN ZCV71 = 1;
    IF PULMCIRC THEN ZCV72 = 1;
    IF HTN_C THEN ZCV73 = 1;
    IF PARA THEN ZCV74 = 1;
    IF NEURO THEN ZCV75 = 1;
    IF CHRNLUNG THEN ZCV76 = 1;
    IF DM THEN ZCV77 = 1;
    IF HYPOTHY THEN ZCV78 = 1;
    IF RENLFAIL THEN ZCV79 = 1;
    IF LIVER THEN ZCV80 = 1;
    IF METS THEN ZCV81 = 1;
    IF OBESE THEN ZCV82 = 1;
    IF WGHTLOSS THEN ZCV83 = 1;
    IF ANEMDEF THEN ZCV84 = 1;
    IF ALCOHOL THEN ZCV85 = 1;
    IF DRUG THEN ZCV86 = 1; 
END;

 IF &N. = 12 THEN DO;
    IF TRNSFER THEN ZCV85 = 1;
    IF CHF THEN ZCV86 = 1;
    IF VALVE THEN ZCV87 = 1;
    IF PULMCIRC THEN ZCV88 = 1;
    IF PERIVASC THEN ZCV89 = 1;
    IF HTN_C THEN ZCV90 = 1;
    IF PARA THEN ZCV91 = 1;
    IF NEURO THEN ZCV92 = 1;
    IF CHRNLUNG THEN ZCV93 = 1;
    IF DM THEN ZCV94 = 1;
    IF HYPOTHY THEN ZCV95 = 1;
    IF RENLFAIL THEN ZCV96 = 1;
    IF AIDS THEN ZCV97 = 1;
    IF LYMPH THEN ZCV98 = 1;
    IF METS THEN ZCV99 = 1;
    IF TUMOR THEN ZCV100 = 1;
    IF OBESE THEN ZCV101 = 1;
    IF WGHTLOSS THEN ZCV102 = 1;
    IF BLDLOSS THEN ZCV103 = 1;
    IF ANEMDEF THEN ZCV104 = 1;
    IF ALCOHOL THEN ZCV105 = 1;
    IF DRUG THEN ZCV106 = 1;
    IF PSYCH THEN ZCV107 = 1;
    IF DEPRESS THEN ZCV108 = 1; 
END;

 IF &N. = 13 THEN DO;
    IF TRNSFER THEN ZCV23 = 1;
    IF CHF THEN ZCV24 = 1;
    IF VALVE THEN ZCV25 = 1;
    IF PULMCIRC THEN ZCV26 = 1;
    IF HTN_C THEN ZCV27 = 1;
    IF PARA THEN ZCV28 = 1;
    IF CHRNLUNG THEN ZCV29 = 1;
    IF HYPOTHY THEN ZCV30 = 1;
    IF RENLFAIL THEN ZCV31 = 1;
    IF LIVER THEN ZCV32 = 1;
    IF OBESE THEN ZCV33 = 1;
    IF WGHTLOSS THEN ZCV34 = 1;
    IF ALCOHOL THEN ZCV35 = 1;
    IF DEPRESS THEN ZCV36 = 1; 
END;

 IF &N. = 14 THEN DO;
    IF CHF THEN ZCV10 = 1;
    IF HTN_C THEN ZCV11 = 1;
    IF CHRNLUNG THEN ZCV12 = 1;
    IF OBESE THEN ZCV13 = 1;
    IF WGHTLOSS THEN ZCV14 = 1;
    IF ALCOHOL THEN ZCV15 = 1; 
END;

 IF &N. = 15 THEN DO;
    IF TRNSFER THEN ZCV90 = 1;
    IF NOPRDAY THEN ZCV91 = 1;
    IF PERIVASC THEN ZCV92 = 1;
    IF DM THEN ZCV93 = 1;
    IF DMCX THEN ZCV94 = 1;
    IF RENLFAIL THEN ZCV95 = 1;
    IF OBESE THEN ZCV96 = 1;
    IF WGHTLOSS THEN ZCV97 = 1;
    IF BLDLOSS THEN ZCV98 = 1;
    IF ANEMDEF THEN ZCV99 = 1; 
END;

 ***************************************************************** ;

 IF NOPOA IN (0) THEN DO;

 IF &N. = 2 THEN DO;
    IF TRNSFER THEN XCV15 = 1;
    IF NOPRDAY THEN XCV16= 1;
    IF XHF THEN XCV17 = 1;
    IF XEURO THEN XCV18 = 1;
    IF XHRNLUNG THEN XCV19 = 1;
    IF XYPOTHY THEN XCV20 = 1;
    IF XENLFAIL THEN XCV21 = 1;
    IF XBESE THEN XCV22 = 1;
    IF XNEMDEF THEN XCV23 = 1;
 END;

 IF &N. = 3 THEN DO;
    IF NOPOUB04 THEN XCV110 = 1;
    IF NOPRDAY THEN XCV111 = 1;
    IF XTN_C THEN XCV112 = 1;
    IF XEURO THEN XCV113 = 1;
    IF XHRNLUNG THEN XCV114 = 1;
    IF XM THEN XCV115 = 1;
    IF XMCX THEN XCV116 = 1;
    IF XYPOTHY THEN XCV117 = 1;
    IF XENLFAIL THEN XCV118 = 1;
    IF XIDS THEN XCV119 = 1;
    IF XUMOR THEN XCV120 = 1;
    IF XGHTLOSS THEN XCV121 = 1;
    IF XNEMDEF THEN XCV122 = 1;
    IF XRUG THEN XCV123 = 1;
    IF XSYCH THEN XCV124 = 1;
    IF XEPRESS THEN XCV125 = 1;
 END;

 IF &N. = 4 THEN DO;
    IF TRNSFER THEN XCV71 = 1;
    IF XHF THEN XCV72 = 1;
    IF XULMCIRC THEN XCV73 = 1;
    IF XERIVASC THEN XCV74 = 1;
    IF XTN_C THEN XCV75 = 1;
    IF XARA THEN XCV76 = 1;
    IF XMCX THEN XCV77 = 1;
    IF XYPOTHY THEN XCV78 = 1;
    IF XENLFAIL THEN XCV79 = 1;
    IF XIVER THEN XCV80 = 1;
    IF XETS THEN XCV81 = 1;
    IF XGHTLOSS THEN XCV82 = 1;
    IF XLDLOSS THEN XCV83 = 1;
    IF XNEMDEF THEN XCV84 = 1;
    IF XRUG THEN XCV85 = 1;
    IF XSYCH THEN XCV86 = 1;
    IF XEPRESS THEN XCV87 = 1;
 END;

 IF &N. = 6 THEN DO;
    IF NOPRDAY THEN XCV15 = 1;
    IF XTN_C THEN XCV16 = 1;
    IF XEURO THEN XCV17 = 1;
    IF XHRNLUNG THEN XCV18 = 1;
    IF XM THEN XCV19 = 1;
    IF XMCX THEN XCV20 = 1;
    IF XETS THEN XCV21 = 1;
    IF XBESE THEN XCV22 = 1;
    IF XGHTLOSS THEN XCV23 = 1;
    IF XRUG THEN XCV24 = 1; 
END;

 IF &N. = 7 THEN DO;
    IF TRNSFER THEN XCV76 = 1;
    IF NOPOUB04 THEN XCV77 = 1;
    IF NOPRDAY THEN XCV78 = 1;
    IF XHF THEN XCV79 = 1;
    IF XALVE THEN XCV80 = 1;
    IF XTN_C THEN XCV81 = 1;
    IF XARA THEN XCV82 = 1;
    IF XEURO THEN XCV83 = 1;
    IF XHRNLUNG THEN XCV84 = 1;
    IF XENLFAIL THEN XCV85 = 1;
    IF XIVER THEN XCV86 = 1;
    IF XRTH THEN XCV87 = 1;
    IF XBESE THEN XCV88 = 1;
    IF XGHTLOSS THEN XCV89 = 1;
    IF XLDLOSS THEN XCV90 = 1;
    IF XNEMDEF THEN XCV91 = 1;
    IF XRUG THEN XCV92 = 1; 
END;

 IF &N. = 8 THEN DO;
    XCV2 = 0;
 END;

 IF &N. = 9 THEN DO;
    IF TRNSFER THEN XCV61 = 1;
    IF NOPOUB04 THEN XCV62 = 1;
    IF XHF THEN XCV63 = 1;
    IF XALVE THEN XCV64 = 1;
    IF XERIVASC THEN XCV65 = 1;
    IF XTN_C THEN XCV66 = 1;
    IF XM THEN XCV67 = 1;
    IF XENLFAIL THEN XCV68 = 1;
    IF XIVER THEN XCV69 = 1;
    IF XUMOR THEN XCV70 = 1;
    IF XNEMDEF THEN XCV71 = 1;
    IF XRUG THEN XCV72 = 1; 
END;

 IF &N. = 10 THEN DO;
    IF XALVE THEN XCV11 = 1;
    IF XBESE THEN XCV12 = 1;
    IF XENLFAIL THEN XCV13 = 1;
    IF XERIVASC THEN XCV14 = 1;
    IF XHF THEN XCV15 = 1;
    IF XIVER THEN XCV16 = 1;
    IF XMCX THEN XCV17 = 1;
    IF XTN_C THEN XCV18 = 1; 
END;

 IF &N. = 11 THEN DO;
    IF TRNSFER THEN XCV67 = 1;
    IF NOPOUB04 THEN XCV68 = 1;
    IF NOPRDAY THEN XCV69 = 1;
    IF XHF THEN XCV70 = 1;
    IF XALVE THEN XCV71 = 1;
    IF XULMCIRC THEN XCV72 = 1;
    IF XTN_C THEN XCV73 = 1;
    IF XARA THEN XCV74 = 1;
    IF XEURO THEN XCV75 = 1;
    IF XHRNLUNG THEN XCV76 = 1;
    IF XM THEN XCV77 = 1;
    IF XYPOTHY THEN XCV78 = 1;
    IF XENLFAIL THEN XCV79 = 1;
    IF XIVER THEN XCV80 = 1;
    IF XETS THEN XCV81 = 1;
    IF XBESE THEN XCV82 = 1;
    IF XGHTLOSS THEN XCV83 = 1;
    IF XNEMDEF THEN XCV84 = 1;
    IF XLCOHOL THEN XCV85 = 1;
    IF XRUG THEN XCV86 = 1; 
END;

 IF &N. = 12 THEN DO;
    IF TRNSFER THEN XCV85 = 1;
    IF XHF THEN XCV86 = 1;
    IF XALVE THEN XCV87 = 1;
    IF XULMCIRC THEN XCV88 = 1;
    IF XERIVASC THEN XCV89 = 1;
    IF XTN_C THEN XCV90 = 1;
    IF XARA THEN XCV91 = 1;
    IF XEURO THEN XCV92 = 1;
    IF XHRNLUNG THEN XCV93 = 1;
    IF XM THEN XCV94 = 1;
    IF XYPOTHY THEN XCV95 = 1;
    IF XENLFAIL THEN XCV96 = 1;
    IF XIDS THEN XCV97 = 1;
    IF XYMPH THEN XCV98 = 1;
    IF XETS THEN XCV99 = 1;
    IF XUMOR THEN XCV100 = 1;
    IF XBESE THEN XCV101 = 1;
    IF XGHTLOSS THEN XCV102 = 1;
    IF XLDLOSS THEN XCV103 = 1;
    IF XNEMDEF THEN XCV104 = 1;
    IF XLCOHOL THEN XCV105 = 1;
    IF XRUG THEN XCV106 = 1;
    IF XSYCH THEN XCV107 = 1;
    IF XEPRESS THEN XCV108 = 1; 
END;

 IF &N. = 13 THEN DO;
    IF TRNSFER THEN XCV23 = 1;
    IF XHF THEN XCV24 = 1;
    IF XALVE THEN XCV25 = 1;
    IF XULMCIRC THEN XCV26 = 1;
    IF XTN_C THEN XCV27 = 1;
    IF XARA THEN XCV28 = 1;
    IF XHRNLUNG THEN XCV29 = 1;
    IF XYPOTHY THEN XCV30 = 1;
    IF XENLFAIL THEN XCV31 = 1;
    IF XIVER THEN XCV32 = 1;
    IF XBESE THEN XCV33 = 1;
    IF XGHTLOSS THEN XCV34 = 1;
    IF XLCOHOL THEN XCV35 = 1;
    IF XEPRESS THEN XCV36 = 1; 
END;

 IF &N. = 14 THEN DO;
    IF XHF THEN XCV10 = 1;
    IF XTN_C THEN XCV11 = 1;
    IF XHRNLUNG THEN XCV12 = 1;
    IF XBESE THEN XCV13 = 1;
    IF XGHTLOSS THEN XCV14 = 1;
    IF XLCOHOL THEN XCV15 = 1; 
END;

 IF &N. = 15 THEN DO;
    IF TRNSFER THEN XCV90 = 1;
    IF NOPRDAY THEN XCV91 = 1;
    IF XERIVASC THEN XCV92 = 1;
    IF XM THEN XCV93 = 1;
    IF XMCX THEN XCV94 = 1;
    IF XENLFAIL THEN XCV95 = 1;
    IF XBESE THEN XCV96 = 1;
    IF XGHTLOSS THEN XCV97 = 1;
    IF XLDLOSS THEN XCV98 = 1;
    IF XNEMDEF THEN XCV99 = 1;
 END;

 END;

 ***************************************************************** ;

 YT&PS. = T&PS.;
 P1 = Q&PS.;

 IF &N. = 2 THEN DO;
    IF NOPOA IN (0) THEN P1 = 0;
    ELSE P1 = .;
 END;

 IF &N. = 4 THEN DO;
    IF NOPOA IN (0) THEN P1 = 0;
    ELSE P1 = .;
 END;

 RUN;

 %let dsid=%sysfunc(open(temp1));
 %let dnum=%sysfunc(attrn(&dsid,nobs));
 %let drc=%sysfunc(close(&dsid));

 %if &dnum ne 0 %then %do;

 %if &dnum gt 11000000 %then %do;

 DATA TEMP1;
 SET TEMP1;

 IDX = RANUNI(-1);
 IF IDX <= (11000000 / &dnum);

 RUN;

 %end;

 DATA TEMP1Y;
 LENGTH KEY HOSPID 8 YT&PS. P1 ZCV1-ZCV&Z. XCV1-XCV&X. 3;
 SET TEMP1
   (KEEP=KEY HOSPID
         YT&PS. P1 
         ZCV1-ZCV&Z. XCV1-XCV&X.);

 RUN;

 PROC SORT DATA=TEMP1Y;
 BY HOSPID KEY;
 RUN;

 PROC MEANS DATA=TEMP1Y N MEAN;
 VAR YT&PS. P1 ZCV1-ZCV&Z. XCV1-XCV&X.;
 RUN;

 DATA TEMP1W;
 SET TEMP1
   (KEEP=KEY HOSPID 
         AGECAT SEXCAT PAYCAT RACECAT
         T&PS. DISCWT);

 RUN;

 PROC SORT DATA=TEMP1W;
 BY HOSPID KEY;
 RUN;

 %INCLUDE PRDP;

 PROC SORT DATA=TEMP1I(KEEP=HOSPID KEY P P_X Y_X_P_0 Y_X_P_0_MCMC);
 BY HOSPID KEY;
 RUN;

 DATA TEMP1W;
 MERGE TEMP1W TEMP1I;
 BY HOSPID KEY;

 %INCLUDE MSXP;
 IF &N. =  2 THEN SUB_N = 1;
 IF &N. =  3 THEN SUB_N = 2;
 IF &N. =  4 THEN SUB_N = 3;
 IF &N. =  6 THEN SUB_N = 4;
 IF &N. =  7 THEN SUB_N = 5;
 IF &N. =  8 THEN SUB_N = 6;
 IF &N. =  9 THEN SUB_N = 7;
 IF &N. = 10 THEN SUB_N = 8;
 IF &N. = 11 THEN SUB_N = 9;
 IF &N. = 12 THEN SUB_N = 10;
 IF &N. = 13 THEN SUB_N = 11;
 IF &N. = 14 THEN SUB_N = 12;
 IF &N. = 15 THEN SUB_N = 13;

 EHAT = (ARRYP3(SUB_N) / (ARRYP3(SUB_N) - ARRYP4(SUB_N)) * Y_X_P_0_MCMC);

 PHAT = EHAT * (1 - EHAT);

 IF P = . THEN PWHT = (1 - P_X) * DISCWT;
 ELSE PWHT = (1 - P) * DISCWT;

 RUN;

 PROC SUMMARY DATA=TEMP1W;
 CLASS HOSPID AGECAT SEXCAT PAYCAT RACECAT;
 VAR T&PS. EHAT PHAT;
 OUTPUT OUT=R&PS. SUM(T&PS. EHAT PHAT)=T&PS. EHAT PHAT 
                  SUMWGT(T&PS.)=P&PS.;
 WEIGHT PWHT;
 RUN;

 DATA R&PS.(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT _TYPE_
                 E&PS. R&PS. L&PS. U&PS. S&PS. X&PS. V&PS.
                 T&PS. P&PS. O&PS.);
 SET R&PS.;
   
 IF _TYPE_ &TYPELVLP;

 %INCLUDE MSXP;
 IF &N. =  2 THEN SUB_N = 1;
 IF &N. =  3 THEN SUB_N = 2;
 IF &N. =  4 THEN SUB_N = 3;
 IF &N. =  6 THEN SUB_N = 4;
 IF &N. =  7 THEN SUB_N = 5;
 IF &N. =  8 THEN SUB_N = 6;
 IF &N. =  9 THEN SUB_N = 7;
 IF &N. = 10 THEN SUB_N = 8;
 IF &N. = 11 THEN SUB_N = 9;
 IF &N. = 12 THEN SUB_N = 10;
 IF &N. = 13 THEN SUB_N = 11;
 IF &N. = 14 THEN SUB_N = 12;
 IF &N. = 15 THEN SUB_N = 13;

 T&PS. = ROUND(T&PS.,1);
 P&PS. = ROUND(P&PS.,1);

 E&PS. = EHAT / P&PS.;
 O&PS.  = T&PS. / P&PS.;
 
 IF PHAT < 0 THEN DO;
    R&PS.   = .;
    SE&PS.  = .;
    V&PS.   = .;
    S&PS.   = .;
    X&PS.   = .;
 END; 
 ELSE IF _TYPE_ IN (0,16) THEN DO;
    R&PS.   = (O&PS. / E&PS.) * ARRYP3(SUB_N);
    SE&PS.  = (ARRYP3(SUB_N) / E&PS.) * (1 / P&PS.) * SQRT(PHAT);
    V&PS.   = SE&PS.**2;
    SN&PS.  = ARRYP2(SUB_N) / (ARRYP2(SUB_N) + V&PS.);
    S&PS.   = (R&PS. * SN&PS.) + ((1 -  SN&PS.) * ARRYP3(SUB_N));
    X&PS.   = SQRT(ARRYP2(SUB_N)- (SN&PS. * ARRYP2(SUB_N)));
 END;
 ELSE DO;
    R&PS.   = (O&PS. / E&PS.);
    SE&PS.  = (1 / E&PS.) * (1 / P&PS.) * SQRT(PHAT);
    V&PS.   = .;
    S&PS.   = .;
    X&PS.   = .;
 END;

 L&PS.   = R&PS. - (1.96 * SE&PS.);
 IF L&PS. < 0 THEN L&PS. = 0;
 U&PS.   = R&PS. + (1.96 * SE&PS.);
 IF U&PS. > 1 THEN U&PS. = 1;
 IF R&PS. > 1 THEN R&PS. = 1;

RUN;

 %end;
 %else %do;

 DATA R&PS.;
    HOSPID=.;AGECAT=.;SEXCAT=.;PAYCAT=.;RACECAT=.;_TYPE_=0;
    E&PS.=.;R&PS.=.;L&PS.=.;U&PS.=.;P&PS.=.;S&PS.=.;X&PS.=.;V&PS.=.;
    T&PS.=.;P&PS.=.;O&PS.=.; 
    OUTPUT;
 RUN;

 %end;

 PROC DATASETS NOLIST;
 DELETE TEMP1 TEMP1Y TEMP1W;
 RUN;

 %MEND;

 %MOD3(2,PPS02,23,23);
 %MOD3(3,PPS03,125,125);
 %MOD3(4,PPS04,87,87);
 %MOD3(6,PPS06,24,24);
 %MOD3(7,PPS07,92,92);
 %MOD3(8,PPS08,2,2);
 %MOD3(9,PPS09,72,72);
 %MOD3(10,PPS10,18,18);
 %MOD3(11,PPS11,86,86);
 %MOD3(12,PPS12,108,108);
 %MOD3(13,PPS13,36,36);
 %MOD3(14,PPS14,15,15);
 %MOD3(15,PPS15,99,99);

 * --- MERGES THE AREA ADJUSTED RATES FOR EACH PS. PREFIX FOR THE - ;
 * --- ADJUSTED RATES IS (R) AND EXPECTED RATES IS (E).           - ;

 DATA RISKADJ;
 MERGE RPPS02(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT EPPS02 RPPS02 LPPS02 UPPS02 SPPS02 XPPS02 VPPS02 TPPS02 PPPS02 OPPS02)
       RPPS03(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT EPPS03 RPPS03 LPPS03 UPPS03 SPPS03 XPPS03 VPPS03 TPPS03 PPPS03 OPPS03)
       RPPS04(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT EPPS04 RPPS04 LPPS04 UPPS04 SPPS04 XPPS04 VPPS04 TPPS04 PPPS04 OPPS04)
       RPPS06(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT EPPS06 RPPS06 LPPS06 UPPS06 SPPS06 XPPS06 VPPS06 TPPS06 PPPS06 OPPS06)
       RPPS07(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT EPPS07 RPPS07 LPPS07 UPPS07 SPPS07 XPPS07 VPPS07 TPPS07 PPPS07 OPPS07)
       RPPS08(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT EPPS08 RPPS08 LPPS08 UPPS08 SPPS08 XPPS08 VPPS08 TPPS08 PPPS08 OPPS08)
       RPPS09(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT EPPS09 RPPS09 LPPS09 UPPS09 SPPS09 XPPS09 VPPS09 TPPS09 PPPS09 OPPS09)
       RPPS10(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT EPPS10 RPPS10 LPPS10 UPPS10 SPPS10 XPPS10 VPPS10 TPPS10 PPPS10 OPPS10)
       RPPS11(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT EPPS11 RPPS11 LPPS11 UPPS11 SPPS11 XPPS11 VPPS11 TPPS11 PPPS11 OPPS11)
       RPPS12(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT EPPS12 RPPS12 LPPS12 UPPS12 SPPS12 XPPS12 VPPS12 TPPS12 PPPS12 OPPS12)
       RPPS13(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT EPPS13 RPPS13 LPPS13 UPPS13 SPPS13 XPPS13 VPPS13 TPPS13 PPPS13 OPPS13)
       RPPS14(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT EPPS14 RPPS14 LPPS14 UPPS14 SPPS14 XPPS14 VPPS14 TPPS14 PPPS14 OPPS14)
       RPPS15(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT EPPS15 RPPS15 LPPS15 UPPS15 SPPS15 XPPS15 VPPS15 TPPS15 PPPS15 OPPS15);
 BY HOSPID AGECAT SEXCAT PAYCAT RACECAT;

 LABEL
 EPPS02 = 'DEATH IN LOW MORT DRGS (Expected)'
 EPPS03 = 'PRESSURE ULCER (Expected)'
 EPPS04 = 'DEATH AMONG SURGICAL (Expected)'
 EPPS06 = 'IATROGENIC PNEUMOTHORAX (Expected)'
 EPPS07 = 'CENTRAL LINE ASSOCIATED BSI (Expected)'
 EPPS08 = 'POSTOPERATIVE HIP FRACTURE (Expected)'
 EPPS09 = 'POSTOP HEMORRHAGE OR HEMATOMA (Expected)'
 EPPS10 = 'POSTOP PHYSIO METABOL DERANGEMENT (Exp)'
 EPPS11 = 'POSTOP RESPIRATORY FAILURE (Expected)'
 EPPS12 = 'POSTOPERATIVE PE OR DVT (Expected)'
 EPPS13 = 'POSTOPERATIVE SEPSIS (Expected)'
 EPPS14 = 'POSTOPERATIVE WOUND DEHISCENCE (Exp)'
 EPPS15 = 'ACCIDENTAL PUNCTURE/LACERATION(Expected)'
 ;

 LABEL
 RPPS02 = 'DEATH IN LOW MORT DRGS (Risk Adj)'
 RPPS03 = 'PRESSURE ULCER (Risk Adj)'
 RPPS04 = 'DEATH AMONG SURGICAL (Risk Adj)'
 RPPS06 = 'IATROGENIC PNEUMOTHORAX (Risk Adj)'
 RPPS07 = 'CENTRAL LINE ASSOCIATED BSI (Risk Adj)'
 RPPS08 = 'POSTOPERATIVE HIP FRACTURE (Risk Adj)'
 RPPS09 = 'POSTOP HEMORRHAGE OR HEMATOMA (RA)'
 RPPS10 = 'POSTOP PHYSIO METABOL DERANGMNT (RA)'
 RPPS11 = 'POSTOP RESPIRATORY FAILURE (Risk Adj)'
 RPPS12 = 'POSTOPERATIVE PE OR DVT (Risk Adj)'
 RPPS13 = 'POSTOPERATIVE SEPSIS (Risk Adj)'
 RPPS14 = 'POSTOPERATIVE WOUND DEHISCENCE (RA)'
 RPPS15 = 'ACCIDENTAL PUNCTURE/LACERATION (RA)'
 ;

 LABEL
 LPPS02 = 'DEATH IN LOW MORT DRGS (Lower CL)'
 LPPS03 = 'PRESSURE ULCER (Lower CL)'
 LPPS04 = 'DEATH AMONG SURGICAL (Lower CL)'
 LPPS06 = 'IATROGENIC PNEUMOTHORAX (Lower CL)'
 LPPS07 = 'CENTRAL LINE ASSOCIATED BSI (Lower CL)'
 LPPS08 = 'POSTOPERATIVE HIP FRACTURE (Lower CL)'
 LPPS09 = 'POSTOP HEMORRHAGE OR HEMATOMA (LL)'
 LPPS10 = 'POSTOP PHYSIO METABOL DERANGMNT (LL)'
 LPPS11 = 'POSTOP RESPIRATORY FAILURE (Lower CL)'
 LPPS12 = 'POSTOPERATIVE PE OR DVT (Lower CL)'
 LPPS13 = 'POSTOPERATIVE SEPSIS (Lower CL)'
 LPPS14 = 'POSTOPERATIVE WOUND DEHISCENCE (LL)'
 LPPS15 = 'ACCIDENTAL PUNCTURE/LACERATION (LL)'
 ;

 LABEL
 UPPS02 = 'DEATH IN LOW MORT DRGS (Upper CL)'
 UPPS03 = 'PRESSURE ULCER (Upper CL)'
 UPPS04 = 'DEATH AMONG SURGICAL (Upper CL)'
 UPPS06 = 'IATROGENIC PNEUMOTHORAX (Upper CL)'
 UPPS07 = 'CENTRAL LINE ASSOCIATED BSI (Upper CL)'
 UPPS08 = 'POSTOPERATIVE HIP FRACTURE (Upper CL)'
 UPPS09 = 'POSTOP HEMORRHAGE OR HEMATOMA (UL)'
 UPPS10 = 'POSTOP PHYSIO METABOL DERANGMNT (UL)'
 UPPS11 = 'POSTOP RESPIRATORY FAILURE (Upper CL)'
 UPPS12 = 'POSTOPERATIVE PE OR DVT (Upper CL)'
 UPPS13 = 'POSTOPERATIVE SEPSIS (Upper CL)'
 UPPS14 = 'POSTOPERATIVE WOUND DEHISCENCE (UL)'
 UPPS15 = 'ACCIDENTAL PUNCTURE/LACERATION (UL)'
 ;

 LABEL 
 SPPS02 = 'DEATH IN LOW MORT DRGS (Smoothed)'
 SPPS03 = 'PRESSURE ULCER (Smoothed)'
 SPPS04 = 'DEATH AMONG SURGICAL (Smoothed)'
 SPPS06 = 'IATROGENIC PNEUMOTHORAX (Smoothed)'
 SPPS07 = 'CENTRAL LINE ASSOCIATED BSI (Smoothed)'
 SPPS08 = 'POSTOPERATIVE HIP FRACTURE (Smoothed)'
 SPPS09 = 'POSTOP HEMORRHAGE OR HEMATOMA (Smoothed)'
 SPPS10 = 'POSTOP PHYSIO METABOL DERANGEMENT (Smthd)'
 SPPS11 = 'POSTOP RESPIRATORY FAILURE (Smoothed)'
 SPPS12 = 'POSTOPERATIVE PE OR DVT (Smoothed)'
 SPPS13 = 'POSTOPERATIVE SEPSIS (Smoothed)'
 SPPS14 = 'POSTOPERATIVE WOUND DEHISCENCE (Smthd)'
 SPPS15 = 'ACCIDENTAL PUNCTURE/LACERATION(Smoothed)'
 ;

 LABEL 
 XPPS02 = 'DEATH IN LOW MORT DRGS (Smthe SE)'
 XPPS03 = 'PRESSURE ULCER (Smthe SE)'
 XPPS04 = 'DEATH AMONG SURGICAL (Smthe SE)'
 XPPS06 = 'IATROGENIC PNEUMOTHORAX (Smthe SE)'
 XPPS07 = 'CENTRAL LINE ASSOCIATED BSI (Smthe SE)'
 XPPS08 = 'POSTOPERATIVE HIP FRACTURE (Smthe SE)'
 XPPS09 = 'POSTOP HEMORRHAGE OR HEMATOMA (Smthe SE)'
 XPPS10 = 'POSTOP PHYSIO METABOL DERANGEMENT (SmtSE)'
 XPPS11 = 'POSTOP RESPIRATORY FAILURE (Smthe SE)'
 XPPS12 = 'POSTOPERATIVE PE OR DVT (Smthe SE)'
 XPPS13 = 'POSTOPERATIVE SEPSIS (Smthe SE)'
 XPPS14 = 'POSTOPERATIVE WOUND DEHISCENCE (SmtSE)'
 XPPS15 = 'ACCIDENTAL PUNCTURE/LACERATION(Smthe SE)'
 ;

 * -------------------------------------------------------------- ;
 * --- RE-LABEL DAY DEPENDENT INDICATORS ------------------------ ;
 * -------------------------------------------------------------- ;
 IF &PRDAY. = 0 THEN DO;

 LABEL 
 EPPS03 = 'PRESSURE ULCER-NO PRDAY (Expected)'
 EPPS08 = 'POSTOP HIP FRACTURE-NO PRDAY (Expected)'
 EPPS09 = 'POSTOP HEMOR OR HEMAT-NO PRDAY (Exp)'
 EPPS10 = 'POSTOP PHYSIO METABO DE-NO PRDAY (Exp)'
 EPPS11 = 'POSTOP RESP FAILURE-NO PRDAY (Expected)'
 EPPS12 = 'POSTOP PE OR DVT-NO PRDAY (Expected)'
 EPPS14 = 'POSTOP WOUND DEHISCENCE-NO PRDAY (Exp)'
 ;

 LABEL 
 RPPS03 = 'PRESSURE ULCER-NO PRDAY (Risk Adj)'
 RPPS08 = 'POSTOP HIP FRACTURE-NO PRDAY (Risk Adj)'
 RPPS09 = 'POSTOP HEMOR OR HEMAT-NO PRDAY (RA)'
 RPPS10 = 'POSTOP PHYSIO METABO DE-NO PRDAY (RA)'
 RPPS11 = 'POSTOP RESP FAILURE-NO PRDAY (Risk Adj)'
 RPPS12 = 'POSTOP PE OR DVT-NO PRDAY (Risk Adj)'
 RPPS14 = 'POSTOP WOUND DEHISCENCE-NO PRDAY (RA)'
 ;

 LABEL 
 SPPS03 = 'PRESSURE ULCER-NO PRDAY (Smoothed)'
 SPPS08 = 'POSTOP HIP FRACTURE-NO PRDAY (Smoothed)'
 SPPS09 = 'POSTOP HEMOR OR HEMAT-NO PRDAY (Smthd)'
 SPPS10 = 'POSTOP PHYSIO METABO DE-NO PRDAY (Smthd)'
 SPPS11 = 'POSTOP RESP FAILURE-NO PRDAY (Smoothed)'
 SPPS12 = 'POSTOP PE OR DVT-NO PRDAY (Smoothed)'
 SPPS14 = 'POSTOP WOUND DEHISCENCE-NO PRDAY (Smthd)'
 ;

 END;

 RUN;

*==================================================================;
*  Title:  PROGRAM A3  PART II:  MERGE PROVIDER RATES FOR AHRQ
*  PATIENT SAFETY INDICATORS
*
*  Description:  MERGED RATES FOR PATIENT SAFETY INDICATORS
*
*          >>>  VERSION 4.0, August 2009 <<<
*
*===================================================================;

TITLE2 'PROGRAM P3 PART II';
TITLE3 'AHRQ PATIENT SAFETY INDICATORS: PROVIDER-LEVEL MERGED FILES';

* ---------------------------------------------------------------- ;
* --- PATIENT SAFETY INDICATOR MERGED RATES                      - ;
* ---------------------------------------------------------------- ;

DATA OUTP3.&OUTFILP3.;
MERGE INP2.&INFILEP2.
  (KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT _TYPE_ 
        TPPS05 TPPS16 TPPS18 TPPS19
        PPPS05 PPPS16 PPPS18 PPPS19 
        OPPS05 OPPS16 OPPS18 OPPS19)
      RISKADJ
   (KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT 
         EPPS02-EPPS04 EPPS06-EPPS15 
         RPPS02-RPPS04 RPPS06-RPPS15 
         LPPS02-LPPS04 LPPS06-LPPS15 
         UPPS02-UPPS04 UPPS06-UPPS15 
         SPPS02-SPPS04 SPPS06-SPPS15 
         XPPS02-XPPS04 XPPS06-XPPS15 
         VPPS02-VPPS04 VPPS06-VPPS15
         TPPS02-TPPS04 TPPS06-TPPS15
         PPPS02-PPPS04 PPPS06-PPPS15
         OPPS02-OPPS04 OPPS06-OPPS15);
 BY HOSPID AGECAT SEXCAT PAYCAT RACECAT;

 %INCLUDE MSXP;

 ARRAY TRRY1{2} _TEMPORARY_ (14 15);
 ARRAY TRRY2{2} OPPS18 OPPS19;
 ARRAY TRRY3{2} PPPS18 PPPS19;
 ARRAY TRRY4{2} EPPS18 EPPS19;
 ARRAY TRRY5{2} RPPS18 RPPS19;
 ARRAY TRRY6{2} SEPS18 SEPS19;
 ARRAY TRRY7{2} VPPS18 VPPS19;
 ARRAY TRRY8{2} SNPS18 SNPS19;
 ARRAY TRRY9{2} SPPS18 SPPS19;
 ARRAY TRRY10{2} XPPS18 XPPS19;
 ARRAY TRRY11{2} LPPS18 LPPS19;
 ARRAY TRRY12{2} UPPS18 UPPS19;

 DO I = 1 TO 2;
    TRRY4(I) = ARRYP3(TRRY1(I));
    IF AGECAT = . AND SEXCAT = . AND PAYCAT = . AND RACECAT = . THEN DO;
       TRRY5(I)  = TRRY2(I);
       TRRY6(I)  = SQRT(TRRY2(I)*(1-TRRY2(I)) / TRRY3(I));
       TRRY7(I)  = TRRY6(I)**2;
       TRRY8(I)  = ARRYP2(TRRY1(I)) / (ARRYP2(TRRY1(I)) + TRRY7(I));
       TRRY9(I)  = (TRRY5(I) * TRRY8(I)) + ((1 -  TRRY8(I)) * ARRYP3(TRRY1(I)));
       TRRY10(I)  = SQRT(ARRYP2(TRRY1(I))- (TRRY8(I) * ARRYP2(TRRY1(I))));
    END;
    ELSE DO;
       TRRY3(I)  = (TRRY2(I) / TRRY4(I));
       TRRY6(I)  = SQRT(TRRY2(I)*(1-TRRY2(I)) / TRRY3(I)) / TRRY4(I);
       TRRY7(I)  = .;
       TRRY9(I)  = .;
       TRRY10(I)  = .;
    END;
    TRRY11(I)   = TRRY5(I) - (1.96 * TRRY6(I));
    IF TRRY11(I) < 0 THEN TRRY11(I) = 0;
    TRRY12(I)   = TRRY5(I) + (1.96 * TRRY6(I));
    TRRY12(I)   = TRRY5(I) + (1.96 * TRRY6(I));
    IF TRRY12(I) > 1 THEN TRRY12(I) = 1;

 END;

 LABEL
 EPPS18 = 'OB TRAUMA - VAGINAL W INSTRUMENT (Exp)'
 EPPS19 = 'OB TRAUMA - VAGINAL W/O INST (Exp)'
 ;

 LABEL
 RPPS18 = 'OB TRAUMA - VAGINAL W INSTRUMENT (RA)'
 RPPS19 = 'OB TRAUMA - VAGINAL W/O INSTRUMENT (RA)'
 ;

 LABEL
 LPPS18 = 'OB TRAUMA - VAGINAL W INSTRUMENT (LL)'
 LPPS19 = 'OB TRAUMA - VAGINAL W/O INSTRUMENT (LL)'
 ;

 LABEL
 UPPS18 = 'OB TRAUMA - VAGINAL W INSTRUMENT (UL)'
 UPPS19 = 'OB TRAUMA - VAGINAL W/O INSTRUMENT (UL)'
 ;

 LABEL
 SPPS18 = 'OB TRAUMA - VAGINAL W INSTRUMENT (Smthd)'
 SPPS19 = 'OB TRAUMA - VAGINAL W/O INST (Smthd)'
 ;

 LABEL
 XPPS18 = 'OB TRAUMA - VAGINAL W INSTRUMENT (SmtSE)'
 XPPS19 = 'OB TRAUMA - VAGINAL W/O INST (SmtSE)'
 ;

 ARRAY ARRY1{15} EPPS02-EPPS04 EPPS06-EPPS15 EPPS18-EPPS19;
 ARRAY ARRY2{15} RPPS02-RPPS04 RPPS06-RPPS15 RPPS18-RPPS19;
 ARRAY ARRY3{15} LPPS02-LPPS04 LPPS06-LPPS15 LPPS18-LPPS19;
 ARRAY ARRY4{15} UPPS02-UPPS04 UPPS06-UPPS15 UPPS18-UPPS19;
 ARRAY ARRY5{15} SPPS02-SPPS04 SPPS06-SPPS15 SPPS18-SPPS19;
 ARRAY ARRY6{15} XPPS02-XPPS04 XPPS06-XPPS15 XPPS18-XPPS19;
 ARRAY ARRY7{15} VPPS02-VPPS04 VPPS06-VPPS15 VPPS18-VPPS19;
 ARRAY ARRY8{15} PPPS02-PPPS04 PPPS06-PPPS15 PPPS18-PPPS19;

 DO I = 1 TO 15;
    IF ARRY8(I) <= 2 THEN DO;
       ARRY1(I) = .; ARRY2(I) = .; ARRY3(I) = .; ARRY4(I) = .;
       ARRY5(I) = .; ARRY6(I) = .; ARRY7(I) = .; 
    END;
 END;

 DROP I;

 FORMAT EPPS02 EPPS03 EPPS04 EPPS06 EPPS07 EPPS08 EPPS09 EPPS10 EPPS11 
		EPPS12 EPPS13 EPPS14 EPPS15 EPPS18 EPPS19
		LPPS02 LPPS03 LPPS04 LPPS06 LPPS07 LPPS08 LPPS09 LPPS10 LPPS11 
		LPPS12 LPPS13 LPPS14 LPPS15 LPPS18 LPPS19 
		OPPS02 OPPS03 OPPS04 OPPS05 OPPS06 OPPS07 OPPS08 OPPS09 OPPS10 
		OPPS11 OPPS12 OPPS13 OPPS14 OPPS15 OPPS16 OPPS18 OPPS19
		RPPS02 RPPS03 RPPS04 RPPS06 RPPS07 RPPS08 RPPS09 RPPS10 RPPS11
		RPPS12 RPPS13 RPPS14 RPPS15 RPPS18 RPPS19 
		SEPS18 SEPS19
		SNPS18 SNPS19
		SPPS02 SPPS03 SPPS04 SPPS06 SPPS07 SPPS08 SPPS09 SPPS10 SPPS11
		SPPS12 SPPS13 SPPS14 SPPS15 SPPS18 SPPS19
		UPPS02 UPPS03 UPPS04 UPPS06 UPPS07 UPPS08 UPPS09 UPPS10 UPPS11
		UPPS12 UPPS13 UPPS14 UPPS15 UPPS18 UPPS19 
		VPPS02 VPPS03 VPPS04 VPPS06 VPPS07 VPPS08 VPPS09 VPPS10 VPPS11
		VPPS12 VPPS13 VPPS14 VPPS15 VPPS18 VPPS19 
		XPPS02 XPPS03 XPPS04 XPPS06 XPPS07 XPPS08 XPPS09 XPPS10 XPPS11
		XPPS12 XPPS13 XPPS14 XPPS15 XPPS18 XPPS19 13.7
		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 13.0;
 RUN;

* -------------------------------------------------------------- ;
* --- CONTENTS AND MEANS OF PROVIDER-LEVEL MERGED FILE --------- ;
* -------------------------------------------------------------- ;

PROC CONTENTS DATA=OUTP3.&OUTFILP3. POSITION;
RUN;

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

* -------------------------------------------------------------- ;
* --- PRINT PROVIDER MERGED FILE ------------------------------- ;
* -------------------------------------------------------------- ;

%MACRO PRT2;

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

%MACRO PRT(PS,TEXT,CASE);

PROC PRINT DATA=OUTP3.&OUTFILP3. LABEL SPLIT='*';
%IF &CASE. = 1 %THEN %DO;
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;
%END;
%ELSE %DO;
VAR HOSPID AGECAT SEXCAT PAYCAT RACECAT
    TPPS&PS. PPPS&PS. OPPS&PS. EPPS&PS. RPPS&PS. LPPS&PS. UPPS&PS. SPPS&PS. XPPS&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)"
      EPPS&PS. = "EPPS&PS.*(Expected)"
      RPPS&PS. = "RPPS&PS.*(Risk Adj)"
      LPPS&PS. = "LPPS&PS.*(Lower CL)"
      UPPS&PS. = "UPPS&PS.*(Upper CL)"
      SPPS&PS. = "SPPS&PS.*(Smoothed)"
      XPPS&PS. = "XPPS&PS.*(Smoth SE)"
      ;
FORMAT AGECAT AGECAT.
       SEXCAT SEXCAT.
       PAYCAT PAYCAT.
       RACECAT RACECAT.
       TPPS&PS. PPPS&PS. 13.0 OPPS&PS. RPPS&PS. SPPS&PS. EPPS&PS. 8.6;
%END;
TITLE4 "FINAL OUTPUT";
TITLE5 "Indicator # &PS.: &TEXT";

RUN;

%MEND PRT;

%PRT(02,DEATH IN LOW MORTALITY DRGS,0);
%PRT(03,PRESSURE ULCER,0);
%PRT(04,DEATH AMONG SURGICAL,0);
%PRT(05,FOREIGN BODY LEFT IN DURING PROCEDURE,1);
%PRT(06,IATROGENIC PNEUMOTHORAX,0);
%PRT(07,CENTRAL LINE ASSOCIATED BSI,0);
%PRT(08,POST-OPERATIVE HIP FRACTURE,0);
%PRT(09,POST-OPERATIVE HEMORRHAGE OR HEMATOMA,0);
%PRT(10,POST-OPERATIVE PHYSIOLOGIC AND METABOLIC DERANGEMENTS,0);
%PRT(11,POST-OPERATIVE RESPIRATORY FAILURE,0);
%PRT(12,POST-OPERATIVE PULMONARY EMBOLISM OR DEEP VEIN THROMBOSIS,0);
%PRT(13,POST-OPERATIVE SEPSIS,0);
%PRT(14,POST-OPERATIVE WOUND DEHISCENCE,0);
%PRT(15,ACCIDENTAL PUNCTURE/LACERATION,0);
%PRT(16,TRANSFUSION REACTION,1);
%PRT(18,OBSTETRIC TRAUMA - VAGINAL WITH INSTRUMENT,0);
%PRT(19,OBSTETRIC TRAUMA - VAGINAL WITHOUT INSTRUMENT,0);

%END;

%MEND PRT2;

%PRT2;


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

%MACRO TEXT;

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

DATA _NULL_;
SET OUTP3.&OUTFILP3;
FILE PSTEXTP3 LRECL=4500;
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" "," 
"EPPS02" "," "EPPS03" "," "EPPS04" "," "EPPS06" ","
"EPPS07" "," "EPPS08" "," "EPPS09" "," "EPPS10" ","
"EPPS11" "," "EPPS12" "," "EPPS13" "," "EPPS14" ","
"EPPS15" "," "EPPS18" "," "EPPS19" "," 
"RPPS02" "," "RPPS03" "," "RPPS04" "," "RPPS06" ","
"RPPS07" "," "RPPS08" "," "RPPS09" "," "RPPS10" ","
"RPPS11" "," "RPPS12" "," "RPPS13" "," "RPPS14" ","
"RPPS15" "," "RPPS18" "," "RPPS19" "," 
"LPPS02" "," "LPPS03" "," "LPPS04" "," "LPPS06" ","
"LPPS07" "," "LPPS08" "," "LPPS09" "," "LPPS10" ","
"LPPS11" "," "LPPS12" "," "LPPS13" "," "LPPS14" ","
"LPPS15" "," "LPPS18" "," "LPPS19" "," 
"UPPS02" "," "UPPS03" "," "UPPS04" "," "UPPS06" ","
"UPPS07" "," "UPPS08" "," "UPPS09" "," "UPPS10" ","
"UPPS11" "," "UPPS12" "," "UPPS13" "," "UPPS14" ","
"UPPS15" "," "UPPS18" "," "UPPS19" "," 
"SPPS02" "," "SPPS03" "," "SPPS04" "," "SPPS06" ","
"SPPS07" "," "SPPS08" "," "SPPS09" "," "SPPS10" ","
"SPPS11" "," "SPPS12" "," "SPPS13" "," "SPPS14" ","
"SPPS15" "," "SPPS18" "," "SPPS19" "," 
"XPPS02" "," "XPPS03" "," "XPPS04" "," "XPPS06" ","
"XPPS07" "," "XPPS08" "," "XPPS09" "," "XPPS10" ","
"XPPS11" "," "XPPS12" "," "XPPS13" "," "XPPS14" ","
"XPPS15" "," "XPPS18" "," "XPPS19"  
;

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 ",") ","
(EPPS02-EPPS04 EPPS06-EPPS15 EPPS18-EPPS19) (12.10 ",") ","
(RPPS02-RPPS04 RPPS06-RPPS15 RPPS18-RPPS19) (12.10 ",") ","
(LPPS02-LPPS04 LPPS06-LPPS15 LPPS18-LPPS19) (12.10 ",") ","
(UPPS02-UPPS04 UPPS06-UPPS15 UPPS18-UPPS19) (12.10 ",") ","
(SPPS02-SPPS04 SPPS06-SPPS15 SPPS18-SPPS19) (12.10 ",") ","
(XPPS02-XPPS04 XPPS06-XPPS15 XPPS18-XPPS19) (12.10 ",")
;

RUN;

%END;

%MEND TEXT;

%TEXT;

