*==================================================================
*  Title:  PROGRAM A2  CALCULATE OBSERVED AREA RATES FOR AHRQ
*          INPATIENT QUALITY INDICATORS
*
*  Description:
*         USES PROC SUMMARY TO CALCULATE OBSERVED RATES FOR
*         INPATIENT QUALITY INDICATORS ACROSS STRATIFIERS:
*         PROGRAM USES AREA, AGECAT, SEXCAT AND RACECAT
*
*          >>>  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;

 OPTIONS COMPRESS=YES;

 TITLE2 'PROGRAM A2';
 TITLE3 'AHRQ INPATIENT QUALITY INDICATORS: CALCULATE OBSERVED AREA RATES';
 run;

 * -------------------------------------------------------------- ;
 * --- Calculate the Numerators (T prefix) for the observed rates ;
 * --- and the Denominators (P prefix) for those Inpatient        ;
 * --- Quality Indicators that are based on the hospitalized      ;
 * --- population.                                                ;
 * -------------------------------------------------------------- ;

 PROC   SUMMARY DATA=INA2.&INFILEA2. NWAY;
 CLASS  %AREA AGECAT SEXCAT RACECAT;
 VAR    TAIQ26-TAIQ29;
 OUTPUT OUT=NWAY
        SUM(TAIQ26-TAIQ29)=TAIQ26-TAIQ29 ;
 RUN;

 PROC   SORT DATA=NWAY;
 BY     %AREA AGECAT SEXCAT RACECAT;
 RUN;

 * -------------------------------------------------------------- ;
 * --- ADD THE AREA POPULATION DENOMINATORS --------------------- ;
 * -------------------------------------------------------------- ;

 PROC   SORT DATA=NWAY OUT=%AREA(KEEP=%AREA) NODUPKEY;
 BY     %AREA;
 RUN;

 * -------------------------------------------------------------- ;
 * --- READ CTY LEVEL POPS AND IF MSALEVL SET TO 1 SUMMARIZE TO-- ;
 * --- MSAS, ELSE SUMMARIZE TO CTY ------------------------------ ;
 * -------------------------------------------------------------- ;

 DATA QIPOP;
    LENGTH HOSPSTCO 8 AGECAT SEXCAT RACECAT $1;
    INFILE POPFILA2 MISSOVER;
    INPUT  HOSPSTCO AGECAT SEXCAT RACECAT POP;
    %CTY2MSA
 RUN;
 
 PROC   SUMMARY DATA=QIPOP NWAY ;
 CLASS  %AREA AGECAT SEXCAT RACECAT;
 VAR    POP;
 OUTPUT OUT=QIPOPSUM SUM(POP)=;
 RUN;

 PROC   SORT DATA=QIPOPSUM;
 BY     %AREA AGECAT SEXCAT RACECAT;
 RUN;

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

 DATA  NWAY;
 MERGE NWAY
       QIPOP(KEEP=%AREA AGECAT SEXCAT RACECAT POP);
 BY    %AREA AGECAT SEXCAT RACECAT;

 IF POP = . AND AGECAT NE '0' AND SEXCAT NE 'O' AND RACECAT NE 'O' THEN
    PUT 'MISSING POPULATION ' %AREA AGECAT SEXCAT RACECAT POP;

 ARRAY  PIQ{4}  PAIQ26-PAIQ29;

 DO J = 1 to 4;
    PIQ{J} = POP;
 END;

 IF SEXCAT EQ 'M' THEN DO;
   TAIQ28=.; PAIQ28=.;
 END;
 IF AGECAT IN ('1') THEN DO;
   TAIQ26 = .; PAIQ26 = .;
   TAIQ27 = .; PAIQ27 = .;
   TAIQ28 = .; PAIQ28 = .;
   TAIQ29 = .; PAIQ29 = .;
 END;
 ELSE IF AGECAT IN ('2') THEN DO;
   TAIQ26 = .; PAIQ26 = .;
   TAIQ27 = .; PAIQ27 = .;
 END;

 LABEL
 POP    = 'AREA POPULATION'
 TAIQ26 = 'CORONARY ARTERY BYPASS GRAFT (Numerator)'
 TAIQ27 = 'PTCA (Numerator)'
 TAIQ28 = 'HYSTERECTORY (Numerator)'
 TAIQ29 = 'LAMINECTOMY/SPINAL FUSION (Numerator)'
 PAIQ26 = 'CORONARY ARTERY BYPASS GRAFT (Den)'
 PAIQ27 = 'PTCA (Denominator)'
 PAIQ28 = 'HYSTERECTOMY (Denominator)'
 PAIQ29 = 'LAMINECTOMY/SPINAL FUSION (Denominator)'
        ;

 DROP J _FREQ_ _TYPE_;

 RUN;

 * -------------------------------------------------------------- ;
 * --- AGGREGATE POPULATION COUNTS, BY STRATIFIERS            --- ;
 * -------------------------------------------------------------- ;

 PROC   SUMMARY DATA=NWAY ;
 CLASS  %AREA AGECAT SEXCAT RACECAT;
 VAR    TAIQ26-TAIQ29 PAIQ26-PAIQ29;
 OUTPUT OUT=&OUTFILA2. SUM=;
 RUN;

 * -------------------------------------------------------------- ;
 * --- CALCULATE OBSERVED RATES                               --- ;
 * -------------------------------------------------------------- ;

 DATA   &OUTFILA2. ;
  SET        &OUTFILA2. (WHERE=(_TYPE_ &TYPELVLA));

 ARRAY  PIQ{4}  PAIQ26-PAIQ29 ;
 ARRAY  TIQ{4}  TAIQ26-TAIQ29 ;
 ARRAY  OIQ{4}  OAIQ26-OAIQ29 ;


 DO J = 1 to 4;
         IF TIQ{J} GT 0 AND PIQ{J} GT 0 THEN OIQ{J} = TIQ{J} / PIQ{J};
    ELSE IF                 PIQ{J} GT 0 THEN OIQ{J} = 0 ;
 END;

 DROP J _FREQ_ ;

 LABEL
 OAIQ26 = 'CORONARY ARTERY BYPASS GRAFT  (Observed)'
 OAIQ27 = 'PTCA                          (Observed)'
 OAIQ28 = 'HYSTERECTORY                  (Observed)'
 OAIQ29 = 'LAMINECTOMY/SPINAL FUSION     (Observed)'
 _TYPE_ = 'STRATIFICATION LEVEL                    '
 ;

 RUN;

 PROC SORT DATA=&OUTFILA2. OUT=OUTA2.&OUTFILA2.;
 BY %AREA;
 RUN;

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

 PROC CONTENTS DATA=OUTA2.&OUTFILA2. POSITION;
 RUN;


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

 * -------------------------------------------------------------- ;
 * --- PRINT AREA OBSERVED MEANS FILE --------------------------- ;
 * -------------------------------------------------------------- ;

  %MACRO PRT2;

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

 %MACRO PRT(IQ,TEXT);

 PROC   PRINT DATA=OUTA2.&OUTFILA2. LABEL SPLIT='*';
 VAR    %AREA AGECAT SEXCAT RACECAT TAIQ&IQ. PAIQ&IQ. OAIQ&IQ. ;
 LABEL %AREA    = "%AREA"
       AGECAT  = "AGECAT"
       SEXCAT  = "SEXCAT"
       RACECAT = "RACECAT"
       TAIQ&IQ.   = "TAIQ&IQ.*(Numerator)"
       PAIQ&IQ.   = "PAIQ&IQ.*(Denominator)"
       OAIQ&IQ.   = "OAIQ&IQ.*(Observed)"
       ;
 FORMAT AGECAT $AGECAT.   
        SEXCAT $SEXCAT.
        RACECAT $RACECAT.
  TAIQ&IQ. PAIQ&IQ. COMMA13.0
  OAIQ&IQ. 8.6
   ;
 TITLE4 "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 COMMA-DELIMITED TEXT FILE --- ;
 * --- FOR EXPORT INTO SPREADSHEETS ----------------------------- ;
 * -------------------------------------------------------------- ;

 %MACRO TEXT;

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

 DATA _NULL_;
   SET OUTA2.&OUTFILA2;
 FILE IQTEXTA2 LRECL=300;
 IF _N_=1 THEN PUT "%AREA"  "," "Age"  "," "Sex"  "," "Race"  "," "Type" ","
 "TAIQ26" "," "TAIQ27" "," "TAIQ28" "," "TAIQ29" ","
 "PAIQ26" "," "PAIQ27" "," "PAIQ28" "," "PAIQ29" ","
 "OAIQ26" "," "OAIQ27" "," "OAIQ28" "," "OAIQ29" "," ;

 PUT %AREA $5.  "," AGECAT $1.  "," SEXCAT $1.  "," RACECAT $1.  "," _TYPE_ 2.  ","
 (TAIQ26-TAIQ29) (7.0 ",")
  ","
 (PAIQ26-PAIQ29) (13.2 ",")
 ","
 (OAIQ26-OAIQ29) (12.10 ",")
 ;
 RUN;

 %END;

 %MEND TEXT;

 %TEXT;
