 %MACRO V2317P1M(INP=, IND=, OUTDATA=, IDVAR=, KEEPVAR=, SEDITS=,
                 DATE_ASOF=, DATE_ASOF_EDIT=, 
                 FMNAME9=, FMNAME0=, 
                 AGEFMT9=, AGEFMT0=, 
                 SEXFMT9=, SEXFMT0=, DF=1, 
                 AGESEXMAC=AGESEXV2, LABELMAC=V23HALL1, 
                 EDITMAC9=V23I9EA1, EDITMAC0=,  
                 HIERMAC=V23HALH1, SCOREMAC=SCOREVAR);

%**********************************************************************
 * Score variables are created using coefficients from 8 final models:
 *  1) Community NonDual Aged
 *  2) Community NonDual Disabled
 *  3) Community Full Benefit Dual Aged
 *  4) Community Full Benefit Dual Disabled
 *  5) Community Partial Benefit Dual Aged
 *  6) Community Partial Benefit Dual Disabled
 *  7) Long Term Institutional
 *  8) New Enrollees
 *
 * Assumptions about input files:
 *   - both files are sorted by person ID
 *   - person level file has the following variables:
 *     :&IDVAR   - person ID variable (it is a macro parameter)
 *     :DOB      - date of birth
 *     :SEX      - sex
 *     :OREC     - original reason for entitlement
 *     :LTIMCAID - Medicaid dummy variable for LTI
 *     :NEMCAID  - Medicaid dummy variable for new enrollees
 *
 *   - diagnosis level file has the following variables:
 *     :&IDVAR  - person ID variable (it is a macro parameter)
 *     :DIAG    - diagnosis
 *     :DIAG_TYPE - diag code version, '9' for ICD9 and '0' for ICD10  
 *
 * Parameters:
 * INP            - input person dataset
 * IND            - input diagnosis dataset
 * OUTDATA        - output dataset
 * IDVAR          - name of person id variable (HICNO for Medicare data)
 * KEEPVAR        - variables to keep in the output file
 * SEDITS         - a switch that controls whether to perform MCE edits 
 *                  on ICD9 and ICD10. 1-YES, 0-NO
 * DATE_ASOF      - reference date to calculate age. Set to February 1
 *                  of the payment year for consistency with CMS.
 * DATE_ASOF_EDIT - reference date to calculate age used for 
 *                  validation of diagnoses (MCE edits)
 * FMNAME9        - format name (crosswalk ICD9 to V23 CCs)
 * AGEFMT9        - format name (crosswalk ICD9 to acceptable age range
 *                  in case MCE edits on diags are to be performed)
 * SEXFMT9        - format name (crosswalk ICD9 to acceptable sex in 
 *                  case MCE edits on diags are to be performed)
 * FMNAME0        - format name (crosswalk ICD10 to V23 CCs)
 * AGEFMT0        - format name (crosswalk ICD10 to acceptable age range
 *                  in case MCE edits on diags are to be performed)
 * SEXFMT0        - format name (crosswalk ICD10 to acceptable sex in 
 *                  case MCE edits on diags are to be performed)
 * DF             - normalization factor.
 *                  Default=1
 * AGESEXMAC      - external macro name: create age/sex,
 *                  originally disabled, disabled vars
 * EDITMAC9       - external macro name: perform edits to ICD9
 * EDITMAC0       - external macro name: perform edits to ICD10
 * LABELMAC       - external macro name: assign labels to HCCs
 * HIERMAC        - external macro name: set HCC=0 according to
 *                  hierarchies
 * SCOREMAC       - external macro name: calculate a score variable
 *
 **********************************************************************;

 %**********************************************************************
 * step1: include external macros
 **********************************************************************;
 %IF "&AGESEXMAC" ne "" %THEN %DO;
     %INCLUDE IN0(&AGESEXMAC) /SOURCE2; %* create demographic variables;
 %END;
 %IF "&EDITMAC9" ne "" %THEN %DO;
     %INCLUDE IN0(&EDITMAC9)   /SOURCE2; %* perform edits on ICD9;
 %END;
 %IF "&EDITMAC0" ne "" %THEN %DO;
     %INCLUDE IN0(&EDITMAC0)   /SOURCE2; %* perform edits on ICD10;
 %END;
 %IF "&LABELMAC" ne "" %THEN %DO;
     %INCLUDE IN0(&LABELMAC)  /SOURCE2; %* hcc labels;
 %END;
 %IF "&HIERMAC" ne "" %THEN %DO;
     %INCLUDE IN0(&HIERMAC)   /SOURCE2; %* hierarchies;
 %END;
 %IF "&SCOREMAC" ne "" %THEN %DO;
     %INCLUDE IN0(&SCOREMAC)  /SOURCE2; %* calculate score variable;
 %END;

 %**********************************************************************
 * step2: define internal macro variables
 **********************************************************************;

 %LET N_CC=204; %*max # of HCCs;

 %* age/sex variables for Community Aged regression;
 %LET AGESEXVA=                                    F65_69
                F70_74 F75_79 F80_84 F85_89 F90_94 F95_GT
                                                   M65_69
                M70_74 M75_79 M80_84 M85_89 M90_94 M95_GT;

 %* age/sex variables for Community Disabled regression;
 %LET AGESEXVD= F0_34  F35_44 F45_54 F55_59 F60_64
                M0_34  M35_44 M45_54 M55_59 M60_64;


 %* diagnostic categories necessary to create interaction variables;
 %LET DIAG_CAT= CANCER  DIABETES  CHF CARD_RESP_FAIL
                gCopdCF  RENAL_V23  SEPSIS PRESSURE_ULCER
                gSubstanceAbuse_V23     
                gPsychiatric_V23
                gSubstanceAbuse_gPsychiatric_V23;   

  /* HCC 132 and HCC 133 excluded from list on 11/14/2017*/
 %LET All_Elig_HCCs=%str(
  HCC1   HCC2   HCC3   HCC4   HCC5   HCC6   HCC7   HCC8   HCC9
  HCC10  HCC11  HCC12  HCC13  HCC14  HCC15  HCC16  HCC17  HCC18
  HCC19  HCC21  HCC22  HCC23  HCC24  HCC25  HCC26  HCC27  HCC28
  HCC29  HCC30  HCC31  HCC32  HCC33  HCC34  HCC35  HCC36  HCC37
  HCC38  HCC39  HCC40  HCC41  HCC42  HCC43  HCC44  HCC45  HCC46
  HCC47  HCC48  HCC49  HCC50  HCC51  HCC52  HCC53  HCC54  HCC55
  HCC56  HCC57  HCC58  HCC59  HCC60  HCC61  HCC62  HCC63  HCC64
  HCC65  HCC66  HCC67  HCC68  HCC69  HCC70  HCC71  HCC72  HCC73
  HCC74  HCC75  HCC76  HCC77  HCC78  HCC79  HCC80  HCC81  HCC82
  HCC83  HCC84  HCC85  HCC86  HCC87  HCC88  HCC89  HCC90  HCC91
  HCC92  HCC93  HCC94  HCC95  HCC96  HCC97  HCC98  HCC99  HCC100
  HCC101 HCC102 HCC103 HCC104 HCC105 HCC106 HCC107 HCC108 HCC109
  HCC110 HCC111 HCC112 HCC113 HCC114 HCC115 HCC116 HCC117 HCC118
  HCC119 HCC120 HCC121 HCC122 HCC123 HCC124 HCC125 HCC126 HCC127
  HCC128 HCC129 HCC130 HCC131               HCC134 HCC135 HCC136
  HCC137 HCC138 HCC139 HCC140 HCC141 HCC142 HCC143 HCC144 HCC145
  HCC146 HCC147 HCC148 HCC149 HCC150 HCC151 HCC152 HCC153 HCC154
  HCC155 HCC156 HCC157 HCC158 HCC159 HCC160 HCC161 HCC162 HCC163
  HCC164 HCC165 HCC166 HCC167 HCC168 HCC169 HCC170 HCC171 HCC172
  HCC173 HCC174 HCC175 HCC176 HCC177 HCC180 HCC181 HCC182 HCC183
  HCC184 HCC186 HCC187 HCC188 HCC189 HCC190 HCC202 HCC203 HCC204
  );

  %LET ADDZ=%STR(
  Z1 Z2 Z3 Z4 Z5 Z6 Z7 Z8 Z9 Z10 Z11 Z12 Z13 Z14 Z15P
  );

 %*orig disabled interactions for Community Aged regressions;
 %LET ORIG_INT =%STR(OriginallyDisabled_Female OriginallyDisabled_Male);

 %*interaction variables for Community Aged regressions;   
 %LET INTERRACC_VARSA=%STR(HCC47_gCancer
           HCC85_gDiabetesMellit
           HCC85_gCopdCF
           HCC85_gRenal_V23
           gRespDepandArre_gCopdCF
           HCC85_HCC96
           );

 %*interaction variables for Community Disabled regressions;
  %LET INTERRACC_VARSD=%STR(HCC47_gCancer
           HCC85_gDiabetesMellit
           HCC85_gCopdCF
           HCC85_gRenal_V23
           gRespDepandArre_gCopdCF
           HCC85_HCC96
           disable_substAbuse_psych_V23
           );


 %*variables for Community Aged regressions ;
 %LET COMM_REGA= %STR(&AGESEXVA
                 &orig_int
                 &HCCV23_list83
                 &INTERRACC_VARSA 
                 &ADDZ);


 %*variables for Community Disabled regressions ;
 %LET COMM_REGD= %STR(&AGESEXVD
                 &HCCV23_list83
                 &INTERRACC_VARSD
                 &ADDZ);


 %* age/sex variables for Insititutional regression;
 %LET AGESEXV=  F0_34  F35_44 F45_54 F55_59 F60_64 F65_69
                F70_74 F75_79 F80_84 F85_89 F90_94 F95_GT
                M0_34  M35_44 M45_54 M55_59 M60_64 M65_69
                M70_74 M75_79 M80_84 M85_89 M90_94 M95_GT;


 %*interaction variables for Institutional regression;
 %LET INTERRACI_VARS = %STR(DISABLED_HCC85       DISABLED_PRESSURE_ULCER
                            DISABLED_HCC161      DISABLED_HCC39
                            DISABLED_HCC77       DISABLED_HCC6
                            CHF_gCopdCF
                            gCopdCF_CARD_RESP_FAIL
                            SEPSIS_PRESSURE_ULCER
                            SEPSIS_ARTIF_OPENINGS
                            ART_OPENINGS_PRESS_ULCER
                            DIABETES_CHF
                            gCopdCF_ASP_SPEC_B_PNEUM
                            ASP_SPEC_B_PNEUM_PRES_ULC
                            SEPSIS_ASP_SPEC_BACT_PNEUM
                            SCHIZOPHRENIA_gCopdCF
                            SCHIZOPHRENIA_CHF
                            SCHIZOPHRENIA_SEIZURES);


 %*variables for Institutional regression;
 %LET INST_REG = %STR(&AGESEXV
                      LTIMCAID  ORIGDS
                      &INTERRACI_VARS
                      &HCCV23_list83
                      &ADDZ);

 %*age/sex variables for non-ORIGDS New Enrollee interactions; 
 %LET NE_AGESEXV=
      NEF0_34    NEF35_44   NEF45_54   NEF55_59   NEF60_64
      NEF65      NEF66      NEF67      NEF68      NEF69
      NEF70_74   NEF75_79   NEF80_84
      NEF85_89   NEF90_94   NEF95_GT
      NEM0_34    NEM35_44   NEM45_54   NEM55_59   NEM60_64
      NEM65      NEM66      NEM67      NEM68      NEM69
      NEM70_74   NEM75_79   NEM80_84
      NEM85_89   NEM90_94   NEM95_GT;

 %*age/sex variables for ORIGDS New Enrollee interactions;
 %LET ONE_AGESEXV=
      NEF65      NEF66      NEF67      NEF68      NEF69
      NEF70_74   NEF75_79   NEF80_84
      NEF85_89   NEF90_94   NEF95_GT
      NEM65      NEM66      NEM67      NEM68      NEM69
      NEM70_74   NEM75_79   NEM80_84
      NEM85_89   NEM90_94   NEM95_GT;

 
 %*variables for New Enrollee regression;
 %LET NE_REG=%STR(
   NMCAID_NORIGDIS_NEF0_34        NMCAID_NORIGDIS_NEF35_44
   NMCAID_NORIGDIS_NEF45_54       NMCAID_NORIGDIS_NEF55_59
   NMCAID_NORIGDIS_NEF60_64       NMCAID_NORIGDIS_NEF65
   NMCAID_NORIGDIS_NEF66          NMCAID_NORIGDIS_NEF67
   NMCAID_NORIGDIS_NEF68          NMCAID_NORIGDIS_NEF69
   NMCAID_NORIGDIS_NEF70_74       NMCAID_NORIGDIS_NEF75_79
   NMCAID_NORIGDIS_NEF80_84       NMCAID_NORIGDIS_NEF85_89
   NMCAID_NORIGDIS_NEF90_94       NMCAID_NORIGDIS_NEF95_GT
   
   NMCAID_NORIGDIS_NEM0_34        NMCAID_NORIGDIS_NEM35_44
   NMCAID_NORIGDIS_NEM45_54       NMCAID_NORIGDIS_NEM55_59
   NMCAID_NORIGDIS_NEM60_64       NMCAID_NORIGDIS_NEM65
   NMCAID_NORIGDIS_NEM66          NMCAID_NORIGDIS_NEM67
   NMCAID_NORIGDIS_NEM68          NMCAID_NORIGDIS_NEM69
   NMCAID_NORIGDIS_NEM70_74       NMCAID_NORIGDIS_NEM75_79
   NMCAID_NORIGDIS_NEM80_84       NMCAID_NORIGDIS_NEM85_89
   NMCAID_NORIGDIS_NEM90_94       NMCAID_NORIGDIS_NEM95_GT
   
   MCAID_NORIGDIS_NEF0_34         MCAID_NORIGDIS_NEF35_44
   MCAID_NORIGDIS_NEF45_54        MCAID_NORIGDIS_NEF55_59
   MCAID_NORIGDIS_NEF60_64        MCAID_NORIGDIS_NEF65
   MCAID_NORIGDIS_NEF66           MCAID_NORIGDIS_NEF67
   MCAID_NORIGDIS_NEF68           MCAID_NORIGDIS_NEF69
   MCAID_NORIGDIS_NEF70_74        MCAID_NORIGDIS_NEF75_79
   MCAID_NORIGDIS_NEF80_84        MCAID_NORIGDIS_NEF85_89
   MCAID_NORIGDIS_NEF90_94        MCAID_NORIGDIS_NEF95_GT
           
   MCAID_NORIGDIS_NEM0_34         MCAID_NORIGDIS_NEM35_44
   MCAID_NORIGDIS_NEM45_54        MCAID_NORIGDIS_NEM55_59
   MCAID_NORIGDIS_NEM60_64        MCAID_NORIGDIS_NEM65
   MCAID_NORIGDIS_NEM66           MCAID_NORIGDIS_NEM67
   MCAID_NORIGDIS_NEM68           MCAID_NORIGDIS_NEM69
   MCAID_NORIGDIS_NEM70_74        MCAID_NORIGDIS_NEM75_79
   MCAID_NORIGDIS_NEM80_84        MCAID_NORIGDIS_NEM85_89
   MCAID_NORIGDIS_NEM90_94        MCAID_NORIGDIS_NEM95_GT
   
   NMCAID_ORIGDIS_NEF65           NMCAID_ORIGDIS_NEF66
   NMCAID_ORIGDIS_NEF67           NMCAID_ORIGDIS_NEF68
   NMCAID_ORIGDIS_NEF69           NMCAID_ORIGDIS_NEF70_74
   NMCAID_ORIGDIS_NEF75_79        NMCAID_ORIGDIS_NEF80_84
   NMCAID_ORIGDIS_NEF85_89        NMCAID_ORIGDIS_NEF90_94
   NMCAID_ORIGDIS_NEF95_GT    
   
   NMCAID_ORIGDIS_NEM65           NMCAID_ORIGDIS_NEM66
   NMCAID_ORIGDIS_NEM67           NMCAID_ORIGDIS_NEM68
   NMCAID_ORIGDIS_NEM69           NMCAID_ORIGDIS_NEM70_74
   NMCAID_ORIGDIS_NEM75_79        NMCAID_ORIGDIS_NEM80_84
   NMCAID_ORIGDIS_NEM85_89        NMCAID_ORIGDIS_NEM90_94
   NMCAID_ORIGDIS_NEM95_GT
        
   MCAID_ORIGDIS_NEF65            MCAID_ORIGDIS_NEF66
   MCAID_ORIGDIS_NEF67            MCAID_ORIGDIS_NEF68
   MCAID_ORIGDIS_NEF69            MCAID_ORIGDIS_NEF70_74
   MCAID_ORIGDIS_NEF75_79         MCAID_ORIGDIS_NEF80_84
   MCAID_ORIGDIS_NEF85_89         MCAID_ORIGDIS_NEF90_94
   MCAID_ORIGDIS_NEF95_GT    
   
   MCAID_ORIGDIS_NEM65            MCAID_ORIGDIS_NEM66
   MCAID_ORIGDIS_NEM67            MCAID_ORIGDIS_NEM68
   MCAID_ORIGDIS_NEM69            MCAID_ORIGDIS_NEM70_74
   MCAID_ORIGDIS_NEM75_79         MCAID_ORIGDIS_NEM80_84
   MCAID_ORIGDIS_NEM85_89         MCAID_ORIGDIS_NEM90_94
   MCAID_ORIGDIS_NEM95_GT);

 %*macro to create New Enrollee regression variables;
 %MACRO INTER(PVAR=, RLIST=);
    %LOCAL I;
    %LET I=1;
    %DO %UNTIL(%SCAN(&RLIST,&I)=);
       &PVAR._%SCAN(&RLIST,&I) = &PVAR * %SCAN(&RLIST,&I);
       %LET I=%EVAL(&I+1);
    %END;
 %MEND INTER;              


 %**********************************************************************
 * step3: merge person and diagnosis files outputting one record
 *        per person with score and HCC variables for each input person
 *        level record
 ***********************************************************************;

 DATA &OUTDATA(KEEP=&KEEPVAR );
   %****************************************************
    * step3.1: declaration section
    ****************************************************;

    %IF "&LABELMAC" ne "" %THEN %&LABELMAC;  *HCC labels;

   %* length of new variables (length for other age/sex vars is set in
      &AGESEXMAC macro);
    LENGTH CC $4. 
           AGEF 
           OriginallyDisabled_Female  
           OriginallyDisabled_Male
           &NE_REG
           CC1-CC&N_CC
           HCC1-HCC&N_CC
           &DIAG_CAT
           &INTERRACC_VARSA
           &INTERRACC_VARSD
           &INTERRACI_VARS 
           AGEF_EDIT 
           HCC_all &ADDZ
           3.;

    %*retain cc vars;
    RETAIN CC1-CC&N_CC 0  AGEF AGEF_EDIT;
    %*arrays;
    ARRAY C(&N_CC)  CC1-CC&N_CC;
    ARRAY HCC(&N_CC) HCC1-HCC&N_CC;
    %*interaction vars;
    ARRAY RV &INTERRACC_VARSA &INTERRACC_VARSD 
          &INTERRACI_VARS &DIAG_CAT HCC_all &ADDZ;

    %***************************************************
    * step3.2: to bring in regression coefficients
    ****************************************************;
    IF _N_ = 1 THEN SET INCOEF.HCCCOEFN;
    %***************************************************
    * step3.3: merge
    ****************************************************;
    MERGE &INP(IN=IN1)
          &IND(IN=IN2);
    BY &IDVAR;

    IF IN1 THEN DO;

    %*******************************************************
    * step3.4: for the first record for a person set CC to 0
    ********************************************************;

       IF FIRST.&IDVAR THEN DO;
          %*set ccs to 0;
           DO I=1 TO &N_CC;
            C(I)=0;
           END;
           %* age;
           AGEF =FLOOR((INTCK(
                'MONTH',DOB,&DATE_ASOF)-(DAY(&DATE_ASOF)<DAY(DOB)))/12);
           %IF "&DATE_ASOF_EDIT" ne "" %THEN  
           AGEF_EDIT =FLOOR((INTCK('MONTH',DOB,&DATE_ASOF_EDIT)
                -(DAY(&DATE_ASOF_EDIT)<DAY(DOB)))/12);
           %ELSE AGEF_EDIT=AGEF;
           ;
       END;         

    %***************************************************
    * step3.5 if there are any diagnoses for a person
    *         then do the following:
    *         - perform diag edits using macro &EDITMAC9 or &EDITMAC0
    *         - create CC using corresponding formats for ICD9 or ICD10
    *         - assign additional CC using provided additional formats
    ****************************************************;

       IF IN1 & IN2 THEN DO;
          %*initialize;
          CC="9999";

          IF DIAG_TYPE = "9" THEN DO;
             %IF "&EDITMAC9" NE "" %THEN 
                  %&EDITMAC9(AGE=AGEF_EDIT,SEX=SEX,ICD9=DIAG); 
             IF CC NE "-1.0" AND CC NE "9999" THEN DO;
                IND=INPUT(CC,4.);
                IF 1 <= IND <= &N_CC THEN C(IND)=1;
             END;
             ELSE IF CC="9999" THEN DO;
                ** assignment 1 **;
                IND = INPUT(LEFT(PUT(DIAG,$I9AS1031823&FMNAME9..)),4.);
                IF 1 <= IND <= &N_CC THEN C(IND)=1;
                ** assignment 2 **;
                IND = INPUT(LEFT(PUT(DIAG,$I9AS2031823&FMNAME9..)),4.);
                IF 1 <= IND <= &N_CC THEN C(IND)=1;
             END;
          END;

          ELSE IF DIAG_TYPE = "0" THEN DO;
            %IF "&EDITMAC0" NE "" %THEN 
                 %&EDITMAC0(AGE=AGEF_EDIT,SEX=SEX,ICD10=DIAG); 
            IF CC NE "-1.0" AND CC NE "9999" THEN DO;
               IND=INPUT(CC,4.);
               IF 1 <= IND <= &N_CC THEN C(IND)=1;
            END;
            ELSE IF CC="9999" THEN DO;
               %IF "&FMNAME0" NE "" %THEN %DO;
               ** assignment 1 **;
               IND = INPUT(LEFT(PUT(DIAG,$I0AS1031823&FMNAME0..)),4.);
               IF 1 <= IND <= &N_CC THEN C(IND)=1;
               ** assignment 2 **;
               IND = INPUT(LEFT(PUT(DIAG,$I0AS2031823&FMNAME0..)),4.);
               IF 1 <= IND <= &N_CC THEN C(IND)=1;
               ** assignment 3 **;
               IND = INPUT(LEFT(PUT(DIAG,$I0AS3031823&FMNAME0..)),4.);
               IF 1 <= IND <= &N_CC THEN C(IND)=1;
               ** assignment 4 **;
               IND = INPUT(LEFT(PUT(DIAG,$I0AS4031823&FMNAME0..)),4.);
               IF 1 <= IND <= &N_CC THEN C(IND)=1;
               %END;
            END;
          END;
       END; %*CC creation;  


    %*************************************************************
    * step3.6 for the last record for a person do the
    *         following:
    *         - create demographic variables needed (macro &AGESEXMAC)
    *         - create HCC using hierarchies (macro &HIERMAC)
    *         - create HCC interaction variables
    *         - create HCC and DISABL interaction variables
    *         - set HCCs and interaction vars to zero if there
    *           are no diagnoses for a person
    *         - create scores for community models
    *         - create score for institutional model
    *         - create score for new enrollee model
    **************************************************************;
       IF LAST.&IDVAR THEN DO;

           %****************************
           * demographic vars
           *****************************;
           %*create age/sex cells, originally disabled, disabled vars;
           %IF "&AGESEXMAC" ne "" %THEN
           %&AGESEXMAC(AGEF=AGEF, SEX=SEX, OREC=OREC);

           %*interaction;
           OriginallyDisabled_Female= ORIGDS*(SEX='2');
           OriginallyDisabled_Male  = ORIGDS*(SEX='1');

           %* NE interactions;
           NE_ORIGDS       = (AGEF>=65)*(OREC='1');
           NMCAID_NORIGDIS = (NEMCAID <=0 and NE_ORIGDS <=0);
           MCAID_NORIGDIS  = (NEMCAID > 0 and NE_ORIGDS <=0);
           NMCAID_ORIGDIS  = (NEMCAID <=0 and NE_ORIGDS > 0);
           MCAID_ORIGDIS   = (NEMCAID > 0 and NE_ORIGDS > 0);

           %INTER(PVAR =  NMCAID_NORIGDIS,  RLIST = &NE_AGESEXV );
           %INTER(PVAR =  MCAID_NORIGDIS,   RLIST = &NE_AGESEXV );
           %INTER(PVAR =  NMCAID_ORIGDIS,   RLIST = &ONE_AGESEXV);
           %INTER(PVAR =  MCAID_ORIGDIS,    RLIST = &ONE_AGESEXV);

           IF IN1 & IN2 THEN DO;
            %**********************
            * hierarchies
            **********************;
            %IF "&HIERMAC" ne "" %THEN %&HIERMAC;
            %************************
            * interaction variables
            *************************; 
            %*diagnostic categories;
            CANCER         = MAX(HCC8, HCC9, HCC10, HCC11, HCC12);
            DIABETES       = MAX(HCC17, HCC18, HCC19);
            CARD_RESP_FAIL = MAX(HCC82, HCC83, HCC84);
            CHF            = HCC85;
            gCopdCF        = MAX(HCC110, HCC111, HCC112);
            RENAL_V23      = MAX(HCC134, HCC135, HCC136, HCC137, HCC138);
            SEPSIS         = HCC2;
            gSubstanceAbuse_V23= MAX(HCC54, HCC55, HCC56);
            gPsychiatric_V23   = MAX(HCC57, HCC58, HCC59, HCC60);
            %*community models interactions ;
            HCC47_gCancer                = HCC47*Cancer;
            HCC85_gDiabetesMellit        = HCC85*Diabetes;
            HCC85_gCopdCF                = HCC85*gCopdCF;
            HCC85_gRenal_V23             = HCC85*Renal_V23;
            gRespDepandArre_gCopdCF      = Card_Resp_Fail*gCopdCF;
            HCC85_HCC96                  = HCC85*HCC96;
            gSubstanceAbuse_gPsychiatric_V23 = gSubstanceAbuse_V23*gPsychiatric_V23;

            %*institutional model;
            PRESSURE_ULCER = MAX(HCC157, HCC158); /*10/19/2012*/;
            CHF_gCopdCF                  = CHF*gCopdCF;
            gCopdCF_CARD_RESP_FAIL       = gCopdCF*CARD_RESP_FAIL;
            SEPSIS_PRESSURE_ULCER        = SEPSIS*PRESSURE_ULCER;
            SEPSIS_ARTIF_OPENINGS        = SEPSIS*(HCC188);
            ART_OPENINGS_PRESS_ULCER     = (HCC188)*PRESSURE_ULCER;
            DIABETES_CHF                 = DIABETES*CHF;
            gCopdCF_ASP_SPEC_B_PNEUM     = gCopdCF*(HCC114);
            ASP_SPEC_B_PNEUM_PRES_ULC    = (HCC114)*PRESSURE_ULCER;
            SEPSIS_ASP_SPEC_BACT_PNEUM   = SEPSIS*(HCC114);
            SCHIZOPHRENIA_gCopdCF        = (HCC57)*gCopdCF;
            SCHIZOPHRENIA_CHF            = (HCC57)*CHF;
            SCHIZOPHRENIA_SEIZURES       = (HCC57)*(HCC79);

            DISABLED_HCC85          = DISABL*(HCC85);
            DISABLED_PRESSURE_ULCER = DISABL*PRESSURE_ULCER;
            DISABLED_HCC161         = DISABL*(HCC161);
            DISABLED_HCC39          = DISABL*(HCC39);
            DISABLED_HCC77          = DISABL*(HCC77);
            DISABLED_HCC6           = DISABL*(HCC6);   

            disable_substAbuse_psych_V23 =
               (AGEF < 65 & gSubstanceAbuse_gPsychiatric_V23 > 0);

           END; *there are some diagnoses for a person;
           ELSE DO;
              DO I=1 TO &N_CC;
                 HCC(I)=0;
              END;
              DO OVER RV;
                 RV=0;
              END;
           END;
           *phone conversation with Sara 11/13/2017;
           IF AGEF > 1 THEN DO;
              HCC182=0;
              HCC183=0;
              HCC184=0;
           END; 
           *HCC Counts;
           ARRAY CHALL(187) &All_Elig_HCCs;
           ARRAY ZS(14) Z1-Z14;
           HCC_all = sum(of CHALL(*));
           do i = 1 to dim(ZS) ;
              ZS(i)=(HCC_all=i);
           end ;
           Z15P=(HCC_all>=15);

           LABEL
              F0_34  ="Female 0-34 "
              F35_44 ="Female 35-44"
              F45_54 ="Female 45-54"
              F55_59 ="Female 55-59"
              F60_64 ="Female 60-64"
              F65_69 ="Female 65-69"
              F70_74 ="Female 70-74"
              F75_79 ="Female 75-79"
              F80_84 ="Female 80-84"
              F85_89 ="Female 85-89"
              F90_94 ="Female 90-94"
              F95_GT ="Female 95+  "
              M0_34  ="Male 0-34   "
              M35_44 ="Male 35-44  "
              M45_54 ="Male 45-54  "
              M55_59 ="Male 55-59  "
              M60_64 ="Male 60-64  "
              M65_69 ="Male 65-69  "
              M70_74 ="Male 70-74  "
              M75_79 ="Male 75-79  "
              M80_84 ="Male 80-84  "
              M85_89 ="Male 85-89  "
              M90_94 ="Male 90-94  "
              M95_GT ="Male 95+    "
              HCC_all  = "All eligible HCCs: Single continuous integer variable"
              Z1= "1 HCC"
              Z2= "2 HCCs"
              Z3= "3 HCCs"
              Z4= "4 HCCs"
              Z5= "5 HCCs"
              Z6= "6 HCCs"
              Z7= "7 HCCs"
              Z8= "8 HCCs"
              Z9= "9 HCCs"
              Z10= "10 HCCs"
              Z11= "11 HCCs"
              Z12= "12 HCCs"
              Z13= "13 HCCs"
              Z14= "14 HCCs"
              Z15P="15 or more HCCs"
              HCC47_gCancer         = "Immune Disorders*Cancer                  "
              DIABETES_CHF          = "CHF*Diabetes                             "
              CHF_gCopdCF           = "CHF*Chronic Obstructive Pulmonary Disease"
              HCC85_gRenal_V23      = "CHF*Renal                                "
              gCopdCF_CARD_RESP_FAIL=
              "Cardiorespiratory Failure *Chronic Obstructive Pulmonary Disease"
              HCC85_HCC96           = "CHF*Specified Heart Arrhythmias          "
   
              OriginallyDisabled_Female="Originally Disabled Female "
              OriginallyDisabled_Male = "Originally Disabled Male)  "
              disable_substAbuse_psych_V23="Disabled w at least one of HCC54, HCC55, HCC56, HCC57, HCC58, HCC59, HCC60";

           %*score calculation;

           /***************************/
           /*    community models     */
           /***************************/;

        %IF "&SCOREMAC" ne "" %THEN %DO;
     %&SCOREMAC(PVAR=SCORE_COMMUNITY_NA,  RLIST=&COMM_REGA, CPREF=CNA_);
     %&SCOREMAC(PVAR=SCORE_COMMUNITY_ND,  RLIST=&COMM_REGD, CPREF=CND_);
     %&SCOREMAC(PVAR=SCORE_COMMUNITY_FBA, RLIST=&COMM_REGA, CPREF=CFA_);
     %&SCOREMAC(PVAR=SCORE_COMMUNITY_FBD, RLIST=&COMM_REGD, CPREF=CFD_);
     %&SCOREMAC(PVAR=SCORE_COMMUNITY_PBA, RLIST=&COMM_REGA, CPREF=CPA_);
     %&SCOREMAC(PVAR=SCORE_COMMUNITY_PBD, RLIST=&COMM_REGD, CPREF=CPD_);

           /***************************/
           /*   institutional model   */
           /***************************/;

     %&SCOREMAC(PVAR=SCORE_INSTITUTIONAL, RLIST=&INST_REG, CPREF=INS_);

           /***************************/
           /*   new enrollees model   */
           /***************************/;

     %&SCOREMAC(PVAR=SCORE_NEW_ENROLLEE, RLIST=&NE_REG, CPREF=NE_);
        %END;
           /****************************/
           /*   normalize the scores   */
           /***************************/;
          SCORE_COMMUNITY_NA     = SCORE_COMMUNITY_NA    *&DF;
          SCORE_COMMUNITY_ND     = SCORE_COMMUNITY_ND    *&DF;
          SCORE_COMMUNITY_FBA    = SCORE_COMMUNITY_FBA   *&DF;
          SCORE_COMMUNITY_FBD    = SCORE_COMMUNITY_FBD   *&DF;
          SCORE_COMMUNITY_PBA    = SCORE_COMMUNITY_PBA   *&DF;
          SCORE_COMMUNITY_PBD    = SCORE_COMMUNITY_PBD   *&DF;
          SCORE_INSTITUTIONAL    = SCORE_INSTITUTIONAL   *&DF;
          SCORE_NEW_ENROLLEE     = SCORE_NEW_ENROLLEE    *&DF;

          OUTPUT &OUTDATA;
       END; %*last record for a person;
     END; %*there is a person record;
 RUN;

 %**********************************************************************
 * step4: data checks and proc contents
 ***********************************************************************;
 PROC PRINT U DATA=&OUTDATA(OBS=46);
     TITLE '*** V2317P1M output file ***';
 RUN ;
 PROC CONTENTS DATA=&OUTDATA;
 RUN;

 %MEND V2317P1M;
