*==================================================================
*  Title:  PROGRAM A2  CALCULATE OBSERVED AREA RATES FOR AHRQ
*          PREVENTION QUALITY INDICATORS
*
*  Description:
*         USES PROC SUMMARY TO CALCULATE OBSERVED RATES FOR
*         PREVENTION QUALITY INDICATORS ACROSS STRATIFIERS:
*         PROGRAM USES AREA, AGECAT, SEXCAT AND RACECAT
*
*          >>>  VERSION 2.1 (revision 3), January 2004 <<<
*
*===================================================================;
FILENAME CONTROL 'c:\pathname\CONTROL_PQI.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;


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

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

 PROC   SUMMARY DATA=IN2.&INFILE2. NWAY;
 CLASS  %AREA AGECAT SEXCAT RACECAT;
 VAR    TAPQ01-TAPQ16;
 OUTPUT OUT=NWAY
        SUM(TAPQ01-TAPQ16)=TAPQ01-TAPQ16
          N(TAPQ02 TAPQ09)=PAPQ02 PAPQ09 ;
 RUN;

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

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

 PROC   SORT DATA=NWAY (KEEP=%AREA) OUT=%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 POPFILE2 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  PPQ{14}  PAPQ01 PAPQ03-PAPQ08 PAPQ10-PAPQ16;

 DO J = 1 to 14;
    PPQ{J} = POP;
 END;

 IF AGECAT IN ('1') THEN DO;
   TAPQ01 = .; PAPQ01 = .;
   TAPQ03 = .; PAPQ03 = .;
   TAPQ05 = .; PAPQ05 = .;
   TAPQ07 = .; PAPQ07 = .;
   TAPQ08 = .; PAPQ08 = .;
   TAPQ13 = .; PAPQ13 = .;
   TAPQ14 = .; PAPQ14 = .;
   TAPQ15 = .; PAPQ15 = .;
   TAPQ16 = .; PAPQ16 = .;
 END;
 ELSE IF AGECAT IN ('2') THEN DO;
   TAPQ04 = .; PAPQ04 = .;
   TAPQ06 = .; PAPQ06 = .;
   TAPQ09 = .; PAPQ09 = .;
 END;
 ELSE IF AGECAT IN ('3','4') THEN DO;
   TAPQ04 = .; PAPQ04 = .;
   TAPQ06 = .; PAPQ06 = .;
   TAPQ09 = .; PAPQ09 = .;
 END;

 LABEL
 POP    = 'AREA POPULATION                         '
 TAPQ01 = 'DIABETES SHORT TRM COMPLICATN (Numeratr)'
 TAPQ02 = 'PERFORATED APPENDIX           (Numeratr)'
 TAPQ03 = 'DIABETES LONG TERM COMPLICATN (Numeratr)'
 TAPQ04 = 'PEDIATRIC ASTHMA              (Numeratr)'
 TAPQ05 = 'COPD                          (Numeratr)'
 TAPQ06 = 'PEDIATRIC GASTROENTERITIS     (Numeratr)'
 TAPQ07 = 'HYPERTENSION                  (Numeratr)'
 TAPQ08 = 'CONGESTIVE HEART FAILURE      (Numeratr)'
 TAPQ09 = 'LOW BIRTH WEIGHT              (Numeratr)'
 TAPQ10 = 'DEHYDRATION                   (Numeratr)'
 TAPQ11 = 'BACTERIAL PNEUMONIA           (Numeratr)'
 TAPQ12 = 'URINARY INFECTION             (Numeratr)'
 TAPQ13 = 'ANGINA                        (Numeratr)'
 TAPQ14 = 'DIABETES UNCONTROLLED         (Numeratr)'
 TAPQ15 = 'ADULT ASTHMA                  (Numeratr)'
 TAPQ16 = 'LOWER EXTREMITY AMPUTATION    (Numeratr)'
 PAPQ01 = 'DIABETES SHORT TRM CMPLICTN(Denominator)'
 PAPQ02 = 'PERFORATED APPENDIX        (Denominator)'
 PAPQ03 = 'DIABETES LONG TRM CMPLICTN (Denominator)'
 PAPQ04 = 'PEDIATRIC ASTHMA           (Denominator)'
 PAPQ05 = 'COPD                       (Denominator)'
 PAPQ06 = 'PEDIATRIC GASTROENTERITIS  (Denominator)'
 PAPQ07 = 'HYPERTENSION               (Denominator)'
 PAPQ08 = 'CONGESTIVE HEART FAILURE   (Denominator)'
 PAPQ09 = 'LOW BIRTH WEIGHT           (Denominator)'
 PAPQ10 = 'DEHYDRATION                (Denominator)'
 PAPQ11 = 'BACTERIAL PNEUMONIA        (Denominator)'
 PAPQ12 = 'URINARY INFECTION          (Denominator)'
 PAPQ13 = 'ANGINA                     (Denominator)'
 PAPQ14 = 'DIABETES UNCONTROLLED      (Denominator)'
 PAPQ15 = 'ADULT ASTHMA               (Denominator)'
 PAPQ16 = 'LOWER EXTREMITY AMPUTATION (Denominator)'
        ;

 DROP J _FREQ_ _TYPE_;

 RUN;

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

 PROC   SUMMARY DATA=NWAY ;
 CLASS  %AREA AGECAT SEXCAT RACECAT;
 VAR    TAPQ01-TAPQ16 PAPQ01-PAPQ16;
 OUTPUT OUT=&OUTFILA2. SUM=;
 RUN;

 * -------------------------------------------------------------- ;
 * --- CALCULATE OBSERVED RATES                               --- ;
 * --- NOTE THAT OBSERVED RATES OF THE TOTAL AREA MAY OR MAY  --- ;
 * --- NOT BE DIFFERENT DEPENDING ON WHETHER COUNTY-LEVEL OR  --- ;
 * --- MSA-LEVEL AREA STRATIFICATION WAS SPECIFIED.  THIS IS  --- ;
 * --- DUE TO THE INCLUSION OF COUNTY POPULATIONS UNDER MSA-  --- ;
 * --- 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 MSA HAD AT LEAST ONE DISCHARGE.     ;
 *                                                                ;
 * --- DISCHARGES WITH MISSING HOSPSTCO DATA HAVE BEEN        --- ;
 * --- EXCLUDED FROM THESE ANALYSIS.                          --- ;
 * -------------------------------------------------------------- ;
 
 DATA   OUT2.&OUTFILA2. ;
  SET        &OUTFILA2. (WHERE=(_TYPE_ &TYPELEVL));

 ARRAY  PPQ{16}  PAPQ01-PAPQ16 ;
 ARRAY  TPQ{16}  TAPQ01-TAPQ16 ;
 ARRAY  OPQ{16}  OAPQ01-OAPQ16 ;


 DO J = 1 to 16;
         IF TPQ{J} GT 0 AND PPQ{J} GT 0 THEN OPQ{J} = TPQ{J} / PPQ{J};
    ELSE IF                 PPQ{J} GT 0 THEN OPQ{J} = 0 ;
 END;

 DROP J _FREQ_ ;

 LABEL
 OAPQ01 = 'DIABETES SHORT TRM COMPLICATN (Observed)'
 OAPQ02 = 'PERFORATED APPENDIX           (Observed)'
 OAPQ03 = 'DIABETES LONG TERM COMPLICATN (Observed)'
 OAPQ04 = 'PEDIATRIC ASTHMA              (Observed)'
 OAPQ05 = 'COPD                          (Observed)'
 OAPQ06 = 'PEDIATRIC GASTROENTERITIS     (Observed)'
 OAPQ07 = 'HYPERTENSION                  (Observed)'
 OAPQ08 = 'CONGESTIVE HEART FAILURE      (Observed)'
 OAPQ09 = 'LOW BIRTH WEIGHT              (Observed)'
 OAPQ10 = 'DEHYDRATION                   (Observed)'
 OAPQ11 = 'BACTERIAL PNEUMONIA           (Observed)'
 OAPQ12 = 'URINARY INFECTION             (Observed)'
 OAPQ13 = 'ANGINA                        (Observed)'
 OAPQ14 = 'DIABETES UNCONTROLLED         (Observed)'
 OAPQ15 = 'ADULT ASTHMA                  (Observed)'
 OAPQ16 = 'LOWER EXTREMITY AMPUTATION    (Observed)'
 _TYPE_ = 'Stratification level                    '
 ;

 RUN;

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

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


 PROC MEANS DATA=OUT2.&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(PQ,TEXT);

 PROC   PRINT DATA=OUT2.&OUTFILA2. LABEL SPLIT='*';
 VAR    %AREA AGECAT SEXCAT RACECAT TAPQ&PQ. PAPQ&PQ. OAPQ&PQ. ;
 LABEL %AREA    = "%AREA"
       AGECAT  = "AGECAT"
       SEXCAT  = "SEXCAT"
       RACECAT = "RACECAT"
       TAPQ&PQ.   = "TAPQ&PQ.*(Numerator)"
       PAPQ&PQ.   = "PAPQ&PQ.*(Denominator)"
       OAPQ&PQ.   = "OAPQ&PQ.*(Observed)"
       ;
 FORMAT AGECAT $AGECAT.   
        SEXCAT $SEXCAT.
        RACECAT $RACECAT.
		TAPQ&PQ. PAPQ&PQ. COMMA13.0
		OAPQ&PQ. 8.6
   ;
 TITLE4 "Indicator # &PQ.: &TEXT";
 RUN;

 %MEND PRT;

 %PRT(01,DIABETES SHORT TERM CX);
 %PRT(02,PERFORATED APPENDIX);
 %PRT(03,DIABETES LONG TERM CX);
 %PRT(04,PEDIATRIC ASTHMA);
 %PRT(05,COPD);
 %PRT(06,PEDIATRIC GASTROENTERITIS);
 %PRT(07,HYPERTENSION);
 %PRT(08,CHF);
 %PRT(09,LOW BIRTH WEIGHT);
 %PRT(10,DEHYDRATION);
 %PRT(11,BACTERIAL PNEUMONIA);
 %PRT(12,URINARY INFECTION);
 %PRT(13,ANGINA W/O PROCEDURES);
 %PRT(14,DIABETES UNCONTROLLED);
 %PRT(15,ADULT ASTHMA);
 %PRT(16,LOWER EXTREMITY AMPUTATION DM);
 %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 OUT2.&OUTFILA2;
 FILE PQTEXTA2 LRECL=600;
 IF _N_=1 THEN PUT "%Area"  "," "Age"  "," "Sex"  "," "Race"  "," "Type" ","
 "TAPQ01" "," "TAPQ02" "," "TAPQ03" "," "TAPQ04" "," 
 "TAPQ05" "," "TAPQ06" "," "TAPQ07" "," "TAPQ08" ","
 "TAPQ09" "," "TAPQ10" "," "TAPQ11" "," "TAPQ12" "," 
 "TAPQ13" "," "TAPQ14" "," "TAPQ15" "," "TAPQ16" ","
 "PAPQ01" "," "PAPQ02" "," "PAPQ03" "," "PAPQ04" "," 
 "PAPQ05" "," "PAPQ06" "," "PAPQ07" "," "PAPQ08" ","
 "PAPQ09" "," "PAPQ10" "," "PAPQ11" "," "PAPQ12" "," 
 "PAPQ13" "," "PAPQ14" "," "PAPQ15" "," "PAPQ16" ","
 "OAPQ01" "," "OAPQ02" "," "OAPQ03" "," "OAPQ04" "," 
 "OAPQ05" "," "OAPQ06" "," "OAPQ07" "," "OAPQ08" ","
 "OAPQ09" "," "OAPQ10" "," "OAPQ11" "," "OAPQ12" "," 
 "OAPQ13" "," "OAPQ14" "," "OAPQ15" "," "OAPQ16";

 PUT %AREA  "," AGECAT $1.  "," SEXCAT $1.  "," RACECAT $1.  "," _TYPE_ 2.  ","
 (TAPQ01-TAPQ16) (7.0 ",")
  ","
 (PAPQ01-PAPQ16) (13.2 ",") 
 ","
 (OAPQ01-OAPQ16) (12.10 ",")
 ;
 RUN;

 %END;

 %MEND TEXT;

 %TEXT;

