*===================================================================;
*  Title:  PROGRAM A3 PART I:  CALCULATE RISK-ADJUSTED AREA RATES
*          FOR AHRQ INPATIENT QUALITY INDICATORS
*
*  Description:
*         USES PROC SCORE TO CALCULATE RISK-ADJUSTED RATES FOR
*         INPATIENT QUALITY INDICATORS.
*         ADJUSTS FOR: AGE AND SEXCAT.
*
*          >>>  VERSION 2.1 (revision 4), December 2004 <<<
*
*===================================================================;
FILENAME CONTROL 'c:\pathname\CONTROL_IQI.SAS';  *<===USER MUST modify;

%INCLUDE CONTROL;

%MACRO AREA;
   %IF &MSALEVL EQ 1 %THEN MSA;
      %ELSE HOSPSTCO;
%MEND;

%MACRO CTY2MSA;
   %IF &MSALEVL EQ 1 %THEN %DO;
       ATTRIB MSA LENGTH=$5
         LABEL='MSA AREA ASSIGNED FROM HOSPSTCO';
       MSA = INPUT(PUT(HOSPSTCO,AREA.),$5.);
   %END;
%MEND;

%MACRO SUM2MSA;
    %IF &MSALEVL EQ 1 %THEN %DO;
        PROC SUMMARY NWAY;
            CLASS MSA POPCAT SEXCAT;
            VAR POP;
            OUTPUT OUT=QIPOP (DROP=_TYPE_ _FREQ_) SUM=;
        RUN;
    %END;
%MEND;

 OPTIONS COMPRESS=YES;

 TITLE2 'PROGRAM A3 PART I';
 TITLE3 'AHRQ INPATIENT QUALITY INDICATORS: CALCULATE ADJUSTED AREA RATES';

 * ---------------------------------------------------------------- ;
 * --- ADD POPULATION DENOMINATOR --------------------------------- ;
 * --- THIS STEP DETERMINES WHICH AREAS ARE INCLUDED IN THE       - ;
 * --- OUTPUT FROM PROGRAM A1.                                    - ;
 * ---------------------------------------------------------------- ;

 PROC   SORT DATA=INA3.&INFILA3X. OUT=%AREA(KEEP=%AREA) NODUPKEY;
 BY     %AREA;
 RUN;

 DATA   QIPOP;
 LENGTH HOSPSTCO 8 POPCAT 3 SEXCAT $1 ;
 INFILE POPFILA3 MISSOVER;
 INPUT  HOSPSTCO POPCAT SEXCAT POP;
 %CTY2MSA
 RUN;

 %SUM2MSA

 PROC   SORT DATA=QIPOP;
 BY     %AREA POPCAT SEXCAT;
 RUN;

 DATA   QIPOP;
 MERGE  %AREA(IN=X) QIPOP;
 BY     %AREA;

 IF X;

 RUN;

 * -------------------------------------------------------------- ;
 * --- 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 (A)                                          - ;
 * --- A - THE NUMBER OF DEMOGRAPHIC CATEGORIES (AGE AND SEX)   - ;
 * --- S - DETERMINES WHETHER AGE*SEX INTERACTIONS ARE NEEDED   - ;
 * --- O - DETERMINES THE OMITTED AGE CATEGORY IN FORMAT        - ;
 * -------------------------------------------------------------- ;

 %MACRO MOD3(N,IQ,A,S,O);

 * --- THIS SET CREATES TEMP1 WHICH CONTAINS THE DEPENDENT      - ;
 * --- VARIABLE (TIQ) AND INDEPENDENT VARIABLES USED IN         - ;
 * --- REGRESSION.                                              - ;
 * --- IT APPENDS TO THE DISCHARGE DATA ONE OBSERVATION PER     - ;
 * --- AREA AND DEMOGRAPHIC GROUP.                              - ;

 DATA   TEMP1(KEEP=KEY %AREA T&IQ. SEX AGE1-AGE%EVAL(2*&A.) AIDX POP MERGEVAR);
 LENGTH AGE1-AGE%EVAL(2*&A.) AIDX 3;
 SET    INA3.&INFILA3X.(KEEP=KEY %AREA T&IQ. SEXCAT AGECAT AGE)
        QIPOP;

 * --- KEEP ONLY DISCHARGES/AREA/DEMOG GRP NEEDED FOR THIS
       INPATIENT QUALITY INDICATOR                             - ;
 N = &N.;
 IF %AREA EQ . THEN DELETE;

 IF POP > 0 THEN T&IQ.=0;

 IF SEXCAT EQ 'M' THEN TAIQ28=.;

 IF AGECAT IN ('1') OR POPCAT in (1,2,3,4) THEN DO;
   IF N IN (26,27,28,29)
     THEN T&IQ. = .;
 END;
 ELSE IF AGECAT IN ('2') OR POPCAT in (5,6,7,8) THEN DO;
   IF N IN (26,27)
     THEN T&IQ. = .;
 END;

 IF T&IQ. NE .;

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

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

 * --- SET INDEX FOR AGE ARRAY (AIDX).  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 IF POP > 0 THEN DO;
   AIDX = POPCAT;
   IF AIDX = &O. THEN AIDX = 0;
 END;
 ELSE AIDX = 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;

 MERGEVAR = 1;

 RUN;

 PROC   SORT DATA=TEMP1;
 BY %AREA SEX AIDX POP;
 RUN;

 * --- THIS DATA STEP SUBTRACTS THE NUMBER OF DISCHARGES FROM   - ;
 * --- THE AREA/DEMOGRAPHIC GROUP POPULATION COUNT.  IT SETS A  - ;
 * --- WEIGHT OF 1 FOR EACH OBSERVATION FROM THE DISCHARGE DATA - ;
 * --- AND A WEIGHT OF (POP-DISCHARGES) FOR THE APPENDED OBS    - ;
 * --- FOR EACH AREA/DEMOGRAPHIC GROUP.                         - ;

 DATA   TEMP1(KEEP=KEY %AREA T&IQ. SEX AGE1-AGE%EVAL(2*&A.) WHT MERGEVAR);
 SET    TEMP1;
 BY %AREA SEX AIDX;

 RETAIN WCNT 0;

 IF FIRST.AIDX THEN WCNT = 0;

 IF POP > 0 THEN WHT = POP - WCNT;
 ELSE WHT = 1;

 WCNT + 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(40-(2*&A.)));
 LENGTH N F1-F%EVAL(40-(2*&A.)) 3 MSEX MAGE1-MAGE%EVAL(2*&A.) 8;
 INFILE MEANSA MISSOVER LRECL=1000;
 INPUT N MSEX MAGE1-MAGE%EVAL(2*&A.) F1-F%EVAL(40-(2*&A.));

 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 AREA RATE IS THE  ;
 * --- RATE FOR THE AVERAGE PERSON, RATHER THEN FOR THE OMITTED - ;
 * --- CATEGORY.                                                - ;

 DATA   TEMP1(KEEP=KEY %AREA T&IQ. SEX AGE1-AGE%EVAL(2*&A.) WHT);
 MERGE  TEMP1 TEMP1M;
 BY     MERGEVAR;

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

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

 RUN;

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

 DATA   TEMP2(KEEP=INTERCPT SEX AGE1-AGE%EVAL(2*&A.) _TYPE_ _MODEL_ _DEPVAR_ T&IQ.);
 LENGTH INTERCPT SEX AGE1-AGE%EVAL(2*&A.) F1-F%EVAL(40-(2*&A.)) T&IQ. 8
        _TYPE_ _MODEL_ _DEPVAR_ $8;
 INFILE COVARA MISSOVER LRECL=1000;
 INPUT  N INTERCPT SEX AGE1-AGE%EVAL(2*&A.) F1-F%EVAL(40-(2*&A.))
        _DEPVAR_ _MODEL_ _TYPE_ _RMSE_ T&IQ.;

 IF N = &N.;

 RUN;

 * --- THIS PROC CALCULATES A PREDICTED INPATIENT QUALITY         - ;
 * --- INDICATOR FOR EACH OBSERVATION USING THE ACTUAL COVARIATES - ;
 * --- AND THE ESTIMATED COEFFICIENTS.                            - ;
 * --- THE COEFFICENTS HAVE BEEN ESTIMATED ON A DE-MEANED SAMPLE, - ;
 * --- WHERE THE AREA EFFECTS HAVE BEEN AVERAGED OUT.  THEN       - ;
 * --- THE AREA EFFECT IS CALCULATED BY SUBTRACTING THE           - ;
 * --- PREDICTION FROM THE ACTUAL ON EACH OBSERVATION, AND        - ;
 * --- CALCULATING THE MEAN DIFFERENCE FOR THE AREA.              - ;
 * --- 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.);
 RUN;

 PROC   DATASETS NOLIST;
 DELETE TEMP1 TEMP1M TEMP2;
 RUN;

 DATA   TEMP1Y;
 SET    TEMP1Y;

 EHAT = T&IQ. - MHAT;
 ONE = 1;

 RUN;

 PROC   SUMMARY DATA=TEMP1Y ;
 CLASS  %AREA;
 VAR    EHAT ONE;
 OUTPUT OUT=R&IQ. MEAN(EHAT)=R&IQ. SUM(ONE)=P&IQ.;
 WEIGHT WHT;
 RUN;

 PROC   DATASETS NOLIST;
 DELETE TEMP1Y;
 RUN;

 %MEND;

 %MOD3(26,AIQ26,18,'F',12);
 %MOD3(27,AIQ27,18,'F',11);
 %MOD3(28,AIQ28,18,' ',9);
 %MOD3(29,AIQ29,18,'F',9);

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

 DATA   RISKADJ;
 MERGE  RAIQ26(KEEP=%AREA RAIQ26 PAIQ26)
        RAIQ27(KEEP=%AREA RAIQ27 PAIQ27)
        RAIQ28(KEEP=%AREA RAIQ28 PAIQ28)
        RAIQ29(KEEP=%AREA RAIQ29 PAIQ29);
 BY     %AREA;

 LABEL
 RAIQ26 = 'CORONARY ARTERY BYPASS GRAFT (Risk Adj)'
 RAIQ27 = 'PTCA (Risk Adj)'
 RAIQ28 = 'HYSTERECTOMY (Risk Adj)'
 RAIQ29 = 'LAMINECTOMY AND/OR SPINAL FUSN(Risk Adj)'
 ;

 LABEL
 PAIQ26 = 'CORONARY ARTERY BYPASS GRAFT (Den)'
 PAIQ27 = 'PTCA (Denominator)'
 PAIQ28 = 'HYSTERECTOMY (Denominator)'
 PAIQ29 = 'LAMINECTOMY AND/OR SPINAL FUSN (Den)'
        ;

 ARRAY ARRY1{4} RAIQ26-RAIQ29;
 DO I = 1 TO 4;
   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 A3  PART II:  CALCULATE SMOOTHED AREA RATES FOR
*  AHRQ INPATIENT QUALITY INDICATORS.
*
*  Description:
*         CALCULATE SMOOTHED RATES FOR INPATIENT QUALITY INDICATORS
*
*                 >>>  VERSION 2.1  <<<
*
*==================================================================;

 TITLE2 'PROGRAM A3  PART II';
 TITLE3 'AHRQ INPATIENT QUALITY INDICATORS: CALCULATE SMOOTHED AREA RATES';

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

 DATA   SMOOTHED(DROP=RAIQ26-RAIQ29);
 SET    RISKADJ;

 %INCLUDE MSXA;

 ARRAY ARRYA{4} RAIQ26-RAIQ29;
 ARRAY ARRYN{4} PAIQ26-PAIQ29;
 ARRAY ARRYX{4} SAIQ26-SAIQ29;

 DO I = 1 TO 4;
   ARRYX(I) = ARRYA(I) - ARRYA3(I);
   IF ARRYN(I) > 0 AND ARRYA2(I) NE 0 THEN
      ARRYX(I) = ARRYX(I) * (ARRYA2(I) / (ARRYA2(I) + (ARRYA1(I) / ARRYN(I))));
   ELSE ARRYX(I)=.;
   ARRYX(I) = ARRYX(I) + ARRYA3(I);
 END;

 LABEL
 SAIQ26 = 'CORONARY ARTERY BYPASS GRAFT (Smoothed)'
 SAIQ27 = 'PTCA (Smoothed)'
 SAIQ28 = 'HYSTERECTOMY (Smoothed)'
 SAIQ29 = 'LAMINECTOMY AND/OR SPINAL FUSN(Smoothed)'
 ;

 DROP I;

 RUN;


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

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

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

 DATA   OUTA3.&OUTFILA3.;
 MERGE  INA3.&INFILA3Y. (WHERE=(_TYPE_ IN(0,8))
               KEEP=%AREA  _TYPE_ TAIQ26-TAIQ29 PAIQ26-PAIQ29 OAIQ26-OAIQ29)
            RISKADJ(KEEP=%AREA RAIQ26-RAIQ29)
            SMOOTHED(KEEP=%AREA SAIQ26-SAIQ29) ;
 BY             %AREA;
 RUN;

 * -------------------------------------------------------------- ;
 * --- CONTENTS AND MEANS OF AREA MERGED MEANS FILE ------------- ;
 * -------------------------------------------------------------- ;

 PROC CONTENTS DATA=OUTA3.&OUTFILA3. POSITION;
 RUN;

 PROC MEANS DATA=OUTA3.&OUTFILA3. (WHERE=(_TYPE_ IN(8))) N NMISS MIN MAX MEAN SUM;
 TITLE4  'SUMMARY OF AREA-LEVEL RATES (_TYPE_=8)';
 RUN;

 * -------------------------------------------------------------- ;
 * --- PRINT AREA MERGED MEANS FILE ----------------------------- ;
 * -------------------------------------------------------------- ;

  %MACRO PRT2;

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

 %MACRO PRT(IQ,TEXT);

 PROC   PRINT DATA=OUTA3.&OUTFILA3. LABEL SPLIT='*';
 VAR    %AREA TAIQ&IQ. PAIQ&IQ. OAIQ&IQ. RAIQ&IQ. SAIQ&IQ. ;
 LABEL %AREA    = "%AREA"
       TAIQ&IQ.   = "TAIQ&IQ.*(Numerator)"
       PAIQ&IQ.   = "PAIQ&IQ.*(Denominator)"
       OAIQ&IQ.   = "OAIQ&IQ.*(Observed)"
       RAIQ&IQ.   = "RAIQ&IQ.*(Risk Adj)"
       SAIQ&IQ.   = "SAIQ&IQ.*(Smoothed)"
       ;
 FORMAT TAIQ&IQ. PAIQ&IQ. COMMA13.0
  OAIQ&IQ. RAIQ&IQ. SAIQ&IQ. 8.6
   ;
 TITLE4 "FINAL OUTPUT";
 TITLE5 "Indicator # &IQ.: &TEXT";
 RUN;

 %MEND PRT;

 %PRT(26,AREA CABG);
 %PRT(27,AREA PTCA);
 %PRT(28,AREA HYSTERECTOMY);
 %PRT(29,AREA LAMINECTOMY);
 %END;

 %MEND PRT2;

 %PRT2; 

 * -------------------------------------------------------------- ;
 * --- WRITE SAS OUTPUT DATA SET TO TEXT FILE ------------------- ;
 * -------------------------------------------------------------- ;

 %MACRO TEXT;

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

 DATA _NULL_;
   SET OUTA3.&OUTFILA3;
 FILE IQTEXTA3 LRECL=500;
 IF _N_=1 THEN PUT "%AREA"  ","  "Type" ","
 "TAIQ26" "," "TAIQ27" "," "TAIQ28" "," "TAIQ29" ","
 "PAIQ26" "," "PAIQ27" "," "PAIQ28" "," "PAIQ29" ","
 "OAIQ26" "," "OAIQ27" "," "OAIQ28" "," "OAIQ29" ","
 "RAIQ26" "," "RAIQ27" "," "RAIQ28" "," "RAIQ29" ","
 "SAIQ26" "," "SAIQ27" "," "SAIQ28" "," "SAIQ29"
 ;

 PUT %AREA $5.  ","   _TYPE_ 2.  ","
 (TAIQ26-TAIQ29) (7.0 ",")
  ","
 (PAIQ26-PAIQ29) (13.2 ",")
 ","
 (OAIQ26-OAIQ29) (12.10 ",")
 ","
 (RAIQ26-RAIQ29) (12.10 ",")
 ","
 (SAIQ26-SAIQ29) (12.10 ",")
 ;
 RUN;

 %END;

 %MEND TEXT;

 %TEXT;

RUN;
