*=================================================================;
*  Title:  PROGRAM 1  ASSIGN AHRQ PATIENT SAFETY INDICATORS
*          TO INPATIENT RECORDS
*
*  Description:
*         ASSIGNS THE OUTCOME OF INTEREST AND POPULATION AT RISK
*         INDICATORS TO INPATIENT RECORDS.  ALSO ASSIGNS 
*         COMORBIDITY CATEGORIES.
*
*                 >>>  VERSION 2.1  <<<
*
*  USER NOTE:  Make sure you have created the format library
*              using PSFMTS.SAS BEFORE running this program.
*
*=================================================================;
FILENAME CONTROL "C:\PATHNAME\CONTROL.SAS";   *<==USER MUST modify;

%INCLUDE CONTROL;

 TITLE2 'PROGRAM 1';
 TITLE3 'AHRQ PATIENT SAFETY INDICATORS: ASSIGN PSIS TO INPATIENT DATA';

 * -------------------------------------------------------------- ;
 * --- PATIENT SAFETY INDICATOR (PSI) NAMING CONVENTION:      --- ;
 * --- THE FIRST LETTER IDENTIFIES THE PATIENT SAFETY         --- ;
 * --- 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 IDENTIFIES THE PSI AS A PROVIDER (P) --- ;
 * --- OR AN AREA (A) LEVEL INDICATOR.  THE                   --- ;
 * --- NEXT TWO CHARACTERS ARE ALWAYS 'PS'. THE LAST TWO      --- ;
 * --- DIGITS ARE THE INDICATOR NUMBER (WITHIN THAT SUBTYPE). --- ;
 * -------------------------------------------------------------- ;

 %MACRO ADDPRDAY; 
    %IF &PRDAY EQ 1 %THEN PRDAY1-PRDAY&NPR. ;
 %MEND;

 %MACRO ADDMSA;
    %IF &MSALEVL EQ 1 %THEN MSA;
 %MEND;

 %MACRO CTY2MSA;
    %IF &MSALEVL EQ 1 %THEN %DO;
        LABEL MSA ='MSA AREA ASSIGNED FROM HOSPSTCO';
        MSA = INPUT(PUT(HOSPSTCO,AREA.),5.);
        IF MSA EQ . THEN MSA = HOSPSTCO;
    %END;
 %MEND;

 DATA OUT1.&OUTFILE1. (
      DROP=DISP PAY1 RACE SEX DX1-DX&NDX. PR1-PR&NPR. %ADDPRDAY 
           I J DXVALUE A1-A35 HTN HTNCX HTNCHF HCHFRF HTNRF DMNB
           CARDFLG PERIFLG CEREFLG PULMFLG DIABFLG HYPOFLG RENALFLG 
           RENFFLG LIVERFLG ULCEFLG HIVFLG LEUKFLG CANCFLG ARTHFLG
           NUTRFLG ANEMFLG ALCFLG);
   SET IN1.&INFILE1. (
      KEEP=KEY DRG MDC SEX AGE HOSPSTCO PAY1 HOSPID DISP RACE LOS ASOURCE ATYPE
           DX1-DX&NDX. PR1-PR&NPR. %ADDPRDAY );
           
 * -------------------------------------------------------------- ;
 * --- DIAGNOSIS AND PROCEDURE MACROS --------------------------- ;
 * -------------------------------------------------------------- ;

 %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;

 %MACRO MPR2(FMT,EQ);

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

 %MEND;

 %MACRO MPR2B(FMT);

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

 %MEND;

 %MACRO MDR(FMT);

 (PUT(PUT(DRG,Z3.),&FMT.) = '1')

 %MEND;

 * -------------------------------------------------------------- ;
 * --- DEFINE COMORBIDITY CATEGORIES ---------------------------- ;
 * -------------------------------------------------------------- ;

 ATTRIB 
 CHF      LENGTH=3
   LABEL='CONGESTIVE HEART FAILURE'
 ARYTHM   LENGTH=3
   LABEL='ARRHYTHMIAS'
 VALVE    LENGTH=3
   LABEL='VALVULAR DISEASE'
 PULMCIRC LENGTH=3
   LABEL='PULMONARY CIRCULATION DISEASE'
 PERIVASC LENGTH=3
   LABEL='PERIPHERAL VASCULAR DISEASE'
 HTN_C    LENGTH=3
   LABEL='HYPERTENSION'
 PARA     LENGTH=3
   LABEL='PARALYSIS'
 NEURO    LENGTH=3
   LABEL='OTHER NEUROLOGICAL DISORDERS'
 CHRNLUNG LENGTH=3
   LABEL='CHRONIC PULMONARY DISEASE'
 DM       LENGTH=3
   LABEL='DIABETES'
 DMCX     LENGTH=3
   LABEL='DIABETES W CHRONIC COMPLICATIONS'
 HYPOTHY  LENGTH=3
   LABEL='HYPOTHYROIDISM'
 RENLFAIL LENGTH=3
   LABEL='RENAL FAILURE'
 LIVER    LENGTH=3
   LABEL='LIVER DISEASE'
 ULCER    LENGTH=3
   LABEL='PEPTIC ULCER DISEASE X BLEEDING'
 AIDS     LENGTH=3
   LABEL='ACQUIRED IMMUNE DEFICIENCY SYNDROME'
 LYMPH    LENGTH=3
   LABEL='LYMPHOMA'
 METS     LENGTH=3
   LABEL='METASTATIC CANCER'
 TUMOR    LENGTH=3
   LABEL='SOLID TUMOR WOUT METASTASIS'
 ARTH     LENGTH=3
   LABEL='RHEUMATOID ARTHRITIS COLLAGEN VAS'
 COAG     LENGTH=3
   LABEL='COAGULOPTHY'
 OBESE    LENGTH=3
   LABEL='OBESITY'
 WGHTLOSS LENGTH=3
   LABEL='WEIGHT LOSS'
 LYTES    LENGTH=3
   LABEL='FLUID AND ELECTROLYTE DISORDERS'
 BLDLOSS  LENGTH=3
   LABEL='CHRONIC BLOOD LOSS ANEMIA'
 ANEMDEF  LENGTH=3
   LABEL='DEFICIENCY ANEMIAS'
 ALCOHOL  LENGTH=3
   LABEL='ALCOHOL ABUSE'
 DRUG     LENGTH=3
   LABEL='DRUG ABUSE'
 PSYCH    LENGTH=3
   LABEL='PSYCHOSES'
 DEPRESS  LENGTH=3
   LABEL='DEPRESSION'
;

 LENGTH A1  A2  A3  A4  A5  A6  A7  A8  A9  A10
        A11 A12 A13 A14 A15 A16 A17 A18 A19 A20
        A21 A22 A23 A24 A25 A26 A27 A28 A29 A30
        A31 A32 A33 A34 A35 DXVALUE $8
;

 * ------------------------------------------------------------- ;
 * --- CREATION OF COMORBIDITY VARIABLES                         ;
 * ---                                                           ; 
 * --- Creates comorbidity variables based on the                ; 
 * --- presence of secondary diagnoses and redefines             ; 
 * --- comorbidity group by eliminating DRGs directly            ; 
 * --- related to them.                                          ; 
 * --------------------------------------------------------------; 

 * ------------------------------------------------------------- ;
 * ---  Declare variables as array elements                      ;
 * ------------------------------------------------------------- ;

   ARRAY ARRY1{&NDX.}  DX1 - DX&NDX.;

   ARRAY ARRY2{35}  CHF      HTNCHF   HCHFRF   ARYTHM   VALVE    
                    PULMCIRC PERIVASC HTN      HTNCX    PARA     
                    NEURO    CHRNLUNG DM       DMCX     DMNB 
                    HYPOTHY  RENLFAIL HTNRF    LIVER    ULCER
                    AIDS     LYMPH    METS     TUMOR    ARTH
                    COAG     OBESE    WGHTLOSS LYTES    BLDLOSS  
                    ANEMDEF  ALCOHOL  DRUG     PSYCH    DEPRESS ;

   ARRAY ARRY3{35}  A1  A2  A3  A4  A5  A6  A7  A8  A9  A10
                    A11 A12 A13 A14 A15 A16 A17 A18 A19 A20
                    A21 A22 A23 A24 A25 A26 A27 A28 A29 A30
                    A31 A32 A33 A34 A35 ;


 * ------------------------------------------------------------- ;
 * ---  Initialize ARRY2 to 0 and assigns the variable           ;
 * ---  name from ARRY2 as the VALUE of ARRY3                    ;
 * ------------------------------------------------------------- ;

   DO I = 1 TO 35;
      ARRY2(I) = 0;
      CALL VNAME(ARRY2(I),ARRY3(I));
   END;

 * ------------------------------------------------------------- ;
 * ---  Looking at the secondary DXs and using formats,          ;
 * ---  create DXVALUE to define each comorbidity group          ;
 * ---                                                           ;
 * ---  If DXVALUE is equal to the comorbidity name in           ;
 * ---  array ARRY3 then a value of 1 is assigned to the         ;
 * ---  corresponding comorbidity group in array ARRY2           ;
 * ------------------------------------------------------------- ;

   DO I = 2 TO &NDX.;
      DXVALUE = PUT(ARRY1(I),$RCOMFMT.) ;
      DO J = 1 TO 35;
         IF DXVALUE = ARRY3(J) THEN ARRY2(J) = 1;
      END;
   END;

 * ------------------------------------------------------------- ;
 * ---  SET UP CODE TO ONLY COUNT THE MORE SEVERE COMORBIDITY    ;
 * ------------------------------------------------------------- ;

   IF HTNCHF = 1 THEN DO; CHF = 1; HTNCX = 1; END;
   IF HCHFRF = 1 THEN DO; CHF = 1; RENLFAIL = 1; HTNCX = 1; END;
   IF HTNRF = 1 THEN DO; RENLFAIL = 1; HTNCX = 1; END;
   IF DMNB = 1 AND AGE < 1 THEN DMCX = 1; 
   IF HTNCX = 1 THEN HTN = 0 ;
   IF METS = 1 THEN TUMOR = 0 ;
   IF DMCX = 1 THEN DM = 0;

 * ------------------------------------------------------------- ;
 * ---  EXAMINE DRG AND SET FLAGS TO IDENTIFY A PARTICULAR       ;
 * ---  DRG GROUP                                                ;
 * ------------------------------------------------------------- ;

   IF PUT(DRG,CARDDRG.)  = 'YES' THEN CARDFLG  = 1;
   IF PUT(DRG,PERIDRG.)  = 'YES' THEN PERIFLG  = 1;
   IF PUT(DRG,CEREDRG.)  = 'YES' THEN CEREFLG  = 1;
   IF PUT(DRG,PULMDRG.)  = 'YES' THEN PULMFLG  = 1;
   IF PUT(DRG,DIABDRG.)  = 'YES' THEN DIABFLG  = 1;
   IF PUT(DRG,HYPODRG.)  = 'YES' THEN HYPOFLG  = 1;
   IF PUT(DRG,RENALDRG.) = 'YES' THEN RENALFLG = 1;
   IF PUT(DRG,RENFDRG.)  = 'YES' THEN RENFFLG  = 1;
   IF PUT(DRG,LIVERDRG.) = 'YES' THEN LIVERFLG = 1;
   IF PUT(DRG,ULCEDRG.)  = 'YES' THEN ULCEFLG  = 1;
   IF PUT(DRG,HIVDRG.)   = 'YES' THEN HIVFLG   = 1;
   IF PUT(DRG,LEUKDRG.)  = 'YES' THEN LEUKFLG  = 1;
   IF PUT(DRG,CANCDRG.)  = 'YES' THEN CANCFLG  = 1;
   IF PUT(DRG,ARTHDRG.)  = 'YES' THEN ARTHFLG  = 1;
   IF PUT(DRG,NUTRDRG.)  = 'YES' THEN NUTRFLG  = 1;
   IF PUT(DRG,ANEMDRG.)  = 'YES' THEN ANEMFLG  = 1;
   IF PUT(DRG,ALCDRG.)   = 'YES' THEN ALCFLG   = 1;


  * ------------------------------------------------------------- ;
  * ---  REDEFINING COMORBIDITIES BY ELIMINATING THE DRG DIRECTLY ;
  * ---  RELATED TO COMORBIDITY, THUS LIMITING THE SCREENS TO     ;
  * ---  PRINCIPAL DIAGNOSES NOT DIRECTLY RELATED TO COMORBIDITY  ;
  * ---  IN QUESTION                                              ;
  * ------------------------------------------------------------- ;

   IF CHF      AND  CARDFLG  THEN CHF      = 0;
   IF ARYTHM   AND  CARDFLG  THEN ARYTHM   = 0;
   IF VALVE    AND  CARDFLG  THEN VALVE    = 0;
   IF PULMCIRC AND (CARDFLG OR DRG = 88) 
                             THEN PULMCIRC = 0;
   IF PERIVASC AND  PERIFLG  THEN PERIVASC = 0;
   IF HTN      AND  DRG=134  THEN HTN      = 0;
   IF HTNCX    AND (CARDFLG OR RENALFLG OR DRG = 134)
                             THEN HTNCX    = 0;
   IF NEURO    AND  MDC = 1  THEN NEURO    = 0;
   IF CHRNLUNG AND  PULMFLG  THEN CHRNLUNG = 0;
   IF DM       AND  DIABFLG  THEN DM       = 0;
   IF DMCX     AND  DIABFLG  THEN DMCX     = 0 ;
   IF HYPOTHY  AND  HYPOFLG  THEN HYPOTHY  = 0;
   IF RENLFAIL AND  RENFFLG  THEN RENLFAIL = 0;
   IF LIVER    AND  LIVERFLG THEN LIVER    = 0;
   IF ULCER    AND  ULCEFLG  THEN ULCER    = 0;
   IF AIDS     AND  HIVFLG   THEN AIDS     = 0;
   IF LYMPH    AND  LEUKFLG  THEN LYMPH    = 0;
   IF METS     AND  CANCFLG  THEN METS     = 0;
   IF TUMOR    AND  CANCFLG  THEN TUMOR    = 0;
   IF ARTH     AND  ARTHFLG  THEN ARTH     = 0;
   IF COAG     AND  DRG=397  THEN COAG     = 0;
   IF OBESE    AND (NUTRFLG OR DRG = 288 ) 
                             THEN OBESE    = 0;
   IF WGHTLOSS AND  NUTRFLG  THEN WGHTLOSS = 0;
   IF LYTES    AND NUTRFLG   THEN LYTES    = 0;
   IF BLDLOSS  AND ANEMFLG   THEN BLDLOSS  = 0;
   IF ANEMDEF  AND ANEMFLG   THEN ANEMDEF  = 0;
   IF ALCOHOL  AND ALCFLG    THEN ALCOHOL  = 0;
   IF DRUG     AND ALCFLG    THEN DRUG     = 0;
   IF PSYCH    AND DRG=430   THEN PSYCH    = 0;
   IF DEPRESS  AND DRG=426   THEN DEPRESS  = 0;

  * ------------------------------------------------------------- ;
  * --- Combine HTN and HTNCX into HTN_C                          ;
  * ------------------------------------------------------------- ;
   IF HTN=1 OR HTNCX=1 THEN HTN_C=1;
     ELSE HTN_C=0;


 * -------------------------------------------------------------- ;
 * --- 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: PAYER CATEGORY ------------------------ ;
 * -------------------------------------------------------------- ;
 ATTRIB PAYCAT LENGTH=$1
   LABEL='(M)edicar Me(D)icaid (P)riv (S)elf (O)th';

 SELECT (PAY1);
   WHEN (1)  PAYCAT = 'M';
   WHEN (2)  PAYCAT = 'D';
   WHEN (3)  PAYCAT = 'P';
   WHEN (4)  PAYCAT = 'S';
   OTHERWISE PAYCAT = 'O';
 END; * SELECT PAY1 ;

 * -------------------------------------------------------------- ;
 * --- 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 PROVIDER LEVEL INDICATORS ------------------------- ;
 * -------------------------------------------------------------- ;

 %MACRO LBL;

 ATTRIB
 TPPS01 LENGTH=3
   LABEL='COMPLICATIONS OF ANESTHESIA  (Numerator)'
 TPPS02 LENGTH=3
   LABEL='DEATH IN LOW MORTALITY DRGS  (Numerator)'
 TPPS02A LENGTH=3
   LABEL='DEATH IN LMD - ADULT MEDICAL (Numerator)'
 TPPS02B LENGTH=3
   LABEL='DEATH IN LMD - PED MEDICAL   (Numerator)'
 TPPS02C LENGTH=3
   LABEL='DEATH IN LMD - ADLT SURGICAL (Numerator)'
 TPPS02D LENGTH=3
   LABEL='DEATH IN LMD - PED SURGICAL  (Numerator)'
 TPPS02E LENGTH=3
   LABEL='DEATH IN LMD - OBSTETRIC     (Numerator)'
 TPPS02F LENGTH=3
   LABEL='DEATH IN LMD - PSYCHIATRIC   (Numerator)'
 TPPS03 LENGTH=3
   LABEL='DECUBITUS ULCER              (Numerator)'
 TPPS04 LENGTH=3
   LABEL='FAILURE TO RESCUE            (Numerator)'
 TPPS05 LENGTH=3
   LABEL='FOREIGN BODY LEFT IN DURING PROC (Numer)'
 TPPS06 LENGTH=3
   LABEL='IATROGENIC PNEUMOTHORAX      (Numerator)'
 TPPS07 LENGTH=3
   LABEL='INFECTION DUE TO MEDICAL CARE (Numeratr)'
 TPPS08 LENGTH=3
   LABEL='POSTOPERATIVE HIP FRACTURE   (Numerator)'
 %IF &PRDAY. = 1 %THEN %DO;
 TPPS09 LENGTH=3
   LABEL='POSTOP HEMORRHAGE OR HEMATOMA (Numeratr)'
 %END;
 %ELSE %DO;
 TPPS09 LENGTH=3
   LABEL='POSTOP HEMOR OR HEMAT-NO PRDAY (Numertr)'
 %END;
 TPPS10 LENGTH=3
   LABEL='POSTOP PHYSIO METABOL DERANGMNT (Numrtr)'
 TPPS11 LENGTH=3
   LABEL='POSTOP RESPIRATORY FAILURE   (Numerator)'
 %IF &PRDAY. = 1 %THEN %DO;
 TPPS12 LENGTH=3
   LABEL='POSTOPERATIVE PE OR DVT      (Numerator)'
 %END;
 %ELSE %DO;
 TPPS12 LENGTH=3
   LABEL='POSTOP PE OR DVT-NO PRDAY    (Numerator)'
 %END;
 TPPS13 LENGTH=3
   LABEL='POSTOPERATIVE SEPSIS         (Numerator)'
 TPPS14 LENGTH=3
   LABEL='POSTOPERATIVE WOUND DEHISCENCE (Numertr)'
 TPPS15 LENGTH=3
   LABEL='ACCIDENTAL PUNCTURE/LACERATION(Numeratr)'
 TPPS16 LENGTH=3
   LABEL='TRANSFUSION REACTION         (Numerator)'
 TPPS17 LENGTH=3
   LABEL='BIRTH TRAUMA                 (Numerator)'
 TPPS18 LENGTH=3
   LABEL='OB TRAUMA - VAGINAL W INSTRUMENT (Numer)'
 TPPS19 LENGTH=3
   LABEL='OB TRAUMA - VAGINAL WO INSTRUMENT (Numr)'
 TPPS20 LENGTH=3
   LABEL='OB TRAUMA - C-SECTION        (Numerator)'
;

 * -------------------------------------------------------------- ;
 * --- DEFINE AREA LEVEL INDICATORS ----------------------------- ;
 * -------------------------------------------------------------- ;
 ATTRIB
 TAPS21 LENGTH=3
   LABEL='FOREIGN BODY LEFT IN DURING PROC (Numer)'
 TAPS22 LENGTH=3
   LABEL='IATROGENIC PNEUMOTHORAX      (Numerator)'
 TAPS23 LENGTH=3
   LABEL='INFECTION DUE TO MEDICAL CARE (Numeratr)'
 TAPS24 LENGTH=3
   LABEL='POSTOPERATIVE WOUND DEHISCENCE (Numertr)'
 TAPS25 LENGTH=3
   LABEL='ACCIDENTAL PUNCTURE/LACERATION(Numeratr)'
 TAPS26 LENGTH=3
   LABEL='TRANSFUSION REACTION         (Numerator)'
;

 %MEND;

 %LBL;


 * -------------------------------------------------------------- ;
 * --- CONSTRUCT PROVIDER LEVEL INDICATORS ---------------------- ;
 * -------------------------------------------------------------- ;

   * --- COMPLICATIONS OF ANESTHESIA                          --- ;

   IF %MDR($SURGIDR.) THEN DO;

      TPPS01 = 0;

      IF %MDX2($ANES1ID.) OR %MDX2($ANES2ID.) THEN TPPS01 = 1;
     
      *** Exclude Active Drug Dependence, Active nondependent abuse
          of drugs, and self-inflicted injury;

      IF %MDX2($ANES1ID.) AND 
         (%MDX($ADRUGID.) OR %MDX($ANONDID.) OR %MDX($SELFIID.))
      THEN TPPS01 = .;

   END;

   * --- DEATH IN LOW MORTALITY DRGS                          --- ;

   IF %MDR($LOWMODR.) THEN DO;

      TPPS02 = 0;

      IF DISP IN (20) THEN TPPS02 = 1;
 
      *** Exclude Trauma, Immunocompomised state, and cancer;

      IF %MDX($TRAUMID.) OR %MDR($TRAUMDR.) OR
         %MDX($IMMUNID.) OR %MPR($IMMUNIP.) OR 
         %MDX($CANCEID.) OR %MDR($CANCEDR.) 
      THEN TPPS02 = .;

      SELECT (PUT(PUT(DRG,Z3.),$LOWM2DR.));
        WHEN ('M')  TPPS02A = TPPS02;
        WHEN ('PM') TPPS02B = TPPS02;
        WHEN ('S')  TPPS02C = TPPS02;
        WHEN ('PS') TPPS02D = TPPS02;
        WHEN ('O')  TPPS02E = TPPS02;
        WHEN ('Y')  TPPS02F = TPPS02;
      END; * SELECT DRG ;

   END;

   * --- DECUBITUS ULCER                                      --- ;

   IF %MDR($MEDICDR.) OR %MDR($SURGIDR.) THEN DO;

      TPPS03=0;
     
      IF %MDX2($DECUBID.) THEN TPPS03 = 1;
 
      *** Exclude Hemiplegia, Paraplagia, Quadriplagia;

      IF %MDX($HEMIPID.) THEN TPPS03 = .;

      *** Exclude MDC 9 and 14;

      IF MDC IN (9, 14) THEN TPPS03 = .;

      *** Exclude LOS < 5, Admitted from LTC;

      IF LOS < 5 OR ASOURCE IN (3) THEN TPPS03 = .;

   END;

   * --- FAILURE TO RESCUE                                    --- ;

   IF (%MDX2($FTR1DX.) AND
       NOT (%MDX1($FTR1DX.) OR %MDX1($FTR1EX.) OR %MDX1($AMIID.) OR %MDX1($CARDRID.) OR 
            %MDX1($CARDIID.) OR %MDX1($HEMORID.) OR %MDX1($GASTRID.) OR %MDX1($SHOCKID.) OR 
            %MDX1($TRAUMID.) OR %MDR($TRAUMDR.))) 
      OR
      (%MDX2($FTR2DX.) AND 
       NOT (%MDX1($FTR2DX.) OR %MDX1($OBEMBOL.)))
      OR
      (%MDX2($FTR3DX.) AND 
       NOT (%MDX1($FTR3DX.) OR %MDX1($FTR3EXA.) OR %MDX($FTR3EXB.) OR %MDX($IMMUNID.) OR 
            %MPR($IMMUNIP.) OR MDC IN (4)))
      OR
      (%MDX2($FTR4DX.) AND 
       NOT (%MDX1($FTR4DX.) OR %MDX($IMMUNID.) OR %MPR($IMMUNIP.) OR
            %MDX1($INFECID.) OR %MDR($INFECDR.) OR LOS < 4))
      OR
      ((%MDX2($FTR5DX.) OR %MPR($FTR5PR.)) AND 
       NOT (%MDX1($FTR5DX.) OR %MDX1($FTR5EX.) OR %MDX1($HEMORID.) OR %MDX1($TRAUMID.) OR 
            %MDR($TRAUMDR.) OR %MDX1($GASTRID.) OR MDC IN (4,5)))
      OR
      (%MDX2($FTR6DX.) AND 
       NOT (%MDX1($FTR6DX.) OR %MDX1($FTR6EX.) OR %MDX1($TRAUMID.) OR %MDR($TRAUMDR.) OR
            %MDX1($ALCHLSM.) OR MDC IN (6,7)))
   THEN DO;

      TPPS04 = 0;

      IF DISP IN (20) THEN TPPS04 = 1;

      *** Exclude Age GE 75 or Transfers from an acute care or LTC facility
          or Transferred to an acute care facility or MDC 15;

      IF AGE >= 75 OR ASOURCE IN (2,3) OR DISP IN (2) OR MDC IN (15) THEN TPPS04 = .;

   END;

   * --- FOREIGN BODY LEFT IN DURING PROCEDURE                --- ;

   IF %MDR($MEDICDR.) OR %MDR($SURGIDR.) THEN DO;

      TPPS05 = 0;

      IF %MDX2($FOREIID.) THEN TPPS05=1;

   END;

   * --- IATROGENIC PNEUMOTHORAX                              --- ;

   IF %MDR($MEDICDR.) OR %MDR($SURGIDR.) THEN DO;

      TPPS06=0;

      IF %MDX2($IATROID.) THEN TPPS06=1;
   
      *** Exclude Trauma, Thoracic surgery, Lung or pleural biopsy,
          or Cardiac surgery or MDC 14;

      IF %MDX($TRAUMID.) OR %MDR($TRAUMDR.) OR %MPR($THORAIP.) OR
         %MPR($LUNGBIP.) OR %MDR($CARDSDR.) OR MDC IN (14)
      THEN TPPS06 = .;

   END;

   * --- INFECTION DUE TO MEDICAL CARE                        --- ;

   IF %MDR($MEDICDR.) OR %MDR($SURGIDR.) THEN DO;

      TPPS07=0;

      IF %MDX2($IDTMCID.) THEN TPPS07 = 1;
 
      *** Exclude Immunocompromised state and Cancer;

      IF %MDX($IMMUNID.) OR %MPR($IMMUNIP.) OR
         %MDX($CANCEID.) OR %MDR($CANCEDR.) 
      THEN TPPS07 = .;
 
   END;

   * --- POSTOPERATIVE HIP FRACTURE                           --- ;

   IF %MDR($SURGIDR.) AND AGE >= 18 THEN DO;

      TPPS08 = 0;
  
      IF %MDX2($HIPFXID.) THEN TPPS08 = 1;

      *** Excludes Seizure, Syncope, Stroke, Coma, Cardiac arrest,
          Poisoning, Trauma, Delirium and other psychoses, Anoxic
          brain injury;

      IF %MDX1($SEIZUID.) OR %MDX1($SYNCOID.) OR %MDX1($STROKID.) OR
         %MDX1($COMAID.)  OR %MDX1($CARDIID.) OR %MDX1($POISOID.) OR
         %MDX1($TRAUMID.) OR %MDR($TRAUMDR.)  OR %MDX1($DELIRID.) OR
         %MDX1($ANOXIID.)
      THEN TPPS08 = .;

      *** Excludes Metastatic cancer, Lymphoid malignancy, 
          Bone malignancy, Self-inflicted injury;
 
      IF %MDX($METACID.) OR %MDX($LYMPHID.) OR %MDX($BONEMID.) OR
         %MDX($SELFIID.)
      THEN TPPS08 = .;

      *** Exclude MDC 8 and 14;

      IF MDC IN (8, 14) THEN TPPS08 = .;

   END;

   * --- POSTOPERATIVE HEMORRHAGE OR HEMATOMA                 --- ;

 %MACRO PS9;

 %IF (&PRDAY. = 1 AND PRDAY1 NE .) %THEN %DO;

   IF %MDR($SURGIDR.) THEN DO;

      TPPS09 = 0;

      IF (%MDX2($POHMAID.) AND %MPR2($HEMATIP.,>= PRDAY1)) OR 
         (%MDX2($POHMRID.) AND %MPR2($HEMORIP.,>= PRDAY1)) 
      THEN TPPS09 = 1;

      *** Exclude MDC 14;

      IF MDC IN (14) THEN TPPS09 = .;

   END;

 %END;
 %ELSE %DO;

   IF %MDR($SURGIDR.) THEN DO;

      TPPS09 = 0;

      IF (%MDX2($POHMAID.) AND %MPR2B($HEMATIP.)) OR 
         (%MDX2($POHMRID.) AND %MPR2B($HEMORIP.)) 
      THEN TPPS09 = 1;

      *** Exclude MDC 14;

      IF MDC IN (14) THEN TPPS09 = .;

   END;

 %END;

 %MEND;

 %PS9;

   * --- POSTOPERATIVE PHYSIOLOGIC AND METABOLIC DERANGEMENTS --- ;

   IF %MDR($SURGIDR.) AND ATYPE IN (3) THEN DO;

      TPPS10 = 0;

      IF %MDX2($PHYSIDA.) OR (%MDX2($PHYSIDB.) AND %MPR($DIALYIP.))
      THEN TPPS10 = 1;

      *** Exclude Ketoacidosis, Hyperosmolarity or other Coma and
          Diabetes;

      IF %MDX2($PHYSIDA.) AND %MDX1($DIABEID.) THEN TPPS10 = .;

      *** Exclude Acute renal failure and AMI, Cardiac arrhythmia,
          Cardiac arrest, Shock, Hemorrhage, or Gastrointestinal
          hemorrhage;

      IF %MDX2($PHYSIDB.) AND (%MDX1($AMIID.) OR %MDX1($CARDRID.) OR
         %MDR($CARDRDR.) OR %MDX1($CARDIID.) OR %MDX1($SHOCKID.) OR
         %MDX1($HEMORID.) OR %MDX1($GASTRID.))
      THEN TPPS10 = .;

      *** Exclude MDC 14;

      IF MDC IN (14) THEN TPPS10 = .;

   END;

   * --- POSTOPERATIVE RESPIRATORY FAILURE                    --- ;

   IF %MDR($SURGIDR.) AND ATYPE IN (3) THEN DO;

      TPPS11 = 0;

      IF %MDX2($ACURFID.) THEN TPPS11 = 1;

      *** Exclude MDC 4, 5, 14;
 
      IF MDC IN (4,5,14) THEN TPPS11 = .;

   END;

   * --- POSTOPERATIVE PE OR DVT                              --- ;

   IF %MDR($SURGIDR.) THEN DO;

      TPPS12 = 0;

      IF %MDX2($DEEPVID.) OR %MDX2($PULMOID.) THEN TPPS12 = 1;

      *** Exclude Deep vein thrombosis;

      IF %MDX1($DEEPVID.) THEN TPPS12 = .;

      *** Exclude Interruption of vena cava;

 %MACRO PS12;

 %IF (&PRDAY. = 1  AND PRDAY1 NE .) %THEN %DO;

      IF %MPR2($VENACIP.,<= PRDAY1) THEN TPPS12 = .;

 %END;
 %ELSE %DO;

      IF %MPR2B($VENACIP.) THEN TPPS12 = .;
 %END;

 %MEND;

 %PS12;

      *** Exclude MDC 14;

      IF MDC IN (14) THEN TPPS12 = .;

   END;

   * --- POSTOPERATIVE SEPSIS                                 --- ;

   IF %MDR($SURGIDR.) AND ATYPE IN (3) THEN DO;

      TPPS13=0;

      IF %MDX2($SEPTIID.) THEN TPPS13 = 1;

      *** Exclude Infection, Immunocompromised or Cancer;

      IF %MDX1($INFECID.) OR %MDR($INFECDR.) OR %MDX($IMMUNID.) OR
         %MPR($IMMUNIP.) OR %MDX($CANCEID.) OR 
         %MDR($CANCEDR.)
      THEN TPPS13 = .;

      *** Exclude Length of stay less then 4 days;

      IF LOS < 4 THEN TPPS13 = .;

      *** Exclude MDC 14;

      IF MDC IN (14) THEN TPPS13 = .;

   END;

   * --- POSTOPERATIVE WOUND DEHISCENCE                       --- ;

   IF %MPR($ABDOMIP.) THEN DO;

      TPPS14 = 0;

      IF %MPR2B($RECLOIP.) THEN TPPS14 = 1;

      *** Exclude MDC 14;

      IF MDC IN (14) THEN TPPS14 = .;

   END;

   * --- ACCIDENTAL PUNCTURE OR LACERATION                    --- ;

   IF %MDR($MEDICDR.) OR %MDR($SURGIDR.) THEN DO;

      TPPS15 = 0;

      IF %MDX2($TECHNID.) THEN TPPS15 = 1;

      *** Exclude MDC 14;

      IF MDC IN (14) THEN TPPS15 = .;

   END;

   * --- TRANSFUSION REACTION                                 --- ;

   IF %MDR($MEDICDR.) OR %MDR($SURGIDR.) THEN DO;

      TPPS16 = 0;
 
      IF %MDX2($TRANFID.) THEN TPPS16 = 1;

   END;

   * --- BIRTH TRAUMA                                         --- ;

   IF (%MDX($LIVEBID.) OR %MDR($LIVEBDR.)) AND ATYPE IN (4) THEN DO;

      TPPS17 = 0;

      IF %MDX($BIRTIDA.) OR %MDX($BIRTIDB.) OR %MDX($BIRTIDC.) THEN TPPS17 = 1;

      *** Exclude Subdural or cerebral hemmorrhage and preterm;

      IF %MDX($BIRTIDA.) AND %MDX($PRETEID.) THEN TPPS17 = .;

      *** Exclude Injury to skeleton and osteogenesis imperfecta;

      IF %MDX($BIRTIDB.) AND %MDX($OSTEOID.) THEN TPPS17 = .;

   END;

   * --- OB TRAUMA - VAGINAL WITH INSTRUMENTATION             --- ;

   IF %MDR($VAGINDR.) AND %MPR($INSTRIP.) THEN DO;

      TPPS18 = 0;

      IF %MDX($OBTRAID.) OR %MPR($OBTRAIP.) THEN TPPS18 = 1;

   END;

   * --- OB TRAUMA - VAGINAL WITHOUT INSTRUMENTATION          --- ;

   IF %MDR($VAGINDR.) AND NOT %MPR($INSTRIP.) THEN DO;

      TPPS19 = 0;

      IF %MDX($OBTRAID.) OR %MPR($OBTRAIP.) THEN TPPS19 = 1;

   END;

   * --- OB TRAUMA - C-SECTION                                --- ;

   IF %MDR($CESARDR.) THEN DO;

      TPPS20 = 0;

      IF %MDX($OBTRAID.) OR %MPR($OBTRAIP.) THEN TPPS20 = 1;

   END;



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

   * --- FOREIGN BODY LEFT IN DURING PROCEDURE                --- ;

   IF %MDR($MEDICDR.) OR %MDR($SURGIDR.) THEN DO;

      TAPS21 = 0;

      IF %MDX($FOREIID.) THEN TAPS21=1;

   END;

   * --- IATROGENIC PNEUMOTHORAX                              --- ;

   IF %MDR($MEDICDR.) OR %MDR($SURGIDR.) THEN DO;

      TAPS22=0;

      IF %MDX($IATROID.) THEN TAPS22=1;
   
      *** Exclude Trauma, Thoracic surgery, Lung or pleural biopsy,
          or Cardiac surgery;

      IF %MDX($TRAUMID.) OR %MDR($TRAUMDR.) OR %MPR($THORAIP.) OR
         %MPR($LUNGBIP.) OR %MDR($CARDSDR.)
      THEN TAPS22 = .;

      *** Exclude MDC 14;

      IF MDC EQ 14 THEN TAPS22 = .;
      
   END;

   * --- INFECTION DUE TO MEDICAL CARE                        --- ;

   IF %MDR($MEDICDR.) OR %MDR($SURGIDR.) THEN DO;

      TAPS23=0;

      IF %MDX($IDTMCID.) THEN TAPS23 = 1;
 
      *** Exclude Immunocompromised state and Cancer;
 
      IF %MDX($IMMUNID.) OR %MPR($IMMUNIP.) OR
         %MDX($CANCEID.) OR %MDR($CANCEDR.) 
      THEN TAPS23 = .;

   END;

   * --- POSTOPERATIVE WOUND DEHISCENCE                       --- ;

   IF %MPR($ABDOMIP.) THEN DO;

      TAPS24 = 0;

      IF %MPR($RECLOIP.) THEN TAPS24 = 1;

      *** Exclude MDC 14;

      IF MDC EQ 14 THEN TAPS24 = .;

   END;

   * --- ACCIDENTAL PUNCTURE OR LACERATION                    --- ;

   IF %MDR($MEDICDR.) OR %MDR($SURGIDR.) THEN DO;

      TAPS25 = 0;

      IF %MDX($TECHNID.) THEN TAPS25 = 1;

      *** Exclude MDC 14;

      IF MDC IN (14) THEN TAPS25 = .;

   END;

   * --- TRANSFUSION REACTION                                 --- ;

   IF %MDR($MEDICDR.) OR %MDR($SURGIDR.) THEN DO;

      TAPS26 = 0;
 
      IF %MDX($TRANFID.) THEN TAPS26 = 1;

   END;
RUN;


PROC MEANS DATA=OUT1.&OUTFILE1. N NMISS MIN MAX MEAN SUM std;
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;
