*===================================================================;
*  Title:  PROGRAM P3 PART 1:  CALCULATE RISK-ADJUSTED PROVIDER
*          RATES FOR AHRQ PATIENT SAFETY INDICATORS
*
*  Description:
*         USES PROC SCORE TO CALCULATE RISK-ADJUSTED RATES FOR
*         PATIENT SAFETY INDICATORS.
*         ADJUSTS FOR: AGE, SEXCAT, DRG AND COMORBIDITY
*
*          >>>  VERSION 2.1 (revision 3), January 2005 <<<
*
*===================================================================;
FILENAME CONTROL "C:\PATHNAME\CONTROL_PSI.SAS"; *<==USER MUST modify;

%INCLUDE CONTROL;

%MACRO MDL;
    %IF &MDL. = 0 %THEN %DO;
        PROC SCORE DATA=TEMP1 SCORE=TEMP2 TYPE=PARMS OUT=TEMP1Y;
            VAR SEX AGE1-AGE%EVAL(&A.) SAGE1-SAGE%EVAL(&A.) D1-D&D. C1-C&C.;
        RUN;

    %END;
    %ELSE %IF &MDL. = 1 %THEN %DO;
        PROC SCORE DATA=TEMP1 SCORE=TEMP2 TYPE=PARMS OUT=TEMP1Y;
            VAR SEX;
        RUN;
    %END;
    %ELSE %IF &MDL. = 2 %THEN %DO;
       PROC SCORE DATA=TEMP1 SCORE=TEMP2 TYPE=PARMS OUT=TEMP1Y;
           VAR AGE1-AGE%EVAL(&A.);
       RUN;
    %END;
%MEND;

%MACRO LBL1;
    LABEL RPPS01 = 'COMPLICATIONS OF ANESTHESIA (Risk Adj)'
          RPPS02 = 'DEATH IN LOW MORTALITY DRGS (Risk Adj)'
          RPPS03 = 'DECUBITUS ULCER (Risk Adj)'
          RPPS04 = 'FAILURE TO RESCUE (Risk Adj)'
          RPPS05 = 'FOREIGN BODY LEFT IN DURING PROC (RA)'
          RPPS06 = 'IATROGENIC PNEUMOTHORAX (Risk Adj)'
          RPPS07 = 'INFECTION DUE TO MEDICAL CARE (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)'
          RPPS16 = 'TRANSFUSION REACTION (Risk Adj)'
          RPPS17 = 'BIRTH TRAUMA - INJURY TO NEONATE (RA)'
          RPPS18 = 'OB TRAUMA - VAGINAL W INSTRUMENT (RA)'
          RPPS19 = 'OB TRAUMA - VAGINAL W/O INSTRUMENT (RA)'
          RPPS20 = 'OB TRAUMA - C-SECTION (Risk Adj)'
          RPPS27 = 'OB TRAUMA W 3 DEG - VAGINAL W INST (RA)'
          RPPS28 = 'OB TRAUMA W 3 DEG - VAGINAL W/O INST (RA)'
          RPPS29 = 'OB TRAUMA W 3 DEG - C-SECTION (Risk Adj)'
          ;

 * -------------------------------------------------------------- ;
 * --- RE-LABEL DAY DEPENDENT INDICATORS ------------------------ ;
 * -------------------------------------------------------------- ;
 %IF &PRDAY. = 0 %THEN %DO;
    LABEL 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)'
          ;
 %END;

%MEND;

%MACRO LBL2;
    LABEL SPPS01 = 'COMPLICATIONS OF ANESTHESIA (Smoothed)'
          SPPS02 = 'DEATH IN LOW MORTALITY DRGS (Smoothed)'
          SPPS03 = 'DECUBITUS ULCER (Smoothed)'
          SPPS04 = 'FAILURE TO RESCUE (Smoothed)'
          SPPS05 = 'FOREIGN BODY LEFT IN DURING PROC (Smthd)'
          SPPS06 = 'IATROGENIC PNEUMOTHORAX (Smoothed)'
          SPPS07 = 'INFECTION DUE TO MEDICAL CARE (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)'
          SPPS16 = 'TRANSFUSION REACTION (Smoothed)'
          SPPS17 = 'BIRTH TRAUMA - INJURY TO NEONATE (Smthd)'
          SPPS18 = 'OB TRAUMA - VAGINAL W INSTRUMENT (Smthd)'
          SPPS19 = 'OB TRAUMA - VAGINAL W/O INST (Smthd)'
          SPPS20 = 'OB TRAUMA - C-SECTION (Smoothed)'
          SPPS27 = 'OB TRAUMA W 3 DEG- VAG W INST (Smthd)'
          SPPS28 = 'OB TRAUMA W 3 DEG- VAG W/O INST (Smthd)'
          SPPS29 = 'OB TRAUMA W 3 DEG- C-SECTION (Smoothed)'
          ;

 * -------------------------------------------------------------- ;
 * --- RE-LABEL DAY DEPENDENT INDICATORS ------------------------ ;
 * -------------------------------------------------------------- ;
 %IF &PRDAY. = 0 %THEN %DO;
    LABEL 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;

%MEND;

%MACRO LBL3;
    LABEL EPPS01 = 'COMPLICATIONS OF ANESTHESIA (Expected)'
          EPPS02 = 'DEATH IN LOW MORTALITY DRGS (Expected)'
          EPPS03 = 'DECUBITUS ULCER (Expected)'
          EPPS04 = 'FAILURE TO RESCUE (Expected)'
          EPPS05 = 'FOREIGN BODY LEFT IN DURING PROC (Exp)'
          EPPS06 = 'IATROGENIC PNEUMOTHORAX (Expected)'
          EPPS07 = 'INFECTION DUE TO MEDICAL CARE (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)'
          EPPS16 = 'TRANSFUSION REACTION (Expected)'
          EPPS17 = 'BIRTH TRAUMA - INJURY TO NEONATE (Exp)'
          EPPS18 = 'OB TRAUMA - VAGINAL W INSTRUMENT (Exp)'
          EPPS19 = 'OB TRAUMA - VAGINAL W/O INST (Exp)'
          EPPS20 = 'OB TRAUMA - C-SECTION (Expected)'
          EPPS27 = 'OB TRAUMA W 3 DEG- VAGINAL W INST (Exp)'
          EPPS28 = 'OB TRAUMA W 3 DEG- VAGINAL W/O INST (Exp)'
          EPPS29 = 'OB TRAUMA W 3 DEG- C-SECTION (Expected)'
          ;

 * -------------------------------------------------------------- ;
 * --- RE-LABEL DAY DEPENDENT INDICATORS ------------------------ ;
 * -------------------------------------------------------------- ;
 %IF &PRDAY. = 0 %THEN %DO;
    LABEL 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)'
          ;
 %END;

%MEND;

%MACRO PRT(PS,TEXT);
    PROC PRINT DATA=OUTP3.&OUTFILP3. LABEL SPLIT='*';
        VAR HOSPID AGECAT SEXCAT PAYCAT RACECAT
            TPPS&PS. PPPS&PS. OPPS&PS. RPPS&PS. SPPS&PS. EPPS&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)"
              RPPS&PS. = "RPPS&PS.*(Risk Adj)"
              SPPS&PS. = "SPPS&PS.*(Smoothed)"
              EPPS&PS. = "EPPS&PS.*(Expected)"
          ;
        FORMAT TPPS&PS. PPPS&PS. 13.0 OPPS&PS. RPPS&PS. SPPS&PS. EPPS&PS. 8.6
               AGECAT $AGECAT. SEXCAT $SEXCAT. PAYCAT $PAYCAT. RACECAT $RACECAT.;
        TITLE4 "FINAL OUTPUT";
        TITLE5 "Indicator # &PS.: &TEXT";
    RUN;
%MEND;


%MACRO PRT2;
   %IF &PRINT. = 1 %THEN %DO;
       %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;


%MACRO TEXT;
   %IF &TEXTP3. = 1  %THEN %DO;
       DATA _NULL_;
          SET OUTP3.&OUTFILP3;
          FILE PSTEXTP3 LRECL=2000;
          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" ","
            "RPPS01" "," "RPPS02" "," "RPPS03" "," "RPPS04" ","
            "RPPS05" "," "RPPS06" "," "RPPS07" "," "RPPS08" ","
            "RPPS09" "," "RPPS10" "," "RPPS11" "," "RPPS12" ","
            "RPPS13" "," "RPPS14" "," "RPPS15" "," "RPPS16" ","
            "RPPS17" "," "RPPS18" "," "RPPS19" "," "RPPS20" ","
            "RPPS27" "," "RPPS28" "," "RPPS29" ","
            "SPPS01" "," "SPPS02" "," "SPPS03" "," "SPPS04" ","
            "SPPS05" "," "SPPS06" "," "SPPS07" "," "SPPS08" ","
            "SPPS09" "," "SPPS10" "," "SPPS11" "," "SPPS12" ","
            "SPPS13" "," "SPPS14" "," "SPPS15" "," "SPPS16" ","
            "SPPS17" "," "SPPS18" "," "SPPS19" "," "SPPS20" ","
            "SPPS27" "," "SPPS28" "," "SPPS29"
            "EPPS01" "," "EPPS02" "," "EPPS03" "," "EPPS04" ","
            "EPPS05" "," "EPPS06" "," "EPPS07" "," "EPPS08" ","
            "EPPS09" "," "EPPS10" "," "EPPS11" "," "EPPS12" ","
            "EPPS13" "," "EPPS14" "," "EPPS15" "," "EPPS16" ","
            "EPPS17" "," "EPPS18" "," "EPPS19" "," "EPPS20" ","
            "EPPS27" "," "EPPS28" "," "EPPS29"
            ;
        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 ",") ","
           (RPPS01-RPPS20 RPPS27-RPPS29) (12.10 ",") ","
           (SPPS01-SPPS20 SPPS27-SPPS29) (12.10 ",") ","
           (EPPS01-EPPS20 EPPS27-EPPS29) (12.10 ",");
       RUN;
   %END;
 %MEND;


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

* -------------------------------------------------------------- ;
* --- PS 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)                                          - ;
* --- A - THE NUMBER OF DEMOGRAPHIC CATEGORIES (AGE AND SEX)   - ;
* --- C - THE NUMBER OF DRG CATEGORIES (FIXED AT 374)          - ;
* --- B - THE NUMBER OF COMORBIDITY CATEGORIES (FIXED AT 27)   - ;
* --- S - DETERMINES WHETHER AGE*SEX INTERACTIONS ARE NEEDED   - ;
* -------------------------------------------------------------- ;

%MACRO MOD3(N,PS,A,D,C,S,MDL);

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

    DATA TEMP1(KEEP=KEY HOSPID T&PS. SEX AGE1-AGE%EVAL(&A.)
        SAGE1-SAGE%EVAL(&A.) D1-D&D. C1-C&C. AGECAT SEXCAT PAYCAT RACECAT);
        LENGTH AIDX AIDX2 DIDX DIDX2 CIDX AGE0-AGE%EVAL(&A.)
               SAGE0-SAGE%EVAL(&A.) D0-D&D. C0-C&C. 8;
        SET INP3.&INFILP3X.;

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

        * --- INITIALIZE COVARIATE DUMMIES TO ZERO --------------------- ;
        ARRAY AGEX{0:%EVAL(&A.)}  AGE0-AGE%EVAL(&A.);
        ARRAY SAGEX{0:%EVAL(&A.)} SAGE0-SAGE%EVAL(&A.);
        ARRAY DX{0:&D.} D0-D&D.;
        ARRAY CX{0:&C.} C0-C&C.;
        ARRAY CX2{27} 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  ;
        SEX = 0;
        DO I = 0 TO &A.;
          AGEX(I)=0;
          SAGEX(I)=0;
        END;
        DO I = 0 TO &D.;
          DX(I)=0;
        END;
        DO I = 0 TO &C.;
          CX(I)=0;
        END;

        * --- SET INDEX FOR AGE ARRAY (AIDX) AND DRG ARRAY (DIDX)    --- ;
        * --- CATEGORIES VARY BY PATIENT SAFETY INDICATOR AND ARE    --- ;
        * --- DETERMINED BY THE FORMAT CORRESPONDING TO N            --- ;
        AIDX = PUT(AGE,AGEFMT.);
        AIDX2 = PUT(AIDX,A&N.PSFMT.); 

        DIDX = PUT(PUT(DRG,Z3.),$DRGFMT.);
        DIDX2 = PUT(DIDX,D&N.PSFMT.);

        * --- SET APPROPRIATE COVARIATE DUMMY TO 1 FOR THIS DISCHARGE  - ;
        IF SEXCAT IN (&S.) THEN SEX = 1;
        IF AIDX2 > 0 THEN DO; AGEX(AIDX2) = 1; SAGEX(AIDX2) = 1 * SEX; END;
        IF DIDX2 > 0 THEN DX(DIDX2) = 1;
        DO I = 1 TO 27;
          IF CX2(I) = 1 THEN DO;
             CIDX = PUT(I,C&N.PSFMT.);
             IF CIDX > 0 THEN CX(CIDX) = 1;
          END;
        END;
    RUN;


    * --- THIS DATA STEP READS THE REGRESSION COEFFICIENTS FOR EACH - ;
    * --- COVARIATE.                                                - ;

    DATA TEMP2(KEEP=INTERCEP SEX AGE1-AGE%EVAL(&A.) SAGE1-SAGE%EVAL(&A.)
                    D1-D&D. C1-C&C. _NAME_ _TYPE_ _LINK_ _LNLIKE_ _STATUS_);
        LENGTH INTERCEP SEX AGE1-AGE%EVAL(&A.) SAGE1-SAGE%EVAL(&A.) D1-D&D. C1-C&C.
              _LNLIKE_ 8 F1-F%EVAL(260-(2*&A.+&D.+&C.)) 8
               _NAME_ _TYPE_ _LINK_ $8 _STATUS_ $11;
        INFILE COVARP MISSOVER LRECL=4000;
        INPUT  N INTERCEP SEX AGE1-AGE%EVAL(&A.) SAGE1-SAGE%EVAL(&A.)
               D1-D&D. C1-C&C. F1-F%EVAL(260-(2*&A.+&D.+&C.))
               _NAME_ _TYPE_ _LINK_ _LNLIKE_ _STATUS_;
        IF N = &N.;
    RUN;

    * --- THIS PROC CALCULATES A PREDICTED PSO FOR EACH OBSERVATION  - ;
    * --- USING THE ACTUAL COVARIATES AND THE ESTIMATED COEFFICIENTS - ;
    * --- THE PROVIDER EFFECT IS CALCULATED BY SUBTRACTING THE       - ;
    * --- PREDICTION FROM THE ACTUAL ON EACH OBSERVATION, AND        - ;
    * --- CALCULATING THE MEAN DIFFERENCE FOR THE PROVIDER.          - ;

    %MDL

    PROC   DATASETS NOLIST;
       DELETE TEMP1 TEMP2;
    RUN;

    * --- THIS PROC CALCULATES A PREDICTED PSI FOR EACH OBSERVATION  - ;
    * --- USING THE ACTUAL COVARIATES AND THE ESTIMATED COEFFICIENTS - ;
    * --- AND ADDING IN THE OVERALL MEAN                             - ;

    DATA TEMP1Y;
        SET TEMP1Y;
        %INCLUDE MSXP;
        SUB_N = &N ;
        IF SUB_N > 20 THEN SUB_N=SUB_N - 6;
        EHAT = ARRYP3(SUB_N) + (T&PS. - (EXP(E&PS.)/(1 + EXP(E&PS.))));
        XHAT = EXP(E&PS.)/(1 + EXP(E&PS.));
    RUN;

    PROC SUMMARY DATA=TEMP1Y;
        CLASS  HOSPID AGECAT SEXCAT PAYCAT RACECAT;
        VAR EHAT XHAT;
        OUTPUT OUT=R&PS. MEAN=R&PS. E&PS.;
    RUN;

    PROC DATASETS NOLIST;
        DELETE TEMP1Y;
    RUN;

    * --- KEEP RISK-ADJUSTED RATE ONLY IF AT LEAST 3 PATIENTS IN      - ;
    * --- STRATIFICATION CELL. OTHERWISE SET RISK-ADJUSTED RATE TO    - ;
    * --- MISSING.                                                    - ;

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

    PROC SORT DATA=INP3.&INFILP3Y.(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT P&PS.)
              OUT=&INFILP3Y.;
        BY  HOSPID AGECAT SEXCAT PAYCAT RACECAT;
    RUN;

    DATA R&PS.;
        MERGE  R&PS. &INFILP3Y.(IN=X);
        BY HOSPID AGECAT SEXCAT PAYCAT RACECAT;
        IF X;
        IF P&PS. LT 3 THEN DO; R&PS=.; E&PS=.; END;
    RUN;

%MEND;

 %MOD3(1,PPS01,18,33,19,'F',0);
 %MOD3(2,PPS02,18,29,22,'F',0);
 %MOD3(3,PPS03,18,193,26,'F',0);
 %MOD3(4,PPS04,15,169,27,'F',0);
 %MOD3(5,PPS05,15,13,18,'F',0);
 %MOD3(6,PPS06,18,74,26,'F',0);
 %MOD3(7,PPS07,18,141,23,'F',0);
 %MOD3(8,PPS08,6,8,17,'F',0);
 %MOD3(9,PPS09,18,63,25,'F',0);
 %MOD3(10,PPS10,12,13,14,'F',0);
 %MOD3(11,PPS11,18,38,25,'F',0);
 %MOD3(12,PPS12,18,104,27,'F',0);
 %MOD3(13,PPS13,16,42,22,'F',0);
 %MOD3(14,PPS14,13,10,19,'F',0);
 %MOD3(15,PPS15,18,133,27,'F',0);
 %MOD3(17,PPS17,2,2,2,'F',1);
 %MOD3(18,PPS18,8,2,2,' ',2);
 %MOD3(19,PPS19,8,2,2,' ',2);
 %MOD3(20,PPS20,6,2,2,' ',2);
 %MOD3(27,PPS27,8,2,2,' ',2);
 %MOD3(28,PPS28,8,2,2,' ',2);
 %MOD3(29,PPS29,6,2,2,' ',2);

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

DATA RISKADJ;
    LENGTH RPPS16 EPPS16 8;
    MERGE RPPS01(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPPS01 EPPS01 PPPS01)
          RPPS02(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPPS02 EPPS02 PPPS02)
          RPPS03(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPPS03 EPPS03 PPPS03)
          RPPS04(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPPS04 EPPS04 PPPS04)
          RPPS05(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPPS05 EPPS05 PPPS05)
          RPPS06(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPPS06 EPPS06 PPPS06)
          RPPS07(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPPS07 EPPS07 PPPS07)
          RPPS08(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPPS08 EPPS08 PPPS08)
          RPPS09(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPPS09 EPPS09 PPPS09)
          RPPS10(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPPS10 EPPS10 PPPS10)
          RPPS11(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPPS11 EPPS11 PPPS11)
          RPPS12(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPPS12 EPPS12 PPPS12)
          RPPS13(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPPS13 EPPS13 PPPS13)
          RPPS14(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPPS14 EPPS14 PPPS14)
          RPPS15(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPPS15 EPPS15 PPPS15)
          RPPS17(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPPS17 EPPS17 PPPS17)
          RPPS18(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPPS18 EPPS18 PPPS18)
          RPPS19(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPPS19 EPPS19 PPPS19)
          RPPS20(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPPS20 EPPS20 PPPS20)
          RPPS27(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPPS27 EPPS27 PPPS27)
          RPPS28(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPPS28 EPPS28 PPPS28)
          RPPS29(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPPS29 EPPS29 PPPS29);
    BY HOSPID AGECAT SEXCAT PAYCAT RACECAT;

    %LBL1;
    %LBL3;

    ARRAY ARRY1{23} RPPS01-RPPS20 RPPS27-RPPS29;
    ARRAY ARRY2{23} EPPS01-EPPS20 EPPS27-EPPS29;
    DO I = 1 TO 23;
             IF ARRY1(I) < 0 AND ARRY1(I) NE . THEN ARRY1(I) = 0;
        ELSE IF ARRY1(I) > 1                   THEN ARRY1(I) = 1;
             IF ARRY2(I) < 0 AND ARRY1(I) NE . THEN ARRY2(I) = 0;
        ELSE IF ARRY2(I) > 1                   THEN ARRY2(I) = 1;
    END;

    DROP I;
RUN;

*==================================================================;
*  Title:  PROGRAM P3 PART II:  CALCULATE SMOOTHED RATES FOR
*  AHRQ PATIENT SAFETY INDICATORS.
*
*  Description:
*         CALCULATE SMOOTHED RATES FOR PATIENT SAFETY INDICATORS
*
*                 >>>  VERSION 2.1  <<<
*
*===================================================================;

TITLE2 'PROGRAM P3  PART II';
TITLE3 'AHRQ PATIENT SAFETY INDICATORS: CALCULATE SMOOTHED PROVIDER RATES';

* ---------------------------------------------------------------- ;
* --- PATIENT SAFETY INDICATOR SMOOTHED RATES                 - ;
* ---  IN THE UNIVARIATE MODEL, SMOOTHED ESTIMATE IS DEMEANED    - ;
* ---  PROVIDER RATE * (SIGNAL / (SIGNAL+NOISE))                 - ;
* ---------------------------------------------------------------- ;

DATA SMOOTHED(DROP=RPPS01-RPPS20 RPPS27-RPPS29 EPPS01-EPPS20 EPPS27-EPPS29);
    SET  RISKADJ;
    %INCLUDE MSXP;

    ARRAY ARRYP{23} RPPS01-RPPS20 RPPS27-RPPS29;
    ARRAY ARRYN{23} PPPS01-PPPS20 PPPS27-PPPS29;
    ARRAY ARRYX{23} SPPS01-SPPS20 SPPS27-SPPS29;

    DO I = 1 TO 23;
      ARRYX(I) = ARRYP(I) - ARRYP3(I);
      IF ARRYN(I) > 0 AND ARRYP2(I) NE 0 THEN
          ARRYX(I) = ARRYX(I) * (ARRYP2(I) / (ARRYP2(I) + (ARRYP1(I) / ARRYN(I))));
      ELSE ARRYX(I)=.;
      ARRYX(I) = ARRYX(I) + ARRYP3(I);
    END;


    %LBL2

    DROP I;
RUN;

*==================================================================;
*  Title:  PROGRAM A3  PART III:  MERGE PROVIDER RATES FOR AHRQ
*  PATIENT SAFETY INDICATORS
*
*  Description:  MERGED RATES FOR PATIENT SAFETY INDICATORS
*
*                 >>>  VERSION 2.1  <<<
*
*===================================================================;

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

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

DATA OUTP3.&OUTFILP3.;
    MERGE INP3.&INFILP3Y.(KEEP=HOSPID _TYPE_ TPPS01-TPPS20 TPPS27-TPPS29
                               PPPS01-PPPS20 PPPS27-PPPS29
                               OPPS01-OPPS20 OPPS27-OPPS29 
                               AGECAT SEXCAT PAYCAT RACECAT)
          RISKADJ(KEEP=HOSPID RPPS01-RPPS20 RPPS27-RPPS29 
                       EPPS01-EPPS20 EPPS27-EPPS29 
                       AGECAT SEXCAT PAYCAT RACECAT)
          SMOOTHED(KEEP=HOSPID SPPS01-SPPS20 SPPS27-SPPS29 
                        AGECAT SEXCAT PAYCAT RACECAT);
    BY HOSPID AGECAT SEXCAT PAYCAT RACECAT;
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 ------------------------------- ;
* -------------------------------------------------------------- ;

%PRT2


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

%TEXT
