*==================================================================
*  Title:  PROGRAM A2  CALCULATE 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 2.1 (revision 3), January 2005 <<<
*
*===================================================================;
FILENAME CONTROL "C:\PATHNAME\CONTROL_PSI.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 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.                                                ;
 * -------------------------------------------------------------- ;

 PROC   SUMMARY DATA=INA2.&INFILEA2. NWAY;
 CLASS  %AREA AGECAT SEXCAT RACECAT;
 VAR    TAPS21-TAPS26;
 OUTPUT OUT=NWAY
        SUM(TAPS21-TAPS26)=TAPS21-TAPS26 ;
 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;

 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  PPS{6}  PAPS21-PAPS26;

 DO J = 1 to 6;
    PPS{J} = POP;
 END;

 LABEL
 POP    = 'AREA POPULATION   '
 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)'
 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)'
        ;

 DROP J _FREQ_ _TYPE_;

 RUN;

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

 PROC   SUMMARY DATA=NWAY ;
 CLASS  %AREA AGECAT SEXCAT RACECAT;
 VAR    TAPS21-TAPS26 PAPS21-PAPS26;
 OUTPUT OUT=&OUTFILA2. SUM=;
 RUN;

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

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

 ARRAY  PPS{6}  PAPS21-PAPS26 ;
 ARRAY  TPS{6}  TAPS21-TAPS26 ;
 ARRAY  OPS{6}  OAPS21-OAPS26 ;


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

 DROP J _FREQ_ ;

 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)'
 _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 PRT(PS,TEXT);

 PROC   PRINT DATA=OUTA2.&OUTFILA2. LABEL SPLIT='*';
 VAR    %AREA AGECAT SEXCAT RACECAT TAPS&PS. PAPS&PS. OAPS&PS. ;
 LABEL 
       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;


  %MACRO PRT2;

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

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

 PUT %AREA $5.  "," AGECAT $1.  "," SEXCAT $1.  "," RACECAT $1.  "," _TYPE_ 2.  ","
 (TAPS21-TAPS26) (7.0 ",")
  ","
 (PAPS21-PAPS26) (13.2 ",")
 ","
 (OAPS21-OAPS26) (12.10 ",")
 ;
 RUN;

 %END;

 %MEND TEXT;

 %TEXT
