*==================================================================
*  Title:  PROGRAM A2 CALCULATES OBSERVED AREA RATES FOR AHRQ
*          PATIENT SAFETY INDICATORS
*
*  Description:
*         USES PROC SUMMARY TO CALCULATE OBSERVED RATES FOR
*         PATIENT SAFETY INDICATORS ACROSS STRATIFIERS:
*         PROGRAM USES AREA, AGECAT, SEXCAT AND RACECAT
*
*          >>>  VERSION 4.2, September 2010           <<<
*
*===================================================================;
FILENAME CONTROL "C:\PATHNAME\CONTROL_PSI.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 PATIENT SAFETY INDICATORS: CALCULATE OBSERVED AREA RATES';

 * -------------------------------------------------------------- ;
 * --- 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    TAPS21-TAPS27;
 OUTPUT OUT=TEMP1
        SUM(TAPS21-TAPS27)=TAPS21-TAPS27;
 RUN;

 PROC   SORT DATA=TEMP1
                  (KEEP=MAREA AGECAT SEXCAT RACECAT
                        TAPS21-TAPS27 _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 MA  -- ;
 * --- 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 PPS1{7} PAPS21-PAPS27;

 DO J = 1 to 7;
    PPS1{J} = POP;
 END;

 IF AGECAT IN (0) THEN DO;
   TAPS21 = .; PAPS21 = .;
   TAPS22 = .; PAPS22 = .;
   TAPS23 = .; PAPS23 = .;
   TAPS24 = .; PAPS24 = .;
   TAPS25 = .; PAPS25 = .;
   TAPS26 = .; PAPS26 = .;
   TAPS27 = .; PAPS27 = .;
 END;

 RUN;

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

 PROC   SUMMARY DATA=TEMP1;
 CLASS  MAREA AGECAT SEXCAT RACECAT;
 VAR    TAPS21-TAPS27 PAPS21-PAPS27;
 OUTPUT OUT=TEMP2 SUM=;
 RUN;

 DATA &OUTFILA2.;
 SET TEMP2;

 IF _TYPE_ &TYPELVLA;

 ARRAY PPS2{7} PAPS21-PAPS27 ;
 ARRAY TPS{7} TAPS21-TAPS27 ;
 ARRAY OPS{7} OAPS21-OAPS27 ;

 DO J = 1 to 7;
    IF TPS{J} GT 0 AND PPS2{J} GT 0 THEN OPS{J} = TPS{J} / PPS2{J};
    ELSE IF PPS2{J} GT 0 THEN OPS{J} = 0 ;
 END;

 LABEL
 TAPS21 = 'FOREIGN BODY LEFT IN DURING PROC (Num)'
 TAPS22 = 'IATROGENIC PNEUMOTHORAX (Numerator)'
 TAPS23 = 'INFECTION DUE TO MEDICAL CARE (Num)'
 TAPS24 = 'POSTOPERATIVE WOUND DEHISCENCE (Num)'
 TAPS25 = 'TECH DIFFICULTY W PROCEDURE (Numerator)'
 TAPS26 = 'TRANSFUSION REACTION (Numerator)'
 TAPS27 = 'POSTOP HEMORRHAGE OR HEMATOMA (Num)'
 ;

 LABEL
 PAPS21 = 'FOREIGN BODY LEFT IN DURING PROC (Den)'
 PAPS22 = 'IATROGENIC PNEUMOTHORAX (Denominator)'
 PAPS23 = 'INFECTION DUE TO MEDICAL CARE (Den)'
 PAPS24 = 'POSTOPERATIVE WOUND DEHISCENCE (Den)'
 PAPS25 = 'TECH DIFFICULTY W PROCEDURE (Denominator)'
 PAPS26 = 'TRANSFUSION REACTION (Denominator)'
 PAPS27 = 'POSTOP HEMORRHAGE OR HEMATOMA (Den)'
 ;

 LABEL
 OAPS21 = 'FOREIGN BODY LEFT IN DURING PROC (Obs)'
 OAPS22 = 'IATROGENIC PNEUMOTHORAX (Observed)'
 OAPS23 = 'INFECTION DUE TO MEDICAL CARE (Observed)'
 OAPS24 = 'POSTOPERATIVE WOUND DEHISCENCE (Observd)'
 OAPS25 = 'TECH DIFFICULTY W PROCEDURE (Observed)'
 OAPS26 = 'TRANSFUSION REACTION (Observed)'
 OAPS27 = 'POSTOP HEMORRHAGE OR HEMATOMA (Obs)'
 _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(PS,TEXT);

 PROC  PRINT DATA=OUTA2.&OUTFILA2. LABEL SPLIT='*';
 VAR   MAREA AGECAT SEXCAT RACECAT TAPS&PS. PAPS&PS. OAPS&PS. ;
 LABEL MAREA   = "MAREA"
       AGECAT  = "AGECAT"
       SEXCAT  = "SEXCAT"
       RACECAT = "RACECAT"
       TAPS&PS.   = "TAPS&PS.*(Numerator)"
       PAPS&PS.   = "PAPS&PS.*(Denominator)"
       OAPS&PS.   = "OAPS&PS.*(Observed)"
       ;
 FORMAT AGECAT AGECAT.   
        SEXCAT SEXCAT.
        RACECAT RACECAT.
        TAPS&PS. PAPS&PS. COMMA13.0
	  OAPS&PS. 8.6
        ;
 TITLE4 "Indicator # &PS.: &TEXT";
 RUN;

 %MEND PRT;

 %PRT(21,FOREIGN BODY LEFT IN DURING PROCEDURE);
 %PRT(22,IATROGENIC PNEUMOTHORAX);
 %PRT(23,INFECTION DUE TO MEDICAL CARE);
 %PRT(24,POST-OPERATIVE WOUND DEHISCENCE);
 %PRT(25,TECHNICAL DIFFICULTY WITH PROCEDURE);
 %PRT(26,TRANSFUSION REACTION);
 %PRT(27,POSTOP HEMORRHAGE OR HEMATOMA);

 %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 PSTEXTA2 LRECL=1000;
 IF _N_=1 THEN PUT "MAREA" "," "Age" "," "Sex" "," "Race" "," "Type" ","
 "TAPS21" "," "TAPS22" "," "TAPS23" "," "TAPS24" "," "TAPS25" "," "TAPS26" "," "TAPS27" ","
 "PAPS21" "," "PAPS22" "," "PAPS23" "," "PAPS24" "," "PAPS25" "," "PAPS26" "," "PAPS27" ","
 "OAPS21" "," "OAPS22" "," "OAPS23" "," "OAPS24" "," "OAPS25" "," "OAPS26" "," "OAPS27";

 PUT MAREA $5. "," AGECAT 3. "," SEXCAT 3. "," RACECAT 3. "," _TYPE_ 2. ","
 (TAPS21-TAPS27) (7.0 ",")
 ","
 (PAPS21-PAPS27) (13.2 ",")
 ","
 (OAPS21-OAPS27) (12.10 ",")
 ;
 RUN;

 %END;

 %MEND TEXT;

 %TEXT
