*===================================================================;
*  Title:  PROGRAM P3 PART 1:  CALCULATE RISK-ADJUSTED PROVIDER
*          RATES FOR AHRQ INPATIENT QUALITY INDICATORS
*
*  Description:
*         USES PROC SCORE TO CALCULATE RISK-ADJUSTED RATES FOR
*         INPATIENT QUALITY INDICATORS.
*         ADJUSTS FOR: AGE, SEXCAT AND APR-DRG SUBCLASSES
*
*          >>>  VERSION 2.1 (revision 3), July 2004 <<<
*
*===================================================================;
FILENAME CONTROL 'c:\pathname\CONTROL_IQI.SAS';  *<===USER MUST modify;

%INCLUDE CONTROL;

OPTIONS COMPRESS=YES;

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

 * -------------------------------------------------------------- ;
 * --- INPATIENT QUALITY INDICATOR ADJUSTED RATES  -------------- ;
 * -------------------------------------------------------------- ;
 * --- THIS STEP SELECTS THE OBSERVATIONS FROM THE PROGRAM 1    - ;
 * --- OUTPUT FOR EACH INPATIENT QUALITY INDICATOR IN TURN.     - ;
 * --- EACH ITERATION PASSES VARIABLES THAT CONTROL THE         - ;
 * --- COVARIATES FOR THAT INPATIENT QUALITY INDICATOR:         - ;
 * --- N - OBSERVATION NUMBER FROM THE MEANS AND COVAR FILES    - ;
 * ---     ALSO IDENTIFIES THE FORMAT USED TO INDEX COVARIATES  - ;
 * --- IQ - THE INPATIENT QUALITY INDICATOR NAME WITHOUT THE    - ;
 * ---      PREFIX (P)                                          - ;
 * --- APRDRG - THE NAME OF THE VARIABLE CONTAINING APR-DRG     - ;
 * --- SUBCLS - THE NAME OF THE VARIABLE CONTAINING THE APR-    - ;
 * ---          DRG SUBCLASS APPROPRIATE FOR THAT INPATIENT     - ;
 * ---          QUALITY INDICATOR                               - ;
 * --- A - THE NUMBER OF DEMOGRAPHIC CATEGORIES (AGE AND SEX)   - ;
 * --- C - THE NUMBER OF APR-DRG CATEGORIES (WITH SUBCLASS)     - ;
 * --- S - DETERMINES WHETHER AGE*SEX INTERACTIONS ARE NEEDED   - ;
 * -------------------------------------------------------------- ;

 %MACRO MOD3(N,IQ,APRDRG,SUBCLS,A,C,S);

 * --- THIS SET CREATES TEMP1 WHICH CONTAINS THE DEPENDENT      - ;
 * --- VARIABLE (TIQ) AND INDEPENDENT VARIABLES USED IN         - ;
 * --- REGRESSION.                                              - ;
 DATA   TEMP1(KEEP=KEY HOSPID T&IQ. SEX AGE1-AGE%EVAL(2*&A.) C1-C&C. MERGEVAR
                   AGECAT SEXCAT PAYCAT RACECAT);
 LENGTH APRDRGX $4 AGE1-AGE%EVAL(2*&A.) C1-C&C. 3;
 SET    INP3.&INFILP3X.;

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

 * --- INITIALIZE COVARIATE DUMMIES TO ZERO --------------------- ;
 ARRAY AGEX{%EVAL(2*&A.)} AGE1-AGE%EVAL(2*&A.);
 ARRAY CX{&C.} C1-C&C.;

 SEX = 0;
 DO I = 1 TO %EVAL(2*&A.);
   AGEX(I)=0;
 END;
 DO I = 1 TO &C.;
   CX(I)=0;
 END;

 * --- SET INDEX FOR AGE ARRAY (AIDX) AND APRDRG ARRAY (CIDX) --- ;
 * --- CATEGORIES VARY BY INPATIENT QUALITY INDICATOR AND ARE --- ;
 * --- DETERMINED BY THE FORMAT CORRESPONDING TO N.           --- ;
 IF AGE > 0 THEN AIDX = PUT(AGE,A&N.IQFMT.); ELSE AIDX = 0;
 IF &APRDRG. > 0 THEN DO;
   APRDRGX = put(&APRDRG.,Z3.)||put(&SUBCLS.,Z1.);
   CIDX = PUT(APRDRGX,C&N.IQFM.);
 END; ELSE CIDX = 0;

 * --- SET APPROPRIATE COVARIATE DUMMY TO 1 FOR THIS DISCHARGE  - ;
 IF SEXCAT IN (&S.) THEN SEX = 1;
 IF AIDX > 0 THEN DO; AGEX(AIDX) = 1; AGEX(&A.+AIDX) = 1 * SEX; END;
 IF CIDX > 0 THEN CX(CIDX) = 1;

 MERGEVAR = 1;

 RUN;

 * --- THIS DATA STEP READS THE OVERALL MEAN FILE FOR THIS      - ;
 * --- INPATIENT QUALITY INDICATOR.  THE F VARIABLES ARE        - ;
 * --- FILLERS.                                                 - ;              

 DATA   TEMP1M(DROP=N F1-F%EVAL(50-(2*&A.+&C.)));
 LENGTH N F1-F%EVAL(50-(2*&A.+&C.)) 3 MSEX MAGE1-MAGE%EVAL(2*&A.) MC1-MC&C. 8;
 INFILE MEANSP MISSOVER LRECL=1200;
 INPUT N MSEX MAGE1-MAGE%EVAL(2*&A.) MC1-MC&C. F1-F%EVAL(50-(2*&A.+&C.));

 MERGEVAR = 1;

 IF N = &N.;

 RUN;

 * --- THIS DATA STEP SUBTRACTS THE OVERALL MEAN OF THE          - ;
 * --- INDEPENDENT VARIABLES FROM THE INDEPENDENT VARIABLES ON   - ;
 * --- EACH OBSERVATION.  THIS WAY THE ADJUSTED PROVIDER RATE IS - ;
 * --- THE RATE FOR THE AVERAGE PERSON, RATHER THEN FOR THE      - ;
 * --- OMITTED CATEGORY.                                         - ;

 DATA   TEMP1(KEEP=KEY HOSPID T&IQ. SEX AGE1-AGE%EVAL(2*&A.) C1-C&C.
                       AGECAT SEXCAT PAYCAT RACECAT);
 MERGE  TEMP1 TEMP1M;
 BY     MERGEVAR;

 ARRAY  COVAR{%EVAL(1+(2*&A.)+&C.)} SEX AGE1-AGE%EVAL(2*&A.) C1-C&C.;
 ARRAY  MEANS{%EVAL(1+(2*&A.)+&C.)} MSEX MAGE1-MAGE%EVAL(2*&A.) MC1-MC&C.;

 IF &APRDRGFG. = 1 THEN DO;

    DO I = 1 TO %EVAL(1+(2*&A.)+&C.);
      COVAR(I) = COVAR(I) - MEANS(I);
    END;

 END;
 ELSE DO;

    DO I = 1 TO %EVAL(1+(2*&A.));
      COVAR(I) = COVAR(I) - MEANS(I);
    END;

 END;

 RUN;

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

 DATA   TEMP2(KEEP=INTERCEP SEX AGE1-AGE%EVAL(2*&A.) C1-C&C. _TYPE_ _MODEL_ _DEPVAR_ T&IQ.);
 LENGTH INTERCEP SEX AGE1-AGE%EVAL(2*&A.) C1-C&C. F1-F%EVAL(50-(2*&A.+&C.)) T&IQ. 8
        _TYPE_ _MODEL_ _DEPVAR_ $8;
 INFILE COVARP MISSOVER LRECL=1200;
 INPUT  N INTERCEP SEX AGE1-AGE%EVAL(2*&A.) C1-C&C. F1-F%EVAL(50-(2*&A.+&C.))
        _DEPVAR_ _MODEL_ _TYPE_ _RMSE_ T&IQ.;

 IF N = &N.;

 RUN;

 * --- THIS PROC CALCULATES A PREDICTED IQI FOR EACH OBSERVATION  - ;
 * --- USING THE ACTUAL COVARIATES AND THE ESTIMATED COEFFICIENTS - ;
 * --- THE COEFFICENTS HAVE BEEN ESTIMATED ON A DE-MEANED SAMPLE, - ;
 * --- WHERE THE PROVIDER EFFECTS HAVE BEEN AVERAGED OUT.  THEN   - ;
 * --- THE PROVIDER EFFECT IS CALCULATED BY SUBTRACTING THE       - ;
 * --- PREDICTION FROM THE ACTUAL ON EACH OBSERVATION, AND        - ;
 * --- CALCULATING THE MEAN DIFFERENCE FOR THE PROVIDER.          - ;
 * --- FOR A DISCUSSION OF THIS METHOD, PLEASE REFER TO GREENE,   - ;
 * --- WILLIAN H, ECONOMETRIC ANALYSIS, 2ND ED (1990) P. 466-69   - ;

 PROC   SCORE DATA=TEMP1 SCORE=TEMP2 TYPE=PARMS OUT=TEMP1Y;
 VAR    SEX AGE1-AGE%EVAL(2*&A.) C1-C&C.;
 RUN;

 PROC   DATASETS NOLIST;
 DELETE TEMP1 TEMP1M TEMP2;
 RUN;

 * --- THIS PROC CALCULATES A PREDICTED IQI FOR EACH OBSERVATION  - ;
 * --- USING THE ACTUAL COVARIATES AND THE ESTIMATED COEFFICIENTS - ;

 DATA   TEMP1Y;
 SET    TEMP1Y;

 EHAT = T&IQ. - MHAT;

 RUN;

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

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

 DATA   R&IQ.;
 MERGE  R&IQ. &INFILP3Y.(IN=X);
 BY     HOSPID AGECAT SEXCAT PAYCAT RACECAT;

 IF X;

 IF P&IQ. LT 3 THEN R&IQ=.;

 RUN;

 %MEND;

 %MOD3(8,PIQ08,&APRDRG.,&MORTAL.,7,4,'F');
 %MOD3(9,PIQ09,&APRDRG.,&MORTAL.,8,4,'F');
 %MOD3(10,PIQ10,&APRDRG.,&MORTAL.,3,20,'F');
 %MOD3(11,PIQ11,&APRDRG.,&MORTAL.,6,8,'F');
 %MOD3(12,PIQ12,&APRDRG.,&MORTAL.,7,12,'F');
 %MOD3(13,PIQ13,&APRDRG.,&MORTAL.,13,8,'F');
 %MOD3(14,PIQ14,&APRDRG.,&MORTAL.,8,4,'F');
 %MOD3(15,PIQ15,&APRDRG.,&MORTAL.,9,28,'F');
 %MOD3(16,PIQ16,&APRDRG.,&MORTAL.,7,4,'F');
 %MOD3(17,PIQ17,&APRDRG.,&MORTAL.,8,12,'F');
 %MOD3(18,PIQ18,&APRDRG.,&MORTAL.,10,16,'F');
 %MOD3(19,PIQ19,&APRDRG.,&MORTAL.,5,12,'F');
 %MOD3(20,PIQ20,&APRDRG.,&MORTAL.,10,8,'F');
 %MOD3(21,PIQ21,&APRDRG.,&SEVERE.,4,2,' ');
 %MOD3(22,PIQ22,&APRDRG.,&SEVERE.,4,2,' ');
 %MOD3(23,PIQ23,&APRDRG.,&SEVERE.,12,2,'F');
 %MOD3(24,PIQ24,&APRDRG.,&SEVERE.,4,36,'F');
 %MOD3(25,PIQ25,&APRDRG.,&SEVERE.,8,24,'F');
 %MOD3(30,PIQ30,&APRDRG.,&MORTAL.,8,8,'F');
 %MOD3(31,PIQ31,&APRDRG.,&MORTAL.,6,4,'F');
 %MOD3(32,PIQ32,&APRDRG.,&MORTAL.,9,28,'F');
 %MOD3(33,PIQ33,&APRDRG.,&SEVERE.,4,2,' ');
 %MOD3(34,PIQ34,&APRDRG.,&SEVERE.,4,2,' ');

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

 DATA   RISKADJ;
 MERGE
        RPIQ08(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPIQ08 PPIQ08)
        RPIQ09(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPIQ09 PPIQ09)
        RPIQ10(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPIQ10 PPIQ10)
        RPIQ11(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPIQ11 PPIQ11)
        RPIQ12(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPIQ12 PPIQ12)
        RPIQ13(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPIQ13 PPIQ13)
        RPIQ14(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPIQ14 PPIQ14)
        RPIQ15(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPIQ15 PPIQ15)
        RPIQ16(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPIQ16 PPIQ16)
        RPIQ17(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPIQ17 PPIQ17)
        RPIQ18(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPIQ18 PPIQ18)
        RPIQ19(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPIQ19 PPIQ19)
        RPIQ20(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPIQ20 PPIQ20)
        RPIQ21(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPIQ21 PPIQ21)
        RPIQ22(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPIQ22 PPIQ22)
        RPIQ23(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPIQ23 PPIQ23)
        RPIQ24(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPIQ24 PPIQ24)
        RPIQ25(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPIQ25 PPIQ25)
        RPIQ30(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPIQ30 PPIQ30)
        RPIQ31(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPIQ31 PPIQ31)
        RPIQ32(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPIQ32 PPIQ32)
        RPIQ33(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPIQ33 PPIQ33)
        RPIQ34(KEEP=HOSPID AGECAT SEXCAT PAYCAT RACECAT RPIQ34 PPIQ34);
 BY     HOSPID AGECAT SEXCAT PAYCAT RACECAT;

 LABEL
 RPIQ08 ='IN-HOSP MORT ESOPHL RESECTION (Risk Adj)'
 RPIQ09 ='IN-HOSP MORT PANCRC RESECTION (Risk Adj)'
 RPIQ10 ='IN-HOSP MORT PEDIC HEART SURG (Risk Adj)'
 RPIQ11 ='IN-HOSP MORT AAA REPAIR       (Risk Adj)'
 RPIQ12 ='IN-HOSP MORT CABG             (Risk Adj)'
 RPIQ13 ='IN-HOSP MORT CRANIOTOMY       (Risk Adj)'
 RPIQ14 ='IN-HOSP MORT HIP REPLACEMENT  (Risk Adj)'
 RPIQ15 ='IN-HOSP MORT AMI              (Risk Adj)'
 RPIQ16 ='IN-HOSP MORT CHF              (Risk Adj)'
 RPIQ17 ='IN-HOSP MORT STROKE           (Risk Adj)'
 RPIQ18 ='IN-HOSP MORT GI HEMORRHAGE    (Risk Adj)'
 RPIQ19 ='IN-HOSP MORT HIP FRACTURE     (Risk Adj)'
 RPIQ20 ='IN-HOSP MORT PNEUMONIA        (Risk Adj)'
 RPIQ21 ='CESAREAN SECTION DELIVERY     (Risk Adj)'
 RPIQ22 ='VAG BTH AFT C_SECTION,UNCOMPL (Risk Adj)'
 RPIQ23 ='LAPAROSCOPIC CHOLECYSTECTOMY  (Risk Adj)'
 RPIQ24 ='INCIDENTAL APPENDECTOMY       (Risk Adj)'
 RPIQ25 ='BI-LATERAL CATHETERIZATION    (Risk Adj)'
 RPIQ30 ='IN-HOSP MORT PTCA             (Risk Adj)'
 RPIQ31 ='IN-HOSP MORT CAROTID ENDARTERECTMY (RAd)'
 RPIQ32 ='IN-HOSP MORT AMI,w/o TRANSFERS(Risk Adj)'
 RPIQ33 ='PRIMARY CESAREAN SECTION      (Risk Adj)'
 RPIQ34 ='VBAC, ALL                     (Risk Adj)'
 ;

 ARRAY ARRY1{23} RPIQ08-RPIQ25 RPIQ30-RPIQ34;
 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;
 END;

 DROP I;

 RUN;

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

 TITLE2 'PROGRAM P3  PART II';
 TITLE3 'AHRQ INPATIENT QUALITY INDICATORS: CALCULATE SMOOTHED PROVIDER RATES';

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

 DATA   SMOOTHED(DROP=RPIQ08-RPIQ25 RPIQ30-RPIQ34);
 SET            RISKADJ;

 %INCLUDE MSXP;

 ARRAY ARRYP{23} RPIQ08-RPIQ25 RPIQ30-RPIQ34;
 ARRAY ARRYN{23} PPIQ08-PPIQ25 PPIQ30-PPIQ34;
 ARRAY ARRYX{23} SPIQ08-SPIQ25 SPIQ30-SPIQ34;

 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;

 LABEL
 SPIQ08 ='IN-HOSP MORT ESOPHL RESECTION (Smoothed)'
 SPIQ09 ='IN-HOSP MORT PANCRC RESECTION (Smoothed)'
 SPIQ10 ='IN-HOSP MORT PEDIC HEART SURG (Smoothed)'
 SPIQ11 ='IN-HOSP MORT AAA REPAIR       (Smoothed)'
 SPIQ12 ='IN-HOSP MORT CABG             (Smoothed)'
 SPIQ13 ='IN-HOSP MORT CRANIOTOMY       (Smoothed)'
 SPIQ14 ='IN-HOSP MORT HIP REPLACEMENT  (Smoothed)'
 SPIQ15 ='IN-HOSP MORT AMI              (Smoothed)'
 SPIQ16 ='IN-HOSP MORT CHF              (Smoothed)'
 SPIQ17 ='IN-HOSP MORT STROKE           (Smoothed)'
 SPIQ18 ='IN-HOSP MORT GI HEMORRHAGE    (Smoothed)'
 SPIQ19 ='IN-HOSP MORT HIP FRACTURE     (Smoothed)'
 SPIQ20 ='IN-HOSP MORT PNEUMONIA        (Smoothed)'
 SPIQ21 ='CESAREAN SECTION DELIVERY     (Smoothed)'
 SPIQ22 ='VAG BTH AFT C-SECTION,UNCOMPL (Smoothed)'
 SPIQ23 ='LAPAROSCOPIC CHOLECYSTECTOMY  (Smoothed)'
 SPIQ24 ='INCIDENTAL APPENDECTOMY       (Smoothed)'
 SPIQ25 ='BI-LATERAL CATHETERIZATION    (Smoothed)'
 SPIQ30 ='IN-HOSP MORT PTCA             (Smoothed)'
 SPIQ31 ='IN-HOSP MORT CAROTID ENDARTERECTMY (Smt)'
 SPIQ32 ='IN-HOSP MORT AMI,w/o TRANSFERS(Smoothed)'
 SPIQ33 ='PRIMARY CESAREAN SECTION      (Smoothed)'
 SPIQ34 ='VBAC, ALL                     (Smoothed)'
;

 DROP I;

 RUN;

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

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

 * ---------------------------------------------------------------- ;
 * --- INPATIENT QUALITY INDICATOR MERGED RATES                   - ;
 * ---------------------------------------------------------------- ;

 DATA   OUTP3.&OUTFILP3.;
 MERGE  INP3.&INFILP3Y.(KEEP=HOSPID _TYPE_ TPIQ01-TPIQ25 TPIQ30-TPIQ34 
                             PPIQ08-PPIQ25 PPIQ30-PPIQ34 OPIQ08-OPIQ25 OPIQ30-OPIQ34
                             AGECAT SEXCAT PAYCAT RACECAT)
           RISKADJ(KEEP=HOSPID RPIQ08-RPIQ25 RPIQ30-RPIQ34 AGECAT SEXCAT PAYCAT RACECAT)
           SMOOTHED(KEEP=HOSPID SPIQ08-SPIQ25 SPIQ30-SPIQ34 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 ------------------------------- ;
 * -------------------------------------------------------------- ;

 %MACRO PRT2;

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

 %MACRO PRT(IQ,TEXT,VOLUME);

 PROC   PRINT DATA=OUTP3.&OUTFILP3. LABEL SPLIT='*';
 %IF &VOLUME=0 %THEN %DO;
 VAR    HOSPID AGECAT SEXCAT PAYCAT RACECAT TPIQ&IQ. PPIQ&IQ. OPIQ&IQ. RPIQ&IQ. SPIQ&IQ.;
 LABEL HOSPID  = "HOSPID"
       AGECAT  = "AGECAT"
       SEXCAT  = "SEXCAT"
       PAYCAT  = "PAYCAT"
       RACECAT = "RACECAT"
       TPIQ&IQ.   = "TPIQ&IQ.*(Numerator)"
       PPIQ&IQ.   = "PPIQ&IQ.*(Denominator)"
       OPIQ&IQ.   = "OPIQ&IQ.*(Observed)"
       RPIQ&IQ.   = "RPIQ&IQ.*(Risk Adj)"
       SPIQ&IQ.   = "SPIQ&IQ.*(Smoothed)"
       ;
 FORMAT TPIQ&IQ. PPIQ&IQ. 13.0 OPIQ&IQ. RPIQ&IQ. SPIQ&IQ. 8.6;
 %END;
 %ELSE %DO;
 VAR    HOSPID AGECAT SEXCAT PAYCAT RACECAT TPIQ&IQ. ;
 LABEL HOSPID  = "HOSPID"
       AGECAT  = "AGECAT"
       SEXCAT  = "SEXCAT"
       PAYCAT  = "PAYCAT"
       RACECAT = "RACECAT"
       TPIQ&IQ.   = "TPIQ&IQ.*(Numerator)"
       ;
 FORMAT TPIQ&IQ. 13.0;
 %END;
 FORMAT AGECAT $AGECAT.
        SEXCAT $SEXCAT.
        PAYCAT $PAYCAT.
        RACECAT $RACECAT.
   ;
 TITLE4 "FINAL OUTPUT";
 TITLE5 "Indicator # &IQ.: &TEXT";
 RUN;

 %MEND PRT;

 %PRT(01,VOLUME ESOPHAGEAL RESECTION,1);
 %PRT(02,VOLUME PANCREATIC RESECTION,1);
 %PRT(03,VOLUME PEDIATRIC HEART SURGERY,1);
 %PRT(04,VOLUME AAA REPAIR,1);
 %PRT(05,VOLUME CABG,1); 
 %PRT(06,VOLUME PTCA,1);
 %PRT(07,VOLUME CAROTID ENDARTERECTOMY,1);
 %PRT(08,IN-HOSP MORT ESOPHAGEAL RESECTION,0);
 %PRT(09,IN-HOSP MORT PANCREATIC RESECTION,0);
 %PRT(10,IN-HOSP MORT PEDIATRIC HEART SURGERY,0);
 %PRT(11,IN-HOSP MORT AAA REPAIR,0);
 %PRT(12,IN-HOSP MORT CABG,0);
 %PRT(13,IN-HOSP MORT CRANIOTOMY,0);
 %PRT(14,IN-HOSP MORT HIP REPLACEMENT,0);
 %PRT(15,IN-HOSP MORT AMI,0);
 %PRT(16,IN-HOSP MORT CHF,0);
 %PRT(17,IN-HOSP MORT STROKE,0);
 %PRT(18,IN-HOSP MORT GI HEMORRHAGE,0);
 %PRT(19,IN-HOSP MORT HIP FRACTURE,0);
 %PRT(20,IN-HOSP MORT PNEUMONIA,0);
 %PRT(21,CESAREAN SECTION DELIVERY,0);
 %PRT(22,VAG BTH AFT C-SECTION-UNCOMPLICATED,0);
 %PRT(23,LAPAROSCOPIC CHOLECYSTECTOMY,0);
 %PRT(24,INCIDENTAL APPENDECTOMY AMONG ELDERLY,0);
 %PRT(25,BI-LATERAL CARDIAC CATHETERIZATION,0);
 %PRT(30,IN-HOSP MORT PTCA,0);
 %PRT(31,IN-HOSP MORT CAROTID ENDARTERECTOMY,0);
 %PRT(32,IN-HOSP MORT AMI-WITHOUT TRANSFER CASES,0);
 %PRT(33,PRIMARY CESAREAN SECTION,0);
 %PRT(34,VBAC-ALL,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 IQTEXTP3 LRECL=2000;
 IF _N_=1 THEN PUT "Hosp Id"  "," "Age"  "," "Sex"  "," "Payer" "," "Race"  "," "Type" ","
 "TPIQ01" "," "TPIQ02" "," "TPIQ03" "," "TPIQ04" ","
 "TPIQ05" "," "TPIQ06" "," "TPIQ07" "," "TPIQ08" ","
 "TPIQ09" "," "TPIQ10" "," "TPIQ11" "," "TPIQ12" ","
 "TPIQ13" "," "TPIQ14" "," "TPIQ15" "," "TPIQ16" ","
 "TPIQ17" "," "TPIQ18" "," "TPIQ19" "," "TPIQ20" ","
 "TPIQ21" "," "TPIQ22" "," "TPIQ23" "," "TPIQ24" ","
 "TPIQ25" "," "TPIQ30" "," "TPIQ31" "," "TPIQ32" "," 
 "TPIQ33" "," "TPIQ34" "," 
 "PPIQ08" ","
 "PPIQ09" "," "PPIQ10" "," "PPIQ11" "," "PPIQ12" ","
 "PPIQ13" "," "PPIQ14" "," "PPIQ15" "," "PPIQ16" ","
 "PPIQ17" "," "PPIQ18" "," "PPIQ19" "," "PPIQ20" ","
 "PPIQ21" "," "PPIQ22" "," "PPIQ23" "," "PPIQ24" "," 
 "PPIQ25" "," "PPIQ30" "," "PPIQ31" "," "PPIQ32" "," 
 "PPIQ33" "," "PPIQ34" ","
 "OPIQ08" ","
 "OPIQ09" "," "OPIQ10" "," "OPIQ11" "," "OPIQ12" ","
 "OPIQ13" "," "OPIQ14" "," "OPIQ15" "," "OPIQ16" ","
 "OPIQ17" "," "OPIQ18" "," "OPIQ19" "," "OPIQ20" ","
 "OPIQ21" "," "OPIQ22" "," "OPIQ23" "," "OPIQ24" ","
 "OPIQ25" "," "OPIQ30" "," "OPIQ31" "," "OPIQ32" "," 
 "OPIQ33" "," "OPIQ34" ","
 "RPIQ08" ","
 "RPIQ09" "," "RPIQ10" "," "RPIQ11" "," "RPIQ12" ","
 "RPIQ13" "," "RPIQ14" "," "RPIQ15" "," "RPIQ16" ","
 "RPIQ17" "," "RPIQ18" "," "RPIQ19" "," "RPIQ20" ","
 "RPIQ21" "," "RPIQ22" "," "RPIQ23" "," "RPIQ24" ","
 "RPIQ25" "," "RPIQ30" "," "RPIQ31" "," "RPIQ32" "," 
 "RPIQ33" "," "RPIQ34" ","
 "SPIQ08" ","
 "SPIQ09" "," "SPIQ10" "," "SPIQ11" "," "SPIQ12" ","
 "SPIQ13" "," "SPIQ14" "," "SPIQ15" "," "SPIQ16" ","
 "SPIQ17" "," "SPIQ18" "," "SPIQ19" "," "SPIQ20" ","
 "SPIQ21" "," "SPIQ22" "," "SPIQ23" "," "SPIQ24" ","
 "SPIQ25" "," "SPIQ30" "," "SPIQ31" "," "SPIQ32" "," 
 "SPIQ33" "," "SPIQ34"  
 ;

 PUT HOSPID 13.  "," AGECAT $1.  "," SEXCAT $1. "," PAYCAT $1. "," RACECAT $1.  ","
 _TYPE_ 2.  ","
 (TPIQ01-TPIQ25 TPIQ30-TPIQ34) (7.0 ",")
  ","
 (PPIQ08-PPIQ25 PPIQ30-PPIQ34) (13.2 ",")
 ","
 (OPIQ08-OPIQ25 OPIQ30-OPIQ34) (12.10 ",")
 ","
 (RPIQ08-RPIQ25 RPIQ30-RPIQ34) (12.10 ",")
 ","
 (SPIQ08-SPIQ25 SPIQ30-SPIQ34) (12.10 ",")
 ;
 RUN;

 %END;

 %MEND TEXT;

 %TEXT;
