*===================================================================;
*  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  <<<
*
*===================================================================;
FILENAME CONTROL "C:\PATHNAME\CONTROL.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 (RskAd)'
          RPPS06 = 'IATROGENIC PNEUMOTHORAX       (Risk Adj)'
          RPPS07 = 'INFECTION DUE TO MEDICAL CARE (Risk Adj)'
          RPPS08 = 'POSTOPERATIVE HIP FRACTURE    (Risk Adj)'
    %IF &PRDAY. = 1 %THEN %DO;
          RPPS09 = 'POSTOP HEMORRHAGE OR HEMATOMA  (RiskAdj)'
    %END;
    %ELSE %DO;
          RPPS09 = 'POSTOP HEMOR OR HEMAT-NO PRDAY (RiskAdj)'
    %END;
          RPPS10 = 'POSTOP PHYSIO METABOL DERANGMNT (RskAdj)'
          RPPS11 = 'POSTOP RESPIRATORY FAILURE    (Risk Adj)'
    %IF &PRDAY. = 1 %THEN %DO;
          RPPS12 = 'POSTOPERATIVE PE OR DVT        (RiskAdj)'
    %END;
    %ELSE %DO;
          RPPS12 = 'POSTOP PE OR DVT-NO PRDAY      (RiskAdj)'
    %END;
          RPPS13 = 'POSTOPERATIVE SEPSIS          (Risk Adj)'
          RPPS14 = 'POSTOPERATIVE WOUND DEHISCENCE (RiskAdj)'
          RPPS15 = 'ACCIDENTAL PUNCTURE/LACERATION(Risk Adj)'
          RPPS16 = 'TRANSFUSION REACTION          (Risk Adj)'
          RPPS17 = 'BIRTH TRAUMA                  (Risk Adj)'
          RPPS18 = 'OB TRAUMA - VAGINAL W INSTRUMENT (RskAd)'
          RPPS19 = 'OB TRAUMA - VAGINAL WO INSTRUMENT (RskA)'
          RPPS20 = 'OB TRAUMA - C-SECTION         (Risk Adj)'
          ;
%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)'
    %IF &PRDAY. = 1 %THEN %DO;
          SPPS09 = 'POSTOP HEMORRHAGE OR HEMATOMA (Smoothed)'
    %END;
    %ELSE %DO;
          SPPS09 = 'POSTOP HEMOR OR HEMAT-NO PRDAY (Smoothd)'
    %END;
          SPPS10 = 'POSTOP PHYSIO METABOL DERANGMNT (Smthed)'
          SPPS11 = 'POSTOP RESPIRATORY FAILURE    (Smoothed)'
    %IF &PRDAY. = 1 %THEN %DO;
          SPPS12 = 'POSTOPERATIVE PE OR DVT       (Smoothed)'
    %END;
    %ELSE %DO;
          SPPS12 = 'POSTOP PE OR DVT-NO PRDAY     (Smoothed)'
    %END;
          SPPS13 = 'POSTOPERATIVE SEPSIS          (Smoothed)'
          SPPS14 = 'POSTOPERATIVE WOUND DEHISCENCE (Smoothd)'
          SPPS15 = 'ACCIDENTAL PUNCTURE/LACERATION(Smoothed)'
          SPPS16 = 'TRANSFUSION REACTION          (Smoothed)'
          SPPS17 = 'BIRTH TRAUMA                  (Smoothed)'
          SPPS18 = 'OB TRAUMA - VAGINAL W INSTRUMENT (Smthd)'
          SPPS19 = 'OB TRAUMA - VAGINAL WO INSTRUMENT (Smth)'
          SPPS20 = 'OB TRAUMA - C-SECTION         (Smoothed)'
    ;
%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.;
        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)"
          ;
        FORMAT TPPS&PS. PPPS&PS. 13.0 OPPS&PS. RPPS&PS. SPPS&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);
   %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" ","
            "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" ","
            "RPPS01" "," "RPPS02" "," "RPPS03" "," "RPPS04" ","
            "RPPS05" "," "RPPS06" "," "RPPS07" "," "RPPS08" ","
            "RPPS09" "," "RPPS10" "," "RPPS11" "," "RPPS12" ","
            "RPPS13" "," "RPPS14" "," "RPPS15" "," "RPPS16" ","
            "RPPS17" "," "RPPS18" "," "RPPS19" "," "RPPS20" ","
            "SPPS01" "," "SPPS02" "," "SPPS03" "," "SPPS04" ","
            "SPPS05" "," "SPPS06" "," "SPPS07" "," "SPPS08" ","
            "SPPS09" "," "SPPS10" "," "SPPS11" "," "SPPS12" ","
            "SPPS13" "," "SPPS14" "," "SPPS15" "," "SPPS16" ","
            "SPPS17" "," "SPPS18" "," "SPPS19" "," "SPPS20"
            ;
        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 ",") "," (RPPS01-RPPS20) (12.10 ",") "," (SPPS01-SPPS20)
           (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 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 (CIDX)    --- ;
        * --- CATEGORIES VARY BY PATIENT SAFETY INDICATOR AND ARE    --- ;
        * --- DETERMINED BY THE FORMAT CORRESPONDING TO N            --- ;
        IF AGE > 0 THEN AIDX = PUT(AGE,A&N.FMT.);
        ELSE AIDX = 0;

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

        * --- SET APPROPRIATE COVARIATE DUMMY TO 1 FOR THIS DISCHARGE  - ;
        IF SEXCAT IN (&S.) THEN SEX = 1;
        IF AIDX > 0 THEN DO; AGEX(AIDX) = 1; SAGEX(AIDX) = 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.FMT.);
             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(220-(2*&A.+&D.+&C.)) 8
               _NAME_ _TYPE_ _LINK_ $8 _STATUS_ $11;
        INFILE COVARP MISSOVER LRECL=3400;
        INPUT  N INTERCEP SEX AGE1-AGE%EVAL(&A.) SAGE1-SAGE%EVAL(&A.)
               D1-D&D. C1-C&C. F1-F%EVAL(220-(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;
        EHAT = ARRYP3(&N.) + (T&PS. - (EXP(E&PS.)/(1 + EXP(E&PS.))));
    RUN;

    PROC SUMMARY DATA=TEMP1Y;
        CLASS  HOSPID AGECAT SEXCAT PAYCAT RACECAT;
        VAR EHAT;
        OUTPUT OUT=R&PS. MEAN=R&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 R&PS=.;
    RUN;

    proc print data=_last_ (obs=100); run;

%MEND;

%MOD3(1,PPS01,13,21,11,'F',0);
%MOD3(2,PPS02,8,24,4,'F',0);
%MOD3(3,PPS03,12,163,13,'F',0);
%MOD3(4,PPS04,10,177,15,'F',0);
%MOD3(5,PPS05,14,12,8,'F',0);
%MOD3(6,PPS06,14,88,12,'F',0);
%MOD3(7,PPS07,14,113,11,'F',0);
%MOD3(8,PPS08,7,10,10,'F',0);
%MOD3(9,PPS09,14,57,11,'F',0);
%MOD3(10,PPS10,12,18,11,'F',0);
%MOD3(11,PPS11,12,30,10,'F',0);
%MOD3(12,PPS12,12,72,11,'F',0);
%MOD3(13,PPS13,11,37,9,'F',0);
%MOD3(14,PPS14,12,12,11,'F',0);
%MOD3(15,PPS15,12,108,13,'F',0);
%MOD3(17,PPS17,2,2,2,'F',1);
%MOD3(18,PPS18,4,2,2,' ',2);
%MOD3(19,PPS19,4,2,2,' ',2);
%MOD3(20,PPS20,4,2,2,' ',2);

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

DATA RISKADJ;
    LENGTH RPPS16 8;
    MERGE RPPS01(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPPS01 PPPS01)
          RPPS02(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPPS02 PPPS02)
          RPPS03(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPPS03 PPPS03)
          RPPS04(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPPS04 PPPS04)
          RPPS05(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPPS05 PPPS05)
          RPPS06(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPPS06 PPPS06)
          RPPS07(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPPS07 PPPS07)
          RPPS08(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPPS08 PPPS08)
          RPPS09(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPPS09 PPPS09)
          RPPS10(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPPS10 PPPS10)
          RPPS11(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPPS11 PPPS11)
          RPPS12(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPPS12 PPPS12)
          RPPS13(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPPS13 PPPS13)
          RPPS14(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPPS14 PPPS14)
          RPPS15(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPPS15 PPPS15)
          RPPS17(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPPS17 PPPS17)
          RPPS18(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPPS18 PPPS18)
          RPPS19(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPPS19 PPPS19)
          RPPS20(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPPS20 PPPS20);
    BY HOSPID AGECAT SEXCAT PAYCAT RACECAT;

    %LBL1

    ARRAY ARRY1{20} RPPS01-RPPS20;
    DO I = 1 TO 20;
             IF ARRY1(I) < 0 AND ARRY1(I) NE . THEN ARRY1(I) = 0;
        ELSE IF ARRY1(I) > 1                   THEN ARRY1(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);
    SET  RISKADJ;
    %INCLUDE MSXP;

    ARRAY ARRYP{20} RPPS01-RPPS20;
    ARRAY ARRYN{20} PPPS01-PPPS20;
    ARRAY ARRYX{20} SPPS01-SPPS20;

    DO I = 1 TO 20;
      ARRYX(I) = ARRYP(I) - ARRYP3(I);
      IF ARRYN(I) NE 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 PPPS01-PPPS20
                            OPPS01-OPPS20 AGECAT SEXCAT PAYCAT RACECAT)
          RISKADJ(KEEP=HOSPID RPPS01-RPPS20 AGECAT SEXCAT PAYCAT RACECAT)
          SMOOTHED(KEEP=HOSPID SPPS01-SPPS20 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
