/***************************************************************/
  /* Title:       CREATION OF COMORBIDITY VARIABLES              */
  /*                                                             */
  /* PROGRAM:     COMORADM                                       */
  /*                                                             */
  /* Description: Creates comorbidity variables based on the     */
  /*              presence of secondary diagnoses and redefines  */
  /*              comorbidity group by eliminating DRGs directly */
  /*              related to them.                               */
  /***************************************************************/

  /***********************************************************/
  /*  Define subdirectory for data files and format library. */
  /*  Input files:    C:\DATA\                               */
  /*  Output files:   C:\DATA\                               */
  /*  Format library: C:\COMORB\FMTLIB\                      */
  /***********************************************************/

LIBNAME IN1     'C:\DATA\';          
LIBNAME OUT1    'C:\DATA\';          
LIBNAME LIBRARY 'C:\COMORB\FMTLIB\'; 
OPTIONS NOCENTER BUFSIZE=64K OBS=MAX LS=159 PS=56;

TITLE1 'CREATION AND VALIDATION OF COMORBIDITY MEASURES FOR';
TITLE2 'USE WITH DISCHARGE ADMINISTRATIVE DATA';

*** Macro Variables that need to be set;
* Maximum number of DXs;        %LET NUMDX=2;
* Input SAS file member name;   %LET CORE=XXXXXX;

DATA OUT1.ANALYSIS;

    SET IN1.&CORE (KEEP=DRG DX1-DX&NUMDX MDC NDX);

    DROP  DRG DX1-DX&NUMDX MDC NDX I J DXVALUE A1-A31
          HTN HTNCX
          CARDFLG PERIFLG CEREFLG PULMFLG DIABFLG
          HYPOFLG RENALFLG RENFFLG LIVERFLG
          ULCEFLG HIVFLG LEUKFLG CANCFLG ARTHFLG
          NUTRFLG ANEMFLG ALCFLG;

   /*****************************************/
   /*  Declare variables as array elements  */
   /*****************************************/

   ARRAY DX (&NUMDX) $  DX1 - DX&NUMDX;

   ARRAY COM1 (31)  CHF      ARYTHM   VALVE    PULMCIRC PERIVASC
                    HTN      HTNCX    PARA     NEURO    CHRNLUNG
                    DM       DMCX     HYPOTHY  RENLFAIL LIVER
                    ULCER    AIDS     LYMPH    METS     TUMOR
                    ARTH     COAG     OBESE    WGHTLOSS LYTES
                    BLDLOSS  ANEMDEF  ALCOHOL  DRUG     PSYCH
                    DEPRESS ;

   ARRAY COM2 (31) $ 8 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;

   LENGTH   CHF      ARYTHM   VALVE    PULMCIRC PERIVASC
            HTN      HTNCX    PARA     NEURO    CHRNLUNG
            DM       DMCX     HYPOTHY  RENLFAIL LIVER
            ULCER    AIDS     LYMPH    METS     TUMOR
            ARTH     COAG     OBESE    WGHTLOSS LYTES
            BLDLOSS  ANEMDEF  ALCOHOL  DRUG     PSYCH
            DEPRESS 3 ;

   /****************************************************/
   /* Initialize  COM1 to 0 and assigns the variable   */
   /*  name from COM1 as the VALUE of COM2             */
   /****************************************************/

    DO I = 1 TO 31;
      COM1(I) = 0;
      CALL VNAME(COM1(I),COM2(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 COM2 then a value of 1 is assigned to the */
    /* corresponding comorbidity group in array COM1   */
    /***************************************************/

   DO I = 2 TO NDX;
       DXVALUE = PUT(DX(I),$RCOMFMT.) ;
            DO J = 1 TO 31;
             IF DXVALUE = COM2(J)  THEN COM1(J) = 1;
            END;
   END;

   /*********************************************************/
   /* SET UP CODE TO ONLY COUNT THE MORE SEVERE COMORBIDITY */
   /*********************************************************/

   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;

   ATTRIB HTN_C LENGTH=3 LABEL='Hypertension';

     LABEL CHF        = 'Congestive heart failure'
           ARYTHM     = 'Arrhythmias'
           VALVE      = 'Valvular disease'
           PULMCIRC   = 'Pulmonary circulation disease'
           PERIVASC   = 'Peripheral vascular disease'
           PARA       = 'Paralysis'
           NEURO      = 'Other neurological disorders'
           CHRNLUNG   = 'Chronic pulmonary disease'
           DM         = 'Diabetes'
           DMCX       = 'Diabetes w/ chronic complications'
           HYPOTHY    = 'Hypothyroidism'
           RENLFAIL   = 'Renal failure'
           LIVER      = 'Liver disease'
           ULCER      = 'Peptic ulcer Disease x bleeding'
           AIDS       = 'Acquired immune deficiency syndrome'
           LYMPH      = 'Lymphoma'
           METS       = 'Metastatic cancer'
           TUMOR      = 'Solid tumor w/out metastasis'
           ARTH       = 'Rheumatoid arthritis/collagen vas'
           COAG       = 'Coagulopthy'
           OBESE      = 'Obesity'
           WGHTLOSS   = 'Weight loss'
           LYTES      = 'Fluid and electrolyte disorders'
           BLDLOSS    = 'Chronic blood loss anemia'
           ANEMDEF    = 'Deficiency Anemias'
           ALCOHOL    = 'Alcohol abuse'
           DRUG       = 'Drug abuse'
           PSYCH      = 'Psychoses'
           DEPRESS    = 'Depression'
        ;
RUN;

   /*************************************************/
   /* Sample print and contents of comorbidity file */
   /*************************************************/

PROC PRINT DATA=OUT1.ANALYSIS (OBS=60);
  TITLE3 'Print of analysis file variables';
RUN;

PROC CONTENTS DATA=OUT1.ANALYSIS ;
  TITLE3 'Contents of comorbidity analysis file';
RUN;

   /****************************************/
   /*  Frequency of comorbidity variables  */
   /****************************************/

PROC FREQ DATA=OUT1.ANALYSIS;
  TABLE  CHF      ARYTHM   VALVE    PULMCIRC PERIVASC
         HTN_C    PARA     NEURO    CHRNLUNG DM
         DMCX     HYPOTHY  RENLFAIL LIVER    ULCER
         AIDS     LYMPH    METS     TUMOR    ARTH
         COAG     OBESE    WGHTLOSS LYTES    BLDLOSS
         ANEMDEF  ALCOHOL  DRUG     PSYCH    DEPRESS
       / LIST MISSING;
  TITLE3 'Frequency of comorbidity variables';
RUN;

   /***********************************/
   /*  Means of comorbidity variables */
   /***********************************/

PROC MEANS DATA=OUT1.ANALYSIS  N NMISS MEAN STD MIN MAX;
  VAR    CHF      ARYTHM   VALVE    PULMCIRC PERIVASC
         HTN_C    PARA     NEURO    CHRNLUNG DM
         DMCX     HYPOTHY  RENLFAIL LIVER    ULCER
         AIDS     LYMPH    METS     TUMOR    ARTH
         COAG     OBESE    WGHTLOSS LYTES    BLDLOSS
         ANEMDEF  ALCOHOL  DRUG     PSYCH    DEPRESS
      ;
   TITLE3 'Means of comorbidity variables';
RUN;