*===============================================================;
*  Title:  PROGRAM A1  ASSIGN AHRQ PREVENTION QUALITY INDICATORS
*          TO INPATIENT RECORDS
*
*  Description:;
*         ASSIGNS THE OUTCOME OF INTEREST AND POPULATION AT RISK
*         INDICATORS TO INPATIENT RECORDS;
*
*          >>>  VERSION 2.1 (revision 3), January 2004 <<<
*
*  USER NOTE:  Make sure you have created format library 
*              (using PQFMTS.SAS) BEFORE running this program.
*
*===============================================================;
FILENAME CONTROL 'c:\pathname\CONTROL_PQI.SAS'; *<===USER MUST modify;

%INCLUDE CONTROL;


 TITLE2 'PROGRAM A1';
 TITLE3 'AHRQ PREVENTION QUALITY INDICATORS: ASSIGN QIS TO INPATIENT DATA';

 * -------------------------------------------------------------- ;
 * --- PREVENTION QUALITY INDICATOR (PQI) NAMING CONVENTION:  --- ;
 * --- THE FIRST LETTER IDENTIFIES THE PREVENTION QUALITY     --- ;
 * --- INDICATOR AS ONE OF THE FOLLOWING:
                (O) OBSERVED RATES
                (R) RISK-ADJUSTED RATES
                (S) SMOOTHED RATES
                (T) NUMERATOR ("TOP")
                (P) POPULATION ("POP")
 * --- THE SECOND LETTER REFERS TO THE INDICATOR              --- ;
 * --- SUBTYPE, (A)MBULATORY CARE SENSITIVE CONDITION. THE    --- ;
 * --- NEXT TWO CHARACTERS ARE ALWAYS 'PQ'. THE LAST TWO      --- ;
 * --- DIGITS ARE THE INDICATOR NUMBER (WITHIN THAT SUBTYPE). --- ;
 * -------------------------------------------------------------- ;

 %MACRO ADDMSA;
    %IF &MSALEVL EQ 1 %THEN MSA;
 %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;

 %MACRO MDX(FMT);

 (%DO I = 1 %TO &NDX.-1;
  (PUT(DX&I.,&FMT.) = '1') OR
  %END;
  (PUT(DX&NDX.,&FMT.) = '1'))

 %MEND;

 %MACRO MDX1(FMT);

 ((PUT(DX1,&FMT.) = '1'))

 %MEND;

 %MACRO MDX2(FMT);

 (%DO I = 2 %TO &NDX.-1;
  (PUT(DX&I.,&FMT.) = '1') OR
  %END;
  (PUT(DX&NDX.,&FMT.) = '1'))

 %MEND;

 %MACRO MPR(FMT);

 (%DO I = 1 %TO &NPR.-1;
  (PUT(PR&I.,&FMT.) = '1') OR
  %END;
  (PUT(PR&NPR.,&FMT.) = '1'))

 %MEND;


DATA OUT1.&outfile1.
                 (KEEP=KEY SEXCAT AGE AGECAT RACECAT HOSPSTCO
                       HOSPID DRG MDC %ADDMSA
                       TAPQ01-TAPQ16 COMPRESS=YES);
 SET IN1.&infile1.
                 (KEEP=KEY DRG MDC SEX AGE HOSPSTCO
                       HOSPID ASOURCE RACE
                       DX1-DX&NDX. PR1-PR&NPR. );
                       
                       
 * -------------------------------------------------------------- ;
 * --- DIAGNOSIS AND PROCEDURE MACROS --------------------------- ;
 * -------------------------------------------------------------- ;

 * -------------------------------------------------------------- ;
 * --- DELETE RECORDS WITH MISSING VALUES FOR AGE OR SEX--------- ;
 * -------------------------------------------------------------- ;
 IF SEX LE 0 THEN DELETE;
 IF AGE LT 0 THEN DELETE;
 
 
 * -------------------------------------------------------------- ;
 * --- DELETE RECORDS OUTSIDE CONTINENTAL US, AK or HI----------- ;
 * -------------------------------------------------------------- ;
 IF INT(HOSPSTCO/1000) GT 56 THEN DELETE;

 * -------------------------------------------------------------- ;
 * --- CONDITIONALLY DEFINE MSAS FROM COUNTY CODES -------------- ;
 * -------------------------------------------------------------- ;
 %CTY2MSA

 * -------------------------------------------------------------- ;
 * --- DEFINE STRATIFIER: RACE CATEGORY ------------------------- ;
 * -------------------------------------------------------------- ;
 ATTRIB RACECAT LENGTH=$1
   LABEL='(W)hite (B)lack (H)ispanic (O)ther';

 SELECT (RACE);
   WHEN (1)  RACECAT = 'W';
   WHEN (2)  RACECAT = 'B';
   WHEN (3)  RACECAT = 'H';
   OTHERWISE RACECAT = 'O';
 END; * SELECT RACE ;

 * -------------------------------------------------------------- ;
 * --- DEFINE STRATIFIER: AGE CATEGORY  ------------------------- ;
 * -------------------------------------------------------------- ;
 ATTRIB AGECAT LENGTH=$1
   LABEL='(1)0-17 (2)18-39 (3)40-64 (4)65+';

 SELECT;
   WHEN (0  <= AGE < 18)  AGECAT = '1';
   WHEN (18 <= AGE < 40)  AGECAT = '2';
   WHEN (40 <= AGE < 65)  AGECAT = '3';
   WHEN (65 <= AGE     )  AGECAT = '4';
 END; * SELECT AGE ;

 * -------------------------------------------------------------- ;
 * --- DEFINE STRATIFIER: SEX CATEGORY  ------------------------- ;
 * -------------------------------------------------------------- ;
 ATTRIB SEXCAT LENGTH=$1
   LABEL='(M)ale (F)emale';

 SELECT (SEX);
   WHEN (1)  SEXCAT = 'M';
   WHEN (2)  SEXCAT = 'F';
 END; * SELECT SEX ;


 * -------------------------------------------------------------- ;
 * --- DEFINE AREA LEVEL ACSC INDICATORS ------------------------ ;
 * -------------------------------------------------------------- ;

 LENGTH TAPQ01 TAPQ02 TAPQ03 TAPQ04 TAPQ05
        TAPQ06 TAPQ07 TAPQ08 TAPQ09 TAPQ10
        TAPQ11 TAPQ12 TAPQ13 TAPQ14 TAPQ15
        TAPQ16
          3 ;

 LABEL
 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)'
        ;


 * -------------------------------------------------------------- ;
 * --- CONSTRUCT AREA LEVEL ACSC INDICATORS --------------------- ;
 * -------------------------------------------------------------- ;

   * --- DIABETES SHORT TERM COMPLICATION  --- ;

   IF MDC NOTIN (14,15) AND (AGE >= 18) AND

      %MDX1($ACDIASD.) THEN

        TAPQ01 = 1;

   * --- PERFORATED APPENDIX               --- ;

   IF MDC NOTIN (14,15) AND %MDX($ACSAP2D.) THEN DO;

      TAPQ02 = 0;

        IF %MDX($ACSAPPD.) THEN TAPQ02 = 1;

   END;

   * --- DIABETES LONG TERM COMPLICATION   --- ;

   IF MDC NOTIN (14,15) AND (AGE >= 18) AND

      %MDX1($ACDIALD.) THEN

        TAPQ03 = 1;

   * --- PEDIATRIC ASTHMA                  --- ;

   IF MDC NOTIN (14,15) AND (0 <= AGE < 18) AND

      %MDX1($ACSASTD.) THEN

        TAPQ04 = 1;

   * --- COPD                              --- ;

   IF MDC NOTIN (14,15) AND (AGE >= 18) AND

      (%MDX1($ACCOPDD.) OR

       (%MDX1($ACCPD2D.) AND %MDX2($ACCOPDD.))

      ) THEN

        TAPQ05 = 1;

   * --- PEDIATRIC GASTROENTERITIS         --- ;

   IF MDC NOTIN (14,15) AND (0 <= AGE < 18) AND

      %MDX1($ACPGASD.) THEN

        TAPQ06 = 1;

   * --- HYPERTENSION                      --- ;

   IF MDC NOTIN (14,15) AND (AGE >= 18) AND

      %MDX1($ACSHYPD.) AND NOT %MPR($ACSCARP.) THEN

        TAPQ07 = 1;

   * --- CONGESTIVE HEART FAILURE          --- ;

   IF MDC NOTIN (14,15) AND (AGE >= 18) AND

      %MDX1($ACSCHFD.) AND NOT %MPR($ACSCARP.) THEN

      TAPQ08 = 1;

   * --- LOW BIRTH WEIGHT                  --- ;

   IF MDC IN (15) THEN DO;

      TAPQ09 = 0;

      IF %MDX($ACSLBWD.) THEN TAPQ09 = 1;

   END;

   * --- DEHYDRATION                       --- ;

   IF MDC NOTIN (14,15) AND %MDX1($ACSDEHD.) THEN

      TAPQ10 = 1;

   * --- BACTERIAL PNEUMONIA               --- ;

   IF MDC NOTIN (14,15) AND %MDX1($ACSBACD.) AND

      NOT %MDX($ACSBA2D.)  THEN

      TAPQ11 = 1;

   * --- URINARY INFECTION                 --- ;

   IF MDC NOTIN (14,15) AND %MDX1($ACSUTID.) THEN

      TAPQ12 = 1;

   * --- ANGINA                            --- ;

   IF MDC NOTIN (14,15) AND (AGE >= 18) AND

      %MDX1($ACSANGD.) AND NOT %MPR($ACSCARP.) THEN

         TAPQ13 = 1;

   * --- DIABETES UNCONTROLLED             --- ;

   IF MDC NOTIN (14,15) AND (AGE >= 18) AND

      %MDX1($ACDIAUD.) THEN

         TAPQ14 = 1;

   * --- ADULT ASTHMA                      --- ;

   IF MDC NOTIN (14,15) AND (AGE >= 18) AND

      %MDX1($ACSASTD.) THEN

         TAPQ15 = 1;

   * --- LOWER EXTREMITY AMPUTATION       --- ;

   IF MDC NOTIN (14,15) AND (AGE >= 18) AND

      %MPR($ACSLEAP.) AND

      %MDX($ACSLEAD.) AND NOT %MDX($ACLEA2D.) THEN

         TAPQ16 = 1;

 * -------------------------------------------------------------- ;
 * --- EXCLUDE TRANSFERS ---------------------------------------- ;
 * -------------------------------------------------------------- ;

 * --- TRANSFER FROM ANOTHER ---------------- ;
 IF (2 <= ASOURCE <= 3) THEN DO;
   TAPQ01 = .;
   TAPQ02 = .;
   TAPQ03 = .;
   TAPQ04 = .;
   TAPQ05 = .;
   TAPQ06 = .;
   TAPQ07 = .;
   TAPQ08 = .;
   TAPQ09 = .;
   TAPQ10 = .;
   TAPQ11 = .;
   TAPQ12 = .;
   TAPQ13 = .;
   TAPQ14 = .;
   TAPQ15 = .;
   TAPQ16 = .;
 END;

RUN;

* --------------------------------- ;
PROC MEANS DATA=OUT1.&OUTFILE1. N NMISS MIN MAX MEAN SUM;
RUN;

PROC CONTENTS DATA=OUT1.&OUTFILE1. POSITION;
RUN;

PROC PRINT DATA=OUT1.&OUTFILE1.(OBS=24);
TITLE4 "FIRST 24 RECORDS IN OUTPUT DATA SET &OUTFILE1.";
RUN;

