*==================================================================
*  Title:  PROGRAM A2 CALCULATES 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 4.1, December 2009 <<<
*
*===================================================================;
FILENAME CONTROL 'C:\PATHNAME\CONTROL_IQI.SAS';  *<===USER MUST modify;

%INCLUDE CONTROL;


%MACRO CTY2MA;
    %IF &MALEVL EQ 0 %THEN %DO;
        ATTRIB MAREA LENGTH=$5
          LABEL='FIPS STATE COUNTY CODE';
        MAREA = FIPSTCO;
    %END;
    %ELSE %IF &MALEVL EQ 1 %THEN %DO;
        ATTRIB MAREA LENGTH=$5
          LABEL='MODIFIED FIPS';
        MAREA = INPUT(PUT(FIPSTCO,$M1AREA.),$5.);
    %END;
    %ELSE %IF &MALEVL EQ 2 %THEN %DO;
        ATTRIB MAREA LENGTH=$5
          LABEL='OMB 1999 METRO AREA';
        MAREA = INPUT(PUT(FIPSTCO,$M2AREA.),$5.);
    %END;
    %ELSE %IF &MALEVL EQ 3 %THEN %DO;
        ATTRIB MAREA LENGTH=$5
          LABEL='OMB 2003 METRO AREA';
        MAREA = INPUT(PUT(FIPSTCO,$M3AREA.),$5.);
    %END;
 %MEND;

 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.                                                ;
 * -------------------------------------------------------------- ;

 DATA   TEMP0;
 SET    IN1.&INFILE1.;

 %CTY2MA

 RUN;

 PROC   SUMMARY DATA=TEMP0 NWAY;
 CLASS  MAREA AGECAT SEXCAT RACECAT;
 VAR    TAIQ26-TAIQ29;
 OUTPUT OUT=TEMP1
        SUM(TAIQ26-TAIQ29)=TAIQ26-TAIQ29;
 RUN;

 PROC   SORT DATA=TEMP1
                  (KEEP=MAREA AGECAT SEXCAT RACECAT
                        TAIQ26-TAIQ29 _TYPE_);
 BY     MAREA AGECAT SEXCAT RACECAT;
 RUN;

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

 PROC   SORT DATA=TEMP1 OUT=MAREA(KEEP=MAREA) NODUPKEY;
 BY     MAREA;
 RUN;

 * -------------------------------------------------------------- ;
 * --- READ CTY LEVEL POPS AND IF MALEVL SET TO 1,2,3 SUMMARIZE - ;
 * --- TO METRO AREA, ELSE SUMMARIZE TO CTY --------------------- ;
 * -------------------------------------------------------------- ;

 DATA QIPOP0;
    LENGTH FIPSTCO $5 SEXCAT POPCAT AGECAT RACECAT 3 
           POP_1995 POP_1996 POP_1997 POP_1998 POP_1999
           POP_2000 POP_2001 POP_2002 POP_2003 POP_2004
           POP_2005 POP_2006 POP_2007 POP_2008 POP_2009 POP 8;

    INFILE POPFILE MISSOVER FIRSTOBS=2;

    INPUT FIPSTCO SEXCAT POPCAT RACECAT 
          POP_1995 POP_1996 POP_1997 POP_1998 POP_1999
          POP_2000 POP_2001 POP_2002 POP_2003 POP_2004
          POP_2005 POP_2006 POP_2007 POP_2008 POP_2009;

    %CTY2MA

    IF POPCAT IN (1,2,3,4)            THEN AGECAT = 0;
    ELSE IF POPCAT IN (5,6,7,8)       THEN AGECAT = 1;
    ELSE IF POPCAT IN (9,10,11,12,13) THEN AGECAT = 2;
    ELSE IF POPCAT IN (14,15)         THEN AGECAT = 3;
    ELSE                                   AGECAT = 4;

    POP = POP_&POPYEAR.;

 RUN;
 
 PROC   SUMMARY DATA=QIPOP0 NWAY;
 CLASS  MAREA AGECAT SEXCAT RACECAT;
 VAR    POP;
 OUTPUT OUT=QIPOP SUM(POP)=;
 RUN;

 PROC   SORT DATA=QIPOP;
 BY     MAREA AGECAT SEXCAT RACECAT;
 RUN;

 DATA   QIPOP(KEEP=MAREA AGECAT SEXCAT RACECAT POP);
 MERGE  MAREA(IN=X) QIPOP;
 BY     MAREA;
 IF X;
 RUN;

 * -------------------------------------------------------------- ;
 * --- CALCULATE OBSERVED RATES                               --- ;
 * --- NOTE THAT OBSERVED RATES OF THE TOTAL AREA MAY OR MAY  --- ;
 * --- NOT BE DIFFERENT DEPENDING ON WHETHER COUNTY-LEVEL OR  --- ;
 * --- MA-LEVEL AREA STRATIFICATION WAS SPECIFIED.  THIS IS   --- ;
 * --- DUE TO THE INCLUSION OF COUNTY POPULATIONS UNDER MA-   --- ;
 * --- LEVEL STRATIFICATION THAT WOULD OTHERWISE BE EXCLUDED  --- ;
 * --- UNDER COUNTY-LEVEL STRATIFICATION.  UNDER COUNTY-LEVEL --- ;
 * --- STRATIFICATION, A COUNTY WOULD BE INCLUDED ONLY IF THERE - ;
 * --- WAS AT LEAST ONE DISCHARGE FROM THAT COUNTY.  UNDER MSA- - ;
 * --- LEVEL STRATIFICATION, A COUNTY WOULD BE INCLUDED IF ANY- - ;
 * --- COUNTY WITHIN THE SAME MA HAD AT LEAST ONE DISCHARGE.      ;
 *                                                                ;
 * --- DISCHARGES WITH MISSING PSTCO DATA HAVE BEEN           --- ;
 * --- EXCLUDED FROM THESE ANALYSIS.                          --- ;
 * -------------------------------------------------------------- ;

 DATA TEMP1;
 MERGE TEMP1 QIPOP;
 BY    MAREA AGECAT SEXCAT RACECAT;

 IF POP = . THEN
    PUT 'MISSING POPULATION ' MAREA AGECAT SEXCAT RACECAT POP;

 ARRAY PIQ1{4} PAIQ26-PAIQ29;

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

 IF SEXCAT IN (1) THEN DO;
   TAIQ28=.; PAIQ28=.;
 END;
 IF AGECAT IN (0) THEN DO;
   TAIQ26 = .; PAIQ26 = .;
   TAIQ27 = .; PAIQ27 = .;
   TAIQ28 = .; PAIQ28 = .;
   TAIQ29 = .; PAIQ29 = .;
 END;
 IF AGECAT IN (1) THEN DO;
   TAIQ26 = .; PAIQ26 = .;
   TAIQ27 = .; PAIQ27 = .;
 END;

 RUN;

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

 PROC   SUMMARY DATA=TEMP1;
 CLASS  MAREA AGECAT SEXCAT RACECAT;
 VAR    TAIQ26-TAIQ29 PAIQ26-PAIQ29;
 OUTPUT OUT=TEMP2 SUM=;
 RUN;

 DATA &OUTFILA2.;
 SET TEMP2;

 IF _TYPE_ &TYPELVLA;

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

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

 LABEL
 TAIQ26 = 'CORONARY ARTERY BYPASS GRAFT (Numerator)'
 TAIQ27 = 'PTCA (Numerator)'
 TAIQ28 = 'HYSTERECTORY (Numerator)'
 TAIQ29 = 'LAMINECTOMY/SPINAL FUSION (Numerator)'
 ;
 LABEL
 PAIQ26 = 'CORONARY ARTERY BYPASS GRAFT (Den)'
 PAIQ27 = 'PTCA (Denominator)'
 PAIQ28 = 'HYSTERECTOMY (Denominator)'
 PAIQ29 = 'LAMINECTOMY/SPINAL FUSION (Denominator)'
 ;
 LABEL
 OAIQ26 = 'CORONARY ARTERY BYPASS GRAFT  (Observed)'
 OAIQ27 = 'PTCA                          (Observed)'
 OAIQ28 = 'HYSTERECTORY                  (Observed)'
 OAIQ29 = 'LAMINECTOMY/SPINAL FUSION     (Observed)'
 _TYPE_ = 'STRATIFICATION LEVEL                    '
 ;

 DROP J;

 RUN;

 PROC SORT DATA=&OUTFILA2. OUT=OUTA2.&OUTFILA2.;
 BY MAREA AGECAT SEXCAT RACECAT;
 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   MAREA AGECAT SEXCAT RACECAT TAIQ&IQ. PAIQ&IQ. OAIQ&IQ. ;
 LABEL MAREA   = "MAREA"
       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=1000;
 IF _N_=1 THEN PUT "MAREA"  "," "Age"  "," "Sex"  "," "Race"  "," "Type" ","
 "TAIQ26" "," "TAIQ27" "," "TAIQ28" "," "TAIQ29" ","
 "PAIQ26" "," "PAIQ27" "," "PAIQ28" "," "PAIQ29" ","
 "OAIQ26" "," "OAIQ27" "," "OAIQ28" "," "OAIQ29";

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

 %END;

 %MEND TEXT;

 %TEXT;
