*****************************************************************************************
*The following is JCL if you are using an IBM-type mainframe:
*
*
*//JOBCARD
*//HCCSY23 EXEC SAS94,REGION=8M,
*// OPTIONS='ERRORS=0,NOCENTER,NEWS'
*//WORK  DD SPACE=(CYL,(1000,2))
*//WORK1   DD SPACE=(CYL,(2000,2))
*//LIBRARY DD DISP=SHR,DSN=XXXX.XXXXXXX.FORMATS  /* user-defined location of formats */
*//IN0 DD DISP=SHR,DSN=XXXX.XXXXXX   /*user-defined the location of macros  */
*//IN1 DD DISP=SHR,DSN=XXXX.PERSON     /*the location of person-level file  */
*//IN2 DD DISP=SHR,DSN=XXXX.DIAG       /*the location of the diagnosis file */  
*//INCOEF DD DISP=SHR,DSN=XXXX.RXCOEFF /*the location of the file containing all coefficients */
*//OUT DD DISP=(NEW,CATLG,KEEP),
*//    DSN=XXX.RXSY23.PERSON,         /*the output file containing person-level scores */ 
*//    SPACE=(TRK,(20,10),RLSE)
*//SYSIN  DD *
*
*
******************************************************************************************
*  If you are using PC-SAS, you must specify the location of the files 
*  on your PC in a libname/filename statement;
*
*LIBNAME LIBRARY "location of formats";
*FILENAME IN0 "location of macros";  
*LIBNAME IN1 "location of person-level file";
*LIBNAME IN2 "location of diagnosis file";
*LIBNAME INCOEF "location of the coefficients file";
*LIBNAME OUT "location for the output file"; 
*
 ***********************************************************************
 *  
 *   DESCRIPTION:
 *
 * R0823T1P program creates 84 RXHCC variables (&RXHCCV8_list84) and 
 * eight score variables for each person who is present in  
 * the PERSON file (provided by the user).
 * If a person has at least one diagnosis in the DIAG file (provided by 
 * the user) then RXHCC variables are created, otherwise RXHCCs are set 
 * to 0.
 *
 * Score variables are created using coefficients from the eight final
 * models - five risk adjustable models and three new enrollee models:
 *     1) Community, Non-Low Income, Aged, Continuing Enrollee
 *     2) Community, Non-Low Income, Non-Aged, Continuing Enrollee
 *     3) Community, Low Income, Aged, Continuing Enrollee
 *     4) Community, Low Income, Non-Aged, Continuing Enrollee
 *     5) Institutional Continuing Enrollee
 *     6) Community, Non-Low Income, New Enrollee
 *     7) Community, Low Income, New Enrollee
 *     8) Institutional New Enrollee
 *
 * Assumptions about input files:
 *   - both PERSON and DIAG files are sorted by person ID
 *
 *   - person level file has the following variables:
 *     :&IDVAR   - person ID variable (MBI for Medicare data)
 *     :DOB      - date of birth
 *     :SEX      - sex
 *     :OREC     - original reason for entitlement
 *     :ESRD     - end stage renal disease 
 *
 *   - diagnosis level file has the following vars:
 *     :&IDVAR - person ID variable 
 *     :DIAG   - diagnosis
*
 * The program supplies parameters to a main macro %R0823T1M that calls
 * other external macros:
 *     %AGESEXV4  - create age/sex and originally disabled variables
 *     %R08I0ED1  - perform edits to ICD10 diagnosis codes
 *     %R08X84L1  - assign labels to RXHCCs
 *     %R08X84H1  - set RXHCC=0 according to hierarchies
 *     %SCOREVAR  - calculate a score variable
 *
 * Program steps:
 *         step1: include external macros
 *         step2: define internal macro variables
 *         step3: merge person and diagnosis files outputting one
 *                record per person for each input person level record
 *         step3.1: declaration section
 *         step3.2: bring regression coefficients
 *         step3.3: merge person and diagnosis file
 *         step3.4: for the first record for a person set RXCC to 0
 *                  and calculate age
 *         step3.5: if there are any diagnoses for a person
 *                  then do the following:
 *                   - perform ICD10 edits using R08I0ED1 macro
 *                   - create RXCC using provided format 
 *                   - create additional RXCC using additional formats
 *         step3.6: for the last record for a person do the
 *                  following:
 *                   - create demographic variables needed
 *                     for regressions (macro AGESEXV4)
 *                   - create RXHCC using hierarchies (macro R08X84H1)
 *                   - create RXHCC interaction variables
 *                   - create RXHCC and NonAged interaction variables
 *                   - set RXHCCs and interaction vars to zero if there
 *                     are no diagnoses for a person
 *                   - create scores for risk adjustable models
 *                   - create scores for new enrollee models
 *         step4: data checks and proc contents
 *
 *   USER CUSTOMIZATION:
 * The user must provide 2 files with the variables described above and
 * set the following parameters:
 *      INP      - SAS input person dataset
 *      IND      - SAS input diagnosis dataset
 *      OUTDATA  - SAS output dataset
 *      IDVAR    - name of person id variable (MBI for Medicare data)
 *      KEEPVAR  - variables kept in output dataset in addition to 
 *                 PersonID
 *      SEDITS   - a switch that controls whether to perform MCE edits 
 *                 on ICD10: 1-YES, 0-NO  
 *      DATE_ASOF- as-of date to calculate age (February 1 of payment
 *                 year)
 **********************************************************************;

 * input variables from PERSON file (in addition to Person ID variable); 
 %LET INPUTVARS=%STR(SEX DOB OREC ESRD);             

 * 24 continuing enrollee age/sex variables;
 %let AGESEXVARS=%str(
  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
 );

 * 32 new enrollee age/sex variables;
 %let NEAGESEXVARS=%str(
  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
 );

 * 5 demographic variables;
 %let DEMVARS=%str(
  AGEF 
  ORIGDS 
  NonAged
  F65OD 
  M65OD 
 );

 * 84 RXHCCs included in models;
 %let RXHCCV8_list84 =%str( 
  RXHCC1 RXHCC5 RXHCC15 RXHCC16 RXHCC17 RXHCC18 RXHCC19 
  RXHCC20 RXHCC21 RXHCC22 RXHCC30 RXHCC31 
  RXHCC40 RXHCC41 RXHCC42 RXHCC43 RXHCC44 RXHCC47 RXHCC54 
  RXHCC55 RXHCC56 RXHCC59 RXHCC65 RXHCC66 RXHCC67
  RXHCC80 RXHCC81 RXHCC82 RXHCC83 RXHCC84 RXHCC87 
  RXHCC95 RXHCC96 RXHCC98 RXHCC99 RXHCC100 RXHCC111
  RXHCC112 RXHCC130 RXHCC131 RXHCC132 RXHCC133 RXHCC146 
  RXHCC147 RXHCC148 RXHCC153 RXHCC154 RXHCC155
  RXHCC157 RXHCC158 RXHCC159 RXHCC160 RXHCC161 RXHCC163 
  RXHCC164 RXHCC166 RXHCC168 RXHCC183 RXHCC184
  RXHCC186 RXHCC187 RXHCC188 RXHCC191 RXHCC193 RXHCC207 
  RXHCC215 RXHCC225 RXHCC226 RXHCC227 RXHCC228
  RXHCC229 RXHCC243 RXHCC244 RXHCC260 RXHCC261 RXHCC262 
  RXHCC263 RXHCC311 RXHCC314 RXHCC316 RXHCC317
  RXHCC355 RXHCC395 RXHCC396
 );

 * 84 RXCCs that correspond to model RxHCCs;
 %let RXCCV8_list84 =%str( 
  RXCC1 RXCC5 RXCC15 RXCC16 RXCC17 RXCC18 RXCC19 RXCC20 
  RXCC21 RXCC22 RXCC30 RXCC31  RXCC40 RXCC41
  RXCC42 RXCC43 RXCC44 RXCC47 RXCC54 RXCC55 RXCC56 
  RXCC59 RXCC65 RXCC66 RXCC67 RXCC80 RXCC81 RXCC82 RXCC83
  RXCC84 RXCC87 RXCC95 RXCC96 RXCC98 RXCC99 RXCC100 
  RXCC111 RXCC112 RXCC130 RXCC131 RXCC132 RXCC133 RXCC146
  RXCC147 RXCC148 RXCC153 RXCC154 RXCC155 RXCC157 
  RXCC158 RXCC159 RXCC160 RXCC161 RXCC163 RXCC164 RXCC166
  RXCC168 RXCC183 RXCC184 RXCC186 RXCC187 RXCC188 
  RXCC191 RXCC193 RXCC207 RXCC215 RXCC225 RXCC226 RXCC227
  RXCC228 RXCC229 RXCC243 RXCC244 RXCC260 RXCC261 
  RXCC262 RXCC263 RXCC311 RXCC314 RXCC316 RXCC317 RXCC355
  RXCC395 RXCC396
 );

 * 8 score variables;
 %let SCOREVARS=%str(
   SCORE_CE_NoLowAged   
   SCORE_CE_NoLowNoAged 
   SCORE_CE_LowAged     
   SCORE_CE_LowNoAged   
   SCORE_CE_LTI    
   SCORE_NE_NonLowCommunity     
   SCORE_NE_LowCommunity   
   SCORE_NE_LTI      
  );   


 %INCLUDE IN0(R0823T1M)/SOURCE2;

 %R0823T1M( INP      =IN1.PERSON,
            IND      =IN2.DIAG,
            OUTDATA  =OUT.PERSON,
            IDVAR    =MBI,
            KEEPVAR  =MBI &INPUTVARS &SCOREVARS &DEMVARS &AGESEXVARS 
                      &NEAGESEXVARS &RXHCCV8_list84 &RXCCV8_list84,
            SEDITS   =1,
            DATE_ASOF="1FEB2023"D);
