/****************************************************************************************
Version 3.0 Men_CRC_RAM_agerange4090 (Fanni Zhang)                               01/30/19
Updates: - replace old incidence rates with new rates (SEER18 2000-2015) in H1_Star data step;
         - replace old mortality rates with new rates (2010-2015) in H2 data step;
         - extend the age range for the new rates to allow projections to age 40.

Version 2.0                                                                      07/21/15
Version 1.0                                                                      05/26/10

Men_CRC_RAM: Men (C)ol(R)ectal (C)ancer (R)isk (A)ssessment (M)acro

SAS macro to project Colorectal Cancer abs risk for men ages 50 or older, based on a
relative risk model from case-control data collected in Utah, Minnesota and Northern
California (first primary tumor diagnosed in 1991-1994  and 1997-2001).

Ref: "Colorectal Cancer Risk Prediction Tool for White Men and Women Without Known
      Susceptibility".   A Freedman et al.  JCO  27:686-693 2008.
****************************************************************************************/

option   nocenter  ls= 133  ps=60  notes;

/*
        Men_CRC_RAM:  SAS macro which performs Colorectal Cancer abs risk projection

        !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

        Modification/change of any code from this point onward should be avoided,
        Any changes to these codes are done at the user's risk, since changes may
        inadvertenly cause the macro to be fundamentally altered resulting in incorrect
        projections and/or abnormal termination

        !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
*/

***                        macro               macro parameters needs to point;
***                        parameters          to an appropriate sas file name;
***                                            or sas variable name;

%macro  Men_CRC_RAM       (SAS_File           =,

                           ID_Num             =,

                           Race               =,
                           T1                 =,
                           T2                 =,

                           Sigmoid_Polyps     =,
                           NoIBuprofen        =,
                           Vigorus_Xrcis      =,

                           Cig_Yrs            =,
                           Vegetable          =,
                           Num_Cigs           =,

                           BMI_Trnd           =,
                           No_NSaids          =,
                           Fam_Hist_CRC       =,

                           PattrnNumber       =,
                           AbsRsk             =);

/*
      !!!!!!!!!!!!!!!!!!!                                                          !!!!!!!!!!!!!!!!!!!!
      !!!!!!!!!!!!!!!!!!!  READ THE DEFINITIONS OF RELATIVE RISK COVARIATES BELOW  !!!!!!!!!!!!!!!!!!!!
      !!!!!!!!!!!!!!!!!!!                                                          !!!!!!!!!!!!!!!!!!!!

       SAS_File            points to a sas system file which contains rr covariates
                           for men under investiation i.e. men whom we wish to project
                           absolute risk based on their race, current age, projection age
                           and relative risk covariate values

                           the sas file which SAS_File points to must be created prior to
                           involking this SAS macro


       Race                =1, white
                           =2, african-american
                           =3, hispanic
                           =4, asian-american


       T1                  T1, current age ranges from:  40 to 85             (1/18/2019 email)

       T2                  T2, projection age ranges from:  T2>T1  and  T2<=90


       Sigmoid_Polyps      =0, yes sigmoidoscopy and/or colonscopy last 10 years,  no history of polyps
                           =1.  no sigmoidoscopy and/or colonscopy last 10 years
                           =2, yes sigmoidoscopy and/or colonscopy last 10 years, yes history of polyps
                           =3, unk sigmoidoscopy and/or colonscopy last 10 years, unk history of polyps  or
                               yes sigmoidoscopy and/or colonscopy last 10 years, unk history of polyps

       NoIBuprofen         =0, regular user IBuprofen...Advil,Nuprin non-Aspirin NSaids  or
                              -9=don't know (10/16/2014 meeting)

                           =1,     non user IBuprofen...Advil,Nuprin non-Aspirin NSaids

       Vigorus_Xrcis       =0, 0 hrs/wk
                           =1, greater than 0 and less than equal 2 hrs/wk
                           =2, greater than 2 and less than equal 4 hrs/wk
                           =3, greater than 4 hrs/wk

       Cig_Yrs             =0, # yrs cigarette smoking  =  0  or
                               don't know               = -9                  (10/16/2014 meeting)

                           =1, # yrs cigarette smoking  >  0  and  < 15
                           =2, # yrs cigarette smoking >= 15  and  < 35
                           =3, # yrs cigarette smoking >= 35

       Vegetable           =0, greater   equal to five servings of vegetables per week
                           =1, less      than     five servings of vegetables per week

       Num_Cigs            =0, # cigarettes smoked   =  0     or
                               don't know            = -9                     (10/16/2014 meeting)

                           =1, # cigarettes smokies >=  1  and  <  11
                           =2, # cigarettes smokies >= 11  and  <= 20
                           =3, # cigarettes smokies >= 21

       BMI_Trnd            =0, BMI <  25
                           =1, BMI >= 25  and  BMI <  30
                           =2, BMI >= 30

       No_NSaids           =0, regular user of NSaids such as Aspirin and or IBuprofen    or
                              -9=don't know (10/16/2014 meeting)

                           =1,     non user of NSaids such as Aspirin and IBuprofen



       Fam_Hist_CRC        =0, 0  relative  with CRC                                 or
                              -9= don't know if any relative with CRC  (10/16/2014)

                           =1, 1  relative  with CRC                                 or
                              -8= have relatives with CRC,  don't know how many

                           =2, >=2 relatives with CRC


       AbsRsk (%)          projected absolute risk of CRC (units of %)



       !!!!!  Important Pleases Read   !!!!!
       Section on Data exceptions:

       if Num_Cigs smoked is 0 or don't know (-9), then both Num_Cigs and Cig_Yrs are set to lowest RR level
       i.e. Num_Cigs is set to 0  and  Cig_Yrs is set to 0.


       For   NoIBuprofen,     dont know=-9
             No_NSaids,       dont know=-9
             Cig_Yrs,         dont know=-9
             Num_Cigs,        dont know=-9
             Fam_Hist_CRC     dont know if any relatives have CRC=-9
             -9 is mapped to  lowest RR level of 0.  (10/16,24/2014  meeting with R Pfeiffer)


       For   Fam_Hist_CRC     has relatives with CRC, dont know how many relatives with CRC=-8
             -8 is mapped to RR level of 1 relative with CRC


       !!!!!!!!!!!!  Warning Warning Warning Warning Warning Warning  !!!!!!!!!!!!
       !!!!!!!!!!!!    Warning about inadmissible error conditions    !!!!!!!!!!!!

       Inadmissible condition for  Num_Cigs  and  Cig_Yrs

       Num_Cigs  and  Cig_Yrs   have to be both   0  simultaneously
                                  or to be both  >0  simultaneously

                                can't have one    0  & the other  >0
                                this is an error condition and will
                                result in an unknown value for abs risk


       Inadmissable condition of  NoIBuprofin=0   and  No_NSAIDS=1

       i.e.  individual uses IBuprofin  but does not use NSaids.
       This is contradictory since IBuprofin is a NSaid drug
       projected absolute risk for records with this contradition will be missing

       !!!!!!!!!!!!    Warning about inadmissible error conditions    !!!!!!!!!!!!
       !!!!!!!!!!!!  Warning Warning Warning Warning Warning Warning  !!!!!!!!!!!!
*/


   data  TmpFile;      *** temporary working sas file containing rr covariate data;
   set  &SAS_File;     *** original  input   sas file containing rr covariate data;


         retain  Count_Error 0;             *** counts # of editing errors in input file;

         title3 'Men_CRC_RAM  sas macro for Men Colorectal Cancer absolute risk projection';

                 SEQ_Num = _n_;

                 Error_Ind = 0;                        *** error in input file indicator;
         label   Error_Ind = "If mean not 0, implies ERROR in file  ---->";


         *** codes to test for consistency of T1 (initial age) and T2 (projection age);
         set_T1_Missing = &T1;
         set_T2_Missing = &T2;

         if (&T1 lt 40 or &T1 gt 85) then do;       *** men must be ages ge 40 and le 85 01/28/2019;
            set_T1_Missing = .;
            Error_Ind      = 1;
         end;
         
         /*  
         if (&T1 lt 40 or &T1 gt 85) then do;       *** men must be ages ge 40 and le 85;
            set_T1_Missing = .;
            Error_Ind      = 1;
         end;
         */
         if (&T2 gt 90) then do;                        *** projection age must be le 90;
            set_T2_Missing = .;
            Error_Ind      = 1;
         end;

         if (&T1 ge &T2) then do;        *** curent age must be less than projection age;
            set_T1_Missing = .;
            set_T2_Missing = .;
            Error_Ind      = 1;
         end;


         *** RR covariates have already been re-coded to integers 0, 1, 2 ...;

         label  Err_Race   = "Set to missing value . when Race of woman is in error"

                Err_Sig_Pol= "Set to missing value . when Sigmiodscopy and Polyps has error"
                Err_IBuprof= "Set to missing value . when IBuprofen use has error"
                Err_Xrcis  = "Set to missing value . when Vigorous Exercise has error"

                Err_CigYr  = "Set to missing value . when Number Yrs Smoked has error"
                Err_Veg    = "Set to missing value . when Vegetable Intake has error"
                Err_Cigs   = "Set to missing value . when Number Cigarette Smoked has error"
                Err_BMI    = "Set to missing value . when Body Mass Index has error"

                Err_NSad   = "Set to missing value . when NSaids use has error"
                Err_FamHx  = "Set to missing value . when Family History CRC has error";


         if      (&Race            eq  1  or
                  &Race            eq  2  or
                  &Race            eq  3  or
                  &Race            eq  4) then  Err_Race    = &Race;
         else                                   Err_Race    =     .;

         if      (&Sigmoid_Polyps  eq  0  or
                  &Sigmoid_Polyps  eq  1  or
                  &Sigmoid_Polyps  eq  2  or
                  &Sigmoid_Polyps  eq  3) then  Err_Sig_Pol = &Sigmoid_Polyps;
         else                                   Err_Sig_Pol =               .;



         if      (&NoIBuprofen     eq  0  or
                  &NoIBuprofen     eq  1) then  Err_IBuprof = &NoIBuprofen;
         else if (&NoIBuprofen     eq -9) then  Err_IBuprof =            0;   *** (10/16/2014 meeting);
         else                                   Err_IBuprof =            .;

         if      (&Vigorus_Xrcis   eq  0  or
                  &Vigorus_Xrcis   eq  1  or
                  &Vigorus_Xrcis   eq  2  or
                  &Vigorus_Xrcis   eq  3) then  Err_Xrcis   = &Vigorus_Xrcis;
         else                                   Err_Xrcis   =              .;

         if      (&Cig_Yrs         eq  0  or
                  &Cig_Yrs         eq  1  or
                  &Cig_Yrs         eq  2  or
                  &Cig_Yrs         eq  3) then  Err_CigYr   = &Cig_Yrs;
         else if (&Cig_Yrs         eq -9) then  Err_CigYr   =        0;   *** (10/16/2014 meeting);
         else                                   Err_CigYr   =        .;

         if      (&Vegetable       eq  0  or
                  &Vegetable       eq  1) then  Err_Veg     = &Vegetable;
         else                                   Err_Veg     =          .;

         if      (&Num_Cigs        eq  0  or
                  &Num_Cigs        eq  1  or
                  &Num_Cigs        eq  2  or
                  &Num_Cigs        eq  3) then  Err_Cigs    = &Num_Cigs;
         else if (&Num_Cigs        eq -9) then  Err_Cigs    =         0;      *** (10/16/2014 meeting);
         else                                   Err_Cigs    =         .;

         if      (Err_Cigs         eq  0  or
                  Err_CigYr        eq  0) then  do;                           *** # cigs smoked = 0 or
                                                                                  # yrs  smoke  = 0   ;
                                                Err_Cigs    = 0;              *** set both cigarette  ;
                                                Err_CigYr   = 0;              *** related RR covariate;
                                                end;                          *** to lowest RR level 0;

         if      (&BMI_Trnd        eq  0  or
                  &BMI_Trnd        eq  1  or
                  &BMI_Trnd        eq  2) then  Err_BMI     = &BMI_Trnd;
         else                                   Err_BMI     =         .;

         if       (&No_NSaids      eq  0  or
                   &No_NSaids      eq  1) then  Err_NSad   = &No_NSaids;
         else if  (&No_NSaids      eq -9) then  Err_NSad   =          0;      *** (10/16/2014 meeting);
         else                                   Err_NSad   =          .;

         if       (&Fam_Hist_CRC   eq  0  or
                   &Fam_Hist_CRC   eq  1  or
                   &Fam_Hist_CRC   eq  2) then  Err_FamHx  = &Fam_Hist_CRC;
         else if  (&Fam_Hist_CRC   eq -9) then  Err_FamHx  =             0;   *** dont know if relatives have CRC;
         else if  (&Fam_Hist_CRC   eq -8) then  Err_FamHx  =             1;   *** has relative with CRC dont know how many;
         else                                   Err_FamHx  =             .;



         /*  replaced by Oct 21, 2014 e-mail to R Pfeiffer
         *** inadmissable condition of  No IBuprofen=0  and  No NSAIDS=1
             i.e.  subject regularly uses IBuprofen, NSaids not contain aspirin
                   but does not use NSaids contain aspirin
                   This is contradictory since IBuprofen is a NSaid drug  change
                   No NSaids=1 (Yes)  to  No NSaids=0 (no)

         if (Err_IBuprof  eq  0  and  Err_NSad  eq 1) then do;
            Err_NSad    = 0;          *** change wrking NSaids to 0, i.e. regularly takes NSaids containing aspirin;
         end;
         */



         /*  replaced by Oct 21, 2014 e-mail to R Pfeiffer
         *** i.e.  subject does not regularly uses IBuprofen and does not regularly uses NSaids
             Err_IBuprof =1 doesnt take IBuprofen NSaids not containing Aspirin
             Err_NSad    =1 doesnt take           NSaids Aspirin as well as IBuprofen
             For  BOTH  NoIBuprofen=no (1)  and  No_Saids=no (1) then  No_NSaids=no (1)  (Thu 10/16/2014 4:09 PM e-mail);

         if (Err_IBuprof  eq  1 and  Err_NSad  eq 1) then do;
             Err_NSad    =1;   *** keep Err_NSad, regularly takes NSaids containing aspirin, as doesnt take (1);
         end;
         */



         /*  replaced by Oct 21, 2014 e-mail to R Pfeiffer
             following paragraph of code is switched off
         *** if IBuprofen=yes  and/or  NSaids=yes,
             change both to Yes                                           10/6/2014
             RE  CLARIFICATION NEEDED PLEASE  Colorectal risk screening questionnaire (2).msg
             e-mail of 09/29/2014;

         if (Err_IBuprof eq 0  or  Err_NSad eq 0) then do;
             Err_IBuprof = 0;          *** change wrking IBuprofen to Yes (0) regularly takes;
             Err_NSad    = 0;          *** change wrking NSaids    to Yes (0) regularly takes;
         end;
         */



         /*
         *** if IBuprofen=NO (1)  and      NSaids=NO (1)     (Thu 10/16/2014 4:09 PM e-mail)
             change wrking NSaids=NO (1);

         if (Err_IBuprof eq 1  and  Err_NSad eq 1) then do;
             Err_NSad    = 1;          *** change wrking NSaids  to  NO (1);
         end;
         */



         *** Oct21, 2014 e-mail to R Pfeiffer;
         if (Err_IBuprof  eq -9                       ) then Err_IBuprof = 0;  *** changing -9 unknown response to 0 RR;
         if (                         Err_NSad  eq  -9) then Err_NSad    = 0;  *** changing -9 unknown response to 0 RR;

         if (Err_IBuprof  eq  0  and  Err_NSad  eq   0) then do;                  *** do nothing loop;
                                                                Err_IBuprof = 0;  *** this loop does not;
                                                                Err_NSad    = 0;  *** change any values;
                                                             end;

         if (Err_IBuprof  eq  0  and  Err_NSad  eq   1) then do;                  *** inadmissible condition;

                                                                /*   Check if this is right or next 2 lines right 4/17/15 */
                                                                /*   These two lines are correct                  6/02/15 */
                                                                Err_IBuprof = .;  *** of IBuprofen =taken   &;
                                                                Err_NSad    = .;  ***    NSaids    =not taken;
                                                                /**/

                                                                /*  This is what CRC RAT does. Since it give same results
                                                                    if these two lines performed.  But this is wrong 6/2/15
                                                                Err_IBuprof = 0;  *** of IBuprofen=taken  & ;
                                                                Err_NSad    = 0;  ***    NSaids   =taken    ;
                                                                */
                                                             end;

         if (Err_IBuprof  eq  1  and  Err_NSad  eq   0) then do;                  *** do nothing loop;
                                                                Err_IBuprof = 1;  *** this loop does not;
                                                                Err_NSad    = 0;  *** change any values;
                                                             end;

         if (Err_IBuprof  eq  1  and  Err_NSad  eq   1) then do;                  *** do nothing loop;
                                                                Err_IBuprof = 1;  *** this loop does not;
                                                                Err_NSad    = 1;  *** change any values;
                                                             end;



         *** inadmissable condition of  Cig_Yrs=0  and  Num_Cigs>0    or
                                        Cig_Yrs>0  and  Num_Cigs=0
         i.e. subject has     smoked but has smoked 0 cigerattes  or
              subject has not smoked but has smoked   cigerattes      10/6/2014;

         if ((Err_CigYr eq 0 and Err_Cigs gt 0)  or
             (Err_CigYr gt 0 and Err_Cigs eq 0)) then do;
            /*
            file  print;
            put  "ID # "  ID_Num  6.0  " is in error ";
            put  "CigYrs=0  and  #Cigs>0  or  CigYrs>0  and  #Cigs=0";
            put  "This person will have a missing Projected Absolute Risk";
            */

            Err_CigYr = .;         *** set wrking # of Cig Years to missing .;
            Err_Cigs  = .;         *** set wrking # of Cig Smoke to missing .;
         end;



         if     (Err_Race        eq  .  or

                 Err_Sig_Pol     eq  .  or
                 Err_IBuprof     eq  .  or
                 Err_Xrcis       eq  .  or

                 Err_CigYr       eq  .  or
                 Err_Veg         eq  .  or
                 Err_Cigs        eq  .  or

                 Err_BMI         eq  .  or
                 Err_NSad        eq  .  or
                 Err_FamHx       eq  .) then  Error_Ind = 1;  *** switch on data error indicator;



         *** create relative risk covariates for the three different sites of cancer,
             Rectal, Prxomial and Distal.

             These names must be different from input RR covariate names by adding "_"
             or shortening etc.  These names are the ones used in the LP calculations
             so sometimes they will take on sas missing values "."  when the covariate
             is in edit error condition.

             note that all Err_xxxxx variables take on missing value . when there are
             editing errors;

         posi_0Ind = 0;
         posi_1Ind = 0;
         posi_2Ind = 0;
         posi_3Ind = 0;

         if      (ERR_Sig_Pol eq 0) then     posi_0Ind = 1;
         else if (ERR_Sig_Pol eq 1) then     posi_1Ind = 1;
         else if (ERR_Sig_Pol eq 2) then     posi_2Ind = 1;
         else if (ERR_Sig_Pol eq 3) then     posi_3Ind = 1;
         else if (ERR_Sig_Pol eq .) then  do;
                                             posi_0Ind = .;
                                             posi_1Ind = .;
                                             posi_2Ind = .;
                                             posi_3Ind = .;
                                          end;


         No_IBuprofen                        = Err_IBuprof;


                                  TotlRelge1 = Err_FamHx;
         if (Err_FamHx eq 2) then TotlRelge1 = 1;


                                XrcisHrsTrnd = 3 - Err_Xrcis;     *** reverse the polarity for Exercise so that RR levels become
                                                                      0=4+hrs 1=3,4hrs  2=1,2hrs  3=0hrs for use in RR modelling;

         CigYr_0 = 0;
         CigYr_1 = 0;
         CigYr_2 = 0;
         CigYr_3 = 0;

         if      (Err_CigYr eq 0) then     CigYr_0 = 1;
         else if (Err_CigYr eq 1) then     CigYr_1 = 1;
         else if (Err_CigYr eq 2) then     CigYr_2 = 1;
         else if (Err_CigYr eq 3) then     CigYr_3 = 1;
         else if (Err_CigYr eq .) then  do;
                                           CigYr_0 = .;
                                           CigYr_1 = .;
                                           CigYr_2 = .;
                                           CigYr_3 = .;
                                        end;


         NumRelTrnd   = Err_FamHx;
         Num_Cigarets = Err_Cigs;
         No_IBuprofen = Err_IBuprof;
         NoNSADS      = Err_NSad;

         BMITrnd      = Err_BMI;
         Veg_lt5      = Err_Veg;


        &PattrnNumber =     (Err_FamHx+1)   +
                          3*(Err_Cigs)      +
                        4*3*(Err_IBuprof)   +
                      2*4*3*(Err_NSad)      +

                    2*2*4*3*(ERR_Sig_Pol)   +

                  4*2*2*4*3*(Err_BMI)       +
                3*4*2*2*4*3*(Err_Veg)       +

              2*3*4*2*2*4*3*(Err_CigYr)     +
            4*2*3*4*2*2*4*3*(XrcisHrsTrnd);          ***  XrcisHrsTrnd=3-Err_Xrcis reverses the polarity of Hrs Exercise;


         if (Error_Ind eq 1 and Count_Error lt 1000) then do;

            put  "Error listing for the input file  (listing up to a max of 1000 records with errors)";

            file print;

            Count_Error = Count_Error + 1;

            if (Count_Error eq 1) then do;
               put " ";
               put "Error listing for the input file  (listing up to a max of 1000 records with errors)";
               put " ";
               put "                              Sigmod            Vigor";
               put "    ID          Currn  Prjcn  Colysc        No  Xrcis  Cig  Vgtble  Num  BMI      No  NumRel";
               put "     #    Race    Age    Age  Polyps  IBuprofn   0123  Yrs   <5/wk  Cig  012  NSaids     012";
               put " ";
            end;

            put
              &ID_Num                    6.0

              &Race                      8.0
              &T1                        7.1
              &T2                        7.1

              &Sigmoid_Polyps            8.0
              &NoIBuprofen              10.0
              &Vigorus_Xrcis             7.0

              &Cig_Yrs                   5.0
              &Vegetable                 8.0
              &Num_Cigs                  5.0

              &BMI_Trnd                  5.0
              &No_NSaids                 8.0
              &Fam_Hist_CRC              8.0;

            put
               Err_Race                 14.0
               set_T1_Missing            7.1
               set_T2_Missing            7.1

               Err_Sig_Pol               8.0
               Err_IBuprof              10.0
               Err_Xrcis                 7.0

               Err_CigYr                 5.0
               Err_Veg                   8.0
               Err_Cigs                  5.0

               Err_BMI                   5.0
               Err_NSad                  8.0
               Err_FamHx                 8.0;

            put " ";
         end;

         Label  Count_Error = "Counts # of editing errors in input file ";

         Key = 1;
   run;


   data &Sas_File;
   set  TmpFile;
   run;


   ***   set up h1*, h2, beta & F(t) with known constants used in the nci brca risk disk;

   data  H1_Star;                                  *** h1star,  CRC composite incidences;

         array  Ages     (II)  A1-A10
        (40, 45, 50, 55, 60, 65, 70, 75, 80, 85);

         *** men rectal cancer composite incidences;

         array  WhiteR_H1 (II)  WRH1_1-WRH1_10
        (0.000089196,0.00015404,0.000279069,0.000311145,0.000388472,0.000475544,
         0.000536782,0.000626264,0.000673191,0.000685024);   *** SEER18 incidence 2000-2015;

         array  BlackR_H1 (II)  BRH1_1-BRH1_10
        (0.000067779,0.000147764,0.00031165,0.000387965,0.000499556,0.000557388,
         0.000618423,0.000599039,0.000824465,0.000632715);

         array  HspncR_H1 (II)  HRH1_1-HRH1_10

        (0.000059508,0.000114764,0.0002375,0.000311163,0.000400663,0.000523825,
         0.00057289,0.000599579,0.000725084,0.000661517);

         array  AsianR_H1 (II)  ARH1_1-ARH1_10
        (0.000075486,0.00013998,0.000303994,0.000346034,0.00044832,0.000535561,
         0.000548544,0.000607099,0.000720916,0.000669886);

         *** men proximal cancer composite incidences;

         array  WhiteP_H1 (II)  WPH1_1-WPH1_10
        (0.000059106,0.000094396,0.000165414,0.000228887,0.000343665,0.000543806,
         0.000822461,0.001096504,0.001453321,0.001621485);   *** SEER18 incidence 2000-2015;

         array  BlackP_H1 (II)  BPH1_1-BPH1_10
        (0.000081436,0.000147764,0.000272013,0.000427224,0.000564453,0.000923829,
         0.001105514,0.001283655,0.00170782,0.001431651);

         array  HspncP_H1 (II)  HPH1_1-HPH1_10
        (0.000037623,0.000063079,0.000134852,0.000198053,0.000317448,0.000481992,
         0.000707204,0.000855138,0.001075836,0.001271582);

         array  AsianP_H1 (II)  APH1_1-APH1_10
        (0.000035782,0.000067319,0.000103039,0.000149862,0.000234278,0.000378658,
         0.000463702,0.00059058,0.000792386,0.000964496);

         *** men distal cancer composite incidences;

         array  WhiteD_H1 (II)  WDH1_1-WDH1_10
        (0.000054163,0.000096446,0.000190759,0.000210349,0.000289598,0.000388529,
         0.00047729,0.0005568,0.00063606,0.000652245);     *** SEER18 incidence 2000-2015;

         array  BlackD_H1 (II)  BDH1_1-BDH1_10
        (0.000055133,0.000120181,0.000230889,0.000290974,0.00045277,0.000560699,
         0.000674946,0.000790329,0.000744542,0.000691696);

         array  HspncD_H1 (II)  HDH1_1-HDH1_10
        (0.000034918,0.00007336,0.000156656,0.000205095,0.000286011,0.000428336,
         0.00047284,0.000593682,0.000621922,0.000558614);

         array  AsianD_H1 (II)  ADH1_1-ADH1_10
        (0.000046076,0.000089224,0.000199816,0.000229617,0.000376713,0.00049895,
         0.000533916,0.000660788,0.000699164,0.000747046);

         II  = .;
         Key = 1;
   run;


   data  H2;                                             *** h2, men competing hazards;

         array  White_H2 (II)  WH2_1-WH2_10
        (0.002517029,0.003880489,0.005977758,0.008746618,0.012207925,0.017445394,
         0.026850993,0.042880578,0.07182451,0.15326379);  *** new mortality rates 2010-2015;

         array  Black_H2 (II)  BH2_1-BH2_10
        (0.003600335,0.005374977,0.008527183,0.01348236,0.019635905,0.026224221,
         0.037208311,0.053555824,0.079356856,0.136105911);

         array  Hspnc_H2 (II)  HH2_1-HH2_10
        (0.001751008,0.002755742,0.00441541,0.006694739,0.009789987,0.013955647,
         0.021310691,0.033762895,0.05510037,0.106585778);

         array  Asian_H2 (II)  AH2_1-AH2_10
        (0.00100262,0.001627231,0.002519184,0.003990335,0.006037592,0.008857362,
         0.014030781,0.023964808,0.042910879,0.097537978);

         II  = .;
         Key = 1;
   run;


   data  LN_RR;

         *** Men Rectal Model;

         array  Rectl_Beta (RR)   posi0R_Beta    posi1R_Beta    posi2R_Beta    noIBuprofnBeta
                                  NRelGE1_Beta   XrcisHrs_Beta
                                 (0.6760430335,  2.0252428101,  1.3283598064,  0.4202874746,
                                  0.4006854532,  0.1882660083);

         array  Rectl_Name (RR) $ RBeta1-RBeta6  ('Posi0Ind', 'Posi1Ind', 'Posi2Ind', 'NoIBupro',
                                                  '#Rel >=1', 'XrcsTrnd');


         *** Men Proximal Model;

         array  Prxml_Beta (PP)   CigYr0_Beta    CigYr2_Beta    CigYr3_Beta    VegLT5_Beta
                                  NumCigs_Beta   BMITrndPBeta   posi1P_Beta    posi2P_Beta
                                  posi3P_Beta    noNSaidPBeta   RelTrnPBeta
                                 (0.5101756750,  0.3843918169,  0.1129943060,  0.5536174113,
                                  0.2656643161,  0.2327835021,  0.3511482106,  0.5707313462,
                                  0.4541009837,  0.4343130874,  0.5935996371);

         array  Prxml_Name (PP) $ PBeta1-PBeta11 ('Cig Yr 0', 'Cig Yr 2', 'Cig Yr 3', 'Veg<5Ind',
                                                  'Num Cigs', 'BMI Trnd', 'Posi1Ind', 'Posi2Ind',
                                                  'Posi3Ind', 'noNSAIDs', 'FamHx012');



         *** Men Distal Model;

         array  Distl_Beta (DD)   BMITrndDBeta   posi1D_Beta    posi2D_Beta    posi3D_Beta
                                  noNSaidDBeta   RelTrnDBeta
                                 (0.3197633636,  1.0404879015,  0.2956944224,  0.9602749397,
                                  0.3413610714,  0.5158856117);

         array  Distl_Name (DD) $ DBeta1-DBeta6  ('BMI Trnd', 'Posi1Ind', 'Posi2Ind', 'Posi3Ind',
                                                  'noNSAIDs', 'FamHx012');

         RR  = .;
         PP  = .;
         DD  = .;

         Key = 1;
   run;


   data  FofT;                                            *** 1-Attributable Risk = F(t);

         Rectal_1_AR = 0.099728891869;
         Proxml_1_AR = 0.136245677145;
         Distal_1_AR = 0.275494599554;

         Key = 1;
   run;




   ***   The following 5 data steps, gathers all the needed parameters/constants
         and the input data set together to get ready for risk projection;

   data  TmpFile;
   merge TmpFile  H1_Star;
   by    Key;
   run;


   data  TmpFile;
   merge TmpFile  H2;
   by    Key;
   run;


   data  TmpFile;
   merge TmpFile  LN_RR;
   by    Key;
   run;


   data  TmpFile  (drop=Key);
   merge TmpFile  FofT;
   by    Key;
   run;


   *** documents all constants;

   data  Dummy;
   set   TmpFile;

         file print;

         array  WhiteR_H1 (II)  WRH1_1-WRH1_10;
         array  BlackR_H1 (II)  BRH1_1-BRH1_10;
         array  HspncR_H1 (II)  HRH1_1-HRH1_10;
         array  AsianR_H1 (II)  ARH1_1-ARH1_10;

         array  WhiteP_H1 (II)  WPH1_1-WPH1_10;
         array  BlackP_H1 (II)  BPH1_1-BPH1_10;
         array  HspncP_H1 (II)  HPH1_1-HPH1_10;
         array  AsianP_H1 (II)  APH1_1-APH1_10;

         array  WhiteD_H1 (II)  WDH1_1-WDH1_10;
         array  BlackD_H1 (II)  BDH1_1-BDH1_10;
         array  HspncD_H1 (II)  HDH1_1-HDH1_10;
         array  AsianD_H1 (II)  ADH1_1-ADH1_10;


         array  White_H2  (II)  WH2_1-WH2_10;
         array  Black_H2  (II)  BH2_1-BH2_10;
         array  Hspnc_H2  (II)  HH2_1-HH2_10;
         array  Asian_H2  (II)  AH2_1-AH2_10;

         array  Ages      (II)  A1-A10;
         array  AgesP5    (II)  AP1-AP10;

         do II = 1 to 10;
           AgesP5 = Ages + 5;
         end;

         if (_N_ eq 1) then do;
            put " ";
            put "              Rectal Ca     Proximal Ca       Distal Ca";
            put " 5 year   composite h1*   composite h1*   composite h1*   cmptng rsk h2";
            put " AgeGrp  SEER18 2000-15  SEER18 2000-15  SEER18 2000-15   US    1992-02";
            put " ";

            put "                                       White Men";

            do II = 1 to 10;
               put "[" Ages 2.0 ":"  AgesP5 2.0 ")"
                   WhiteR_H1  16.6
                   WhiteP_H1  16.6
                   WhiteD_H1  16.6
                   White_H2   16.6;
            end;
            put " ";


            put "                                       Black Men";

            do II = 1 to 10;
               put "[" Ages 2.0 ":"  AgesP5 2.0 ")"
                   BlackR_H1  16.6
                   BlackP_H1  16.6
                   BlackD_H1  16.6
                   Black_H2   16.6;
            end;
            put " ";


            put "                                      Hispanic Men";

            do II = 1 to 10;
               put "[" Ages 2.0 ":"  AgesP5 2.0 ")"
                   HspncR_H1  16.6
                   HspncP_H1  16.6
                   HspncD_H1  16.6
                   Hspnc_H2   16.6;
            end;
            put " ";


            put "                                       Asian Men";

            do II = 1 to 10;
               put "[" Ages 2.0 ":"  AgesP5 2.0 ")"
                   AsianR_H1  16.6
                   AsianP_H1  16.6
                   AsianD_H1  16.6
                   Asian_H2   16.6;
            end;
            put " ";

            put " ";
            put " ";
            put " ";
            put " ";
            put " ";
            put " ";
            put " ";
            put " ";
            put " ";
            put " ";
            put " ";
         end;


         array  Rectl_Beta (RR)   posi0R_Beta    posi1R_Beta    posi2R_Beta    noIBuprofnBeta
                                  NRelGE1_Beta   XrcisHrs_Beta;

         array  Rectl_Name (RR) $ RBeta1-RBeta6  ('Posi0Ind', 'Posi1Ind', 'Posi2Ind', 'NoIBupro',
                                                  '#Rel >=1', 'XrcsTrnd');

         array  Prxml_Beta (PP)   CigYr0_Beta    CigYr2_Beta    CigYr3_Beta    VegLT5_Beta
                                  NumCigs_Beta   BMITrndPBeta   posi1P_Beta    posi2P_Beta
                                  posi3P_Beta    noNSaidPBeta   RelTrnPBeta;

         array  Prxml_Name (PP) $ PBeta1-PBeta11 ('Cig Yr 0', 'Cig Yr 2', 'Cig Yr 3', 'Veg<5Ind',
                                                  'Num Cigs', 'BMI Trnd', 'Posi1Ind', 'Posi2Ind',
                                                  'Posi3Ind', 'noNSAIDs', 'FamHx012');

         array  Distl_Beta (DD)   BMITrndDBeta   posi1D_Beta    posi2D_Beta    posi3D_Beta
                                  noNSaidDBeta   RelTrnDBeta;

         array  Distl_Name (DD) $ DBeta1-DBeta6  ('BMI Trnd', 'Posi1Ind', 'Posi2Ind', 'Posi3Ind',
                                                  'noNSAIDs', 'FamHx012');



         if (_N_ eq 1) then do;
            put " ";
            put "Ln Relative Risk from logistic regressions";
            put " ";
            put "Rel Risk         Rectal      Proximal        Distal";
            put "Covarate           lnRR          lnRR          lnRR";
            put " ";

            do XX = 1 to 17;

               if      (XX le 6) then do;
                  RR = XX;

                  put  Rectl_Name  $char9.
                       Rectl_Beta   14.7;
               end;
               else if (XX gt 6 and XX le 11) then do;
                  PP = XX - 6;

                  if (XX eq 7) then put  " ";

                  put  Prxml_Name  $char9.
                       "              "
                       Prxml_Beta   14.7;
               end;
               else if (XX gt 11) then do;
                  PP = XX -  6;
                  DD = XX - 11;

                  if (XX eq 12) then put  " ";

                  put  Prxml_Name  $char9.
                       "              "
                       Prxml_Beta   14.7
                       Distl_Beta   14.7;
               end;
            end;
            put " ";
         end;



         if (_N_ eq 1) then do;
            put " ";
            put "1-Attributable Risk:  F(t)";
            put " ";
            put "                              1-AR";
            put " ";

            put "Rectal Cancer       "  Rectal_1_AR  14.7;
            put "Proxml Cancer       "  Proxml_1_AR  14.7;
            put "Distal Ca           "  Distal_1_AR  14.7;
         end;

   title5  "Listing of All constants required for CRC absolute risk projections";
   run;


   *** for each man in input file, make race specific baseline cancer incidences
       h1_R, h1_P, h1_D  and competing hazard h2         all are arrays of 50 deep;

   data  TmpFile;
   set   TmpFile;

         array  WhiteR_H1 (II)  WRH1_1-WRH1_10;
         array  BlackR_H1 (II)  BRH1_1-BRH1_10;
         array  HspncR_H1 (II)  HRH1_1-HRH1_10;
         array  AsianR_H1 (II)  ARH1_1-ARH1_10;

         array  WhiteP_H1 (II)  WPH1_1-WPH1_10;
         array  BlackP_H1 (II)  BPH1_1-BPH1_10;
         array  HspncP_H1 (II)  HPH1_1-HPH1_10;
         array  AsianP_H1 (II)  APH1_1-APH1_10;

         array  WhiteD_H1 (II)  WDH1_1-WDH1_10;
         array  BlackD_H1 (II)  BDH1_1-BDH1_10;
         array  HspncD_H1 (II)  HDH1_1-HDH1_10;
         array  AsianD_H1 (II)  ADH1_1-ADH1_10;


         array  White_H2  (II)  WH2_1-WH2_10;
         array  Black_H2  (II)  BH2_1-BH2_10;
         array  Hspnc_H2  (II)  HH2_1-HH2_10;
         array  Asian_H2  (II)  AH2_1-AH2_10;

         array  Ages      (II)    A1-A10;
         array  AgesP5    (II)    AP1-AP10;

         do II = 1 to 10;
           AgesP5 = Ages + 5;
         end;


         ***  h1_R,  h1_P and  h1_D  are baseline cancer incidences associated with
              race of woman for this record  i.e. h1=(1-ar)*h1star

              h2 is competing hazrd associated with race of woman for this record;

         array  h1_R (j_intvl) h1R01_1-h1R01_5  h1R02_1-h1R02_5  h1R03_1-h1R03_5
                               h1R04_1-h1R04_5  h1R05_1-h1R05_5  h1R06_1-h1R06_5
                               h1R07_1-h1R07_5  h1R08_1-h1R08_5  h1R09_1-h1R09_5  h1R10_1-h1R10_5;  *** rectal baseline h1;

         array  h1_P (j_intvl) h1P01_1-h1P01_5  h1P02_1-h1P02_5  h1P03_1-h1P03_5
                               h1P04_1-h1P04_5  h1P05_1-h1P05_5  h1P06_1-h1P06_5
                               h1P07_1-h1P07_5  h1P08_1-h1P08_5  h1P09_1-h1P09_5  h1P10_1-h1P10_5;  *** prximl baseline h1;

         array  h1_D (j_intvl) h1D01_1-h1D01_5  h1D02_1-h1D02_5  h1D03_1-h1D03_5
                               h1D04_1-h1D04_5  h1D05_1-h1D05_5  h1D06_1-h1D06_5
                               h1D07_1-h1D07_5  h1D08_1-h1D08_5  h1D09_1-h1D09_5  h1D10_1-h1D10_5;  *** distal baseline h1;

         array  h2   (j_intvl) h2_01_1-h2_01_5  h2_02_1-h2_02_5  h2_03_1-h2_03_5
                               h2_04_1-h2_04_5  h2_05_1-h2_05_5  h2_06_1-h2_06_5
                               h2_07_1-h2_07_5  h2_08_1-h2_08_5  h2_09_1-h2_09_5  h2_10_1-h2_10_5;


         if      (Err_Race eq 1) then do;

            *** white men rectal;

            j_intvl = 0;

            do II = 1 to 10;
               tmp1 = Rectal_1_AR * WhiteR_H1;       *** rect incidence for the 5yr age cat;
               tmp2 =               White_H2;        *** competing hazs for the 5yr age cat;

               do I5 = 1 to 5;     *** fill in each yr of the 5yr cat with the correct rate;

                  j_intvl = j_intvl + 1;

                  h1_R    = tmp1;
                  h2      = tmp2;
               end;
            end;

            *** white men proximal;

            j_intvl = 0;

            do II = 1 to 10;
               tmp1 = Proxml_1_AR * WhiteP_H1;       *** prxm incidence for the 5yr age cat;

               do I5 = 1 to 5;     *** fill in each yr of the 5yr cat with the correct rate;

                  j_intvl = j_intvl + 1;

                  h1_P    = tmp1;
               end;
            end;

            *** white men distal;

            j_intvl = 0;

            do II = 1 to 10;
               tmp1 = Distal_1_AR * WhiteD_H1;       *** dist incidence for the 5yr age cat;

               do I5 = 1 to 5;     *** fill in each yr of the 5yr cat with the correct rate;

                  j_intvl = j_intvl + 1;

                  h1_D    = tmp1;
               end;
            end;
         end;
         else if (Err_Race eq 2) then do;

            *** black men rectal;

            j_intvl = 0;

            do II = 1 to 10;
               tmp1 = Rectal_1_AR * BlackR_H1;       *** rect incidence for the 5yr age cat;
               tmp2 =               Black_H2;        *** competing hazs for the 5yr age cat;

               do I5 = 1 to 5;     *** fill in each yr of the 5yr cat with the correct rate;

                  j_intvl = j_intvl + 1;

                  h1_R    = tmp1;
                  h2      = tmp2;
               end;
            end;

            *** black men proximal;

            j_intvl = 0;

            do II = 1 to 10;
               tmp1 = Proxml_1_AR * BlackP_H1;       *** prxm incidence for the 5yr age cat;

               do I5 = 1 to 5;     *** fill in each yr of the 5yr cat with the correct rate;

                  j_intvl = j_intvl + 1;

                  h1_P    = tmp1;
               end;
            end;

            *** black men distal;

            j_intvl = 0;

            do II = 1 to 10;
               tmp1 = Distal_1_AR * BlackD_H1;       *** dist incidence for the 5yr age cat;

               do I5 = 1 to 5;     *** fill in each yr of the 5yr cat with the correct rate;

                  j_intvl = j_intvl + 1;

                  h1_D    = tmp1;
               end;
            end;
         end;
         else if (Err_Race eq 3) then do;

            *** hispanic men rectal;

            j_intvl = 0;

            do II = 1 to 10;
               tmp1 = Rectal_1_AR * HspncR_H1;       *** rect incidence for the 5yr age cat;
               tmp2 =               Hspnc_H2;        *** competing hazs for the 5yr age cat;

               do I5 = 1 to 5;     *** fill in each yr of the 5yr cat with the correct rate;

                  j_intvl = j_intvl + 1;

                  h1_R    = tmp1;
                  h2      = tmp2;
               end;
            end;

            *** hispanic men proximal;

            j_intvl = 0;

            do II = 1 to 10;
               tmp1 = Proxml_1_AR * HspncP_H1;       *** prxm incidence for the 5yr age cat;

               do I5 = 1 to 5;     *** fill in each yr of the 5yr cat with the correct rate;

                  j_intvl = j_intvl + 1;

                  h1_P    = tmp1;
               end;
            end;

            ** hispanic men distal;

            j_intvl = 0;

            do II = 1 to 10;
               tmp1 = Distal_1_AR * HspncD_H1;       *** dist incidence for the 5yr age cat;

               do I5 = 1 to 5;     *** fill in each yr of the 5yr cat with the correct rate;

                  j_intvl = j_intvl + 1;

                  h1_D    = tmp1;
               end;
            end;
         end;
         else if (Err_Race eq 4) then do;

            *** asian men rectal;

            j_intvl = 0;

            do II = 1 to 10;
               tmp1 = Rectal_1_AR * AsianR_H1;       *** rect incidence for the 5yr age cat;
               tmp2 =               Asian_H2;        *** competing hazs for the 5yr age cat;

               do I5 = 1 to 5;     *** fill in each yr of the 5yr cat with the correct rate;

                  j_intvl = j_intvl + 1;

                  h1_R    = tmp1;
                  h2      = tmp2;
               end;
            end;

            *** asian men proximal;

            j_intvl = 0;

            do II = 1 to 10;
               tmp1 = Proxml_1_AR * AsianP_H1;       *** prxm incidence for the 5yr age cat;

               do I5 = 1 to 5;     *** fill in each yr of the 5yr cat with the correct rate;

                  j_intvl = j_intvl + 1;

                  h1_P    = tmp1;
               end;
            end;

            *** asian men distal;

            j_intvl = 0;

            do II = 1 to 10;
               tmp1 = Distal_1_AR * AsianD_H1;       *** dist incidence for the 5yr age cat;

               do I5 = 1 to 5;     *** fill in each yr of the 5yr cat with the correct rate;

                  j_intvl = j_intvl + 1;

                  h1_D    = tmp1;
               end;
            end;
         end;
   run;


   *** BrCa risk projection;

   data  TmpFile;
   set   TmpFile;

         *** note that all h1 (cancer incidences) are baseline i.e. h1=(1-AR)*h1Star;

         array  h1_R (j_intvl) h1R01_1-h1R01_5  h1R02_1-h1R02_5  h1R03_1-h1R03_5
                               h1R04_1-h1R04_5  h1R05_1-h1R05_5  h1R06_1-h1R06_5
                               h1R07_1-h1R07_5  h1R08_1-h1R08_5  h1R09_1-h1R09_5 h1R10_1-h1R10_5;  *** rectal baseline h1;

         array  h1_P (j_intvl) h1P01_1-h1P01_5  h1P02_1-h1P02_5  h1P03_1-h1P03_5
                               h1P04_1-h1P04_5  h1P05_1-h1P05_5  h1P06_1-h1P06_5
                               h1P07_1-h1P07_5  h1P08_1-h1P08_5  h1P09_1-h1P09_5 h1P10_1-h1P10_5;  *** prximl baseline h1;

         array  h1_D (j_intvl) h1D01_1-h1D01_5  h1D02_1-h1D02_5  h1D03_1-h1D03_5
                               h1D04_1-h1D04_5  h1D05_1-h1D05_5  h1D06_1-h1D06_5
                               h1D07_1-h1D07_5  h1D08_1-h1D08_5  h1D09_1-h1D09_5 h1D10_1-h1D10_5;  *** distal baseline h1;

         array  h2   (j_intvl) h2_01_1-h2_01_5  h2_02_1-h2_02_5  h2_03_1-h2_03_5
                               h2_04_1-h2_04_5  h2_05_1-h2_05_5  h2_06_1-h2_06_5
                               h2_07_1-h2_07_5  h2_08_1-h2_08_5  h2_09_1-h2_09_5 h2_10_1-h2_10_5;  *** competing hazards;

         array  Rectl_Beta (RR)   posi0R_Beta    posi1R_Beta    posi2R_Beta    noIBuprofnBeta
                                  NRelGE1_Beta   XrcisHrs_Beta;

         array  Rectl_Name (RR) $ RBeta1-RBeta6  ('Posi0Ind', 'Posi1Ind', 'Posi2Ind', 'NoIBupro',
                                                  '#Rel >=1', 'XrcsTrnd');

         array  Prxml_Beta (PP)   CigYr0_Beta    CigYr2_Beta    CigYr3_Beta    VegLT5_Beta
                                  NumCigs_Beta   BMITrndPBeta   posi1P_Beta    posi2P_Beta
                                  posi3P_Beta    noNSaidPBeta   RelTrnPBeta;

         array  Prxml_Name (PP) $ PBeta1-PBeta11 ('Cig Yr 0', 'Cig Yr 2', 'Cig Yr 3', 'Veg<5Ind',
                                                  'Num Cigs', 'BMI Trnd', 'Posi1Ind', 'Posi2Ind',
                                                  'Posi3Ind', 'noNSAIDs', 'FamHx012');

         array  Distl_Beta (DD)   BMITrndDBeta   posi1D_Beta    posi2D_Beta    posi3D_Beta
                                  noNSaidDBeta   RelTrnDBeta;

         array  Distl_Name (DD) $ DBeta1-DBeta6  ('BMI Trnd', 'Posi1Ind', 'Posi2Ind', 'Posi3Ind',
                                                  'noNSAIDs', 'FamHx012');


         *** linear predictor men of interest for rectal cancer;

         LP_R = posi_0Ind    * posi0R_Beta     +
                posi_1Ind    * posi1R_Beta     +
                posi_2Ind    * posi2R_Beta     +
                No_IBuprofen * noIBuprofnBeta  +
                TotlRelge1   * NRelGE1_Beta    +
                XrcisHrsTrnd * XrcisHrs_Beta;

         *** linear predictor men of interest for proximal cancer;

         LP_P = CigYr_0      * CigYr0_Beta  +
                CigYr_2      * CigYr2_Beta  +
                CigYr_3      * CigYr3_Beta  +
                Veg_lt5      * VegLT5_Beta  +
                Num_Cigarets * NumCigs_Beta +

                BMITrnd      * BMITrndPBeta +
                posi_1Ind    * posi1P_Beta  +
                posi_2Ind    * posi2P_Beta  +
                posi_3Ind    * posi3P_Beta  +
                NoNSADS      * noNSaidPBeta +
                NumRelTrnd   * RelTrnPBeta;


         *** linear predictor men of interest for distal cancer;

         LP_D = BMITrnd      * BMITrndDBeta +
                posi_1Ind    * posi1D_Beta  +
                posi_2Ind    * posi2D_Beta  +
                posi_3Ind    * posi3D_Beta  +
                NoNSADS      * noNSaidDBeta +
                NumRelTrnd   * RelTrnDBeta;

         *** double iterated integral ala  eq 2.3  Benichou & Gail Biometrics 46,813-826;

         RRStar_R   = exp(LP_R);               *** RR man of interest          rectal  cancer;
         RRStar_P   = exp(LP_P);               *** RR man of interest          proximl cancer;
         RRStar_D   = exp(LP_D);               *** RR man of interest          distal  cancer;

         Strt_Intvl = floor(&T1) - 40 + 1;     *** example:  T1=51 T2=53, Strt_Intvl=51-40+1= 12;
         Endd_Intvl =  ceil(&T2) - 40 + 0;     ***                        EndD_Intvl=53-40+0= 13;
         NumbrIntvl =  ceil(&T2) - floor(&T1); ***                        NumbrIntvl=53-51  = 2;

         if      (Error_Ind    eq     1  or
                 &PattrnNumber lt     1  or
                 &PattrnNumber gt 18432) then do;
           &AbsRsk       = .;                  *** erroneous input record set absrsk  to missng;
           &PattrnNumber =-9;                  ***                        set pattrn# to missng;
         end;

         else if (Error_Ind eq 0) then do;                   *** error free input record;
           &AbsRsk = 0;                                      *** calculate abs risk;
            Cum_H  = 0;

            do jj = 1 to NumbrIntvl;
               j_intvl = Strt_Intvl + (jj - 1);

               if      (NumbrIntvl gt 1 and jj gt          1
                                        and jj lt NumbrIntvl) then IntgrlLngth = 1;
               else if (NumbrIntvl gt 1 and jj eq          1) then IntgrlLngth = 1 - (&T1 - floor(&T1));
               else if (NumbrIntvl gt 1 and jj eq NumbrIntvl) then IntgrlLngth =     (&T2 - floor(&T2))*(&T2 gt floor(&T2)) +
                                                                                                        (&T2 eq floor(&T2));
               else if (NumbrIntvl eq 1                     ) then IntgrlLngth =      &T2 - &T1;

               Hj = (h1_R*RRStar_R + h1_P*RRStar_P + h1_D*RRStar_D) + h2;

               PI_j   = (( (h1_R*RRStar_R + h1_P*RRStar_P + h1_D*RRStar_D) /Hj)*exp(-Cum_H)) * (1-exp(-Hj*IntgrlLngth));

              &AbsRsk = &AbsRsk + PI_j;
               Cum_H  = Cum_H  + Hj*IntgrlLngth;
            end;
         end;


         &AbsRsk = 100*&AbsRsk;


         label   LP_R         =  "Linear Prdctr for Rectal   cancer"
                 LP_P         =  "Linear Prdctr for Proximal cancer"
                 LP_D         =  "Linear Prdctr for Distal   cancer"

                 RRStar_R     =  "Relative Risk for Rectal   cancer"
                 RRStar_P     =  "Relative Risk for Proximal cancer"
                 RRStar_D     =  "Relative Risk for Distal   cancer"

                &PattrnNumber =  "Relative risk covariate pattern #"
                &AbsRsk       =  "Abs risk (%) of CRC in age interval [T1,T2)";

   run;


   *** checking summary statistics which indicates error in input file &SAS_File;

   proc  means  data=TmpFile  mean std n nmiss  maxdec=4;
   var   Error_Ind
        &AbsRsk;

   title5  "Quick check for errornous records on input file";
   title6  " ";
   title7  "IF MEAN OF  'Error_Ind'   EQUALS  0,   ERROR  FREE.    NO ERROR LISTING.";
   title8  "IF MEAN OF  'Error_Ind'   IS NOT  0,   ERRORS EXISTS.  CHECK ERROR LISTING.";
   title9  " ";
   title10 "(# of records with errors is the # listed under the NMiss column in the "
           "line for 'Absolute_Risk')";


   *** proc  contents  data=TmpFile  position;
   *** run;


   *** deleting temporary variables created and used in the sas macro from the sas file &SAS_File;
   data &SAS_File  (keep = ID_Num  --  NoNSaids
                          &PattrnNumber
                          &AbsRsk);

***data &SAS_File  (keep = ID_Num        --  SEQ_Num
                           Posi_0Ind     --  NumRelTrnd
                           Posi0R_Beta   --  XrcisHrs_Beta
                           Cigyr0_Beta   --  RelTrnPBeta
                           BMITrndDBeta  --  RelTrnDBeta
                           Rectal_1_AR   --  Distal_1_AR
                           LP_R          --  RRStar_D
                          &PattrnNumber
                          &AbsRsk);

   set   TmpFile;
   run;
%mend;

