/****************************************************************************************
Version 3.0  Women_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                                                                      06/09/15
Version 1.0                                                                      05/14/10

Women_CRC_RAM: Women (C)ol(R)ectal (C)ancer (R)isk (A)ssessment (M)acro

SAS macro to project Colorectal Cancer abs risk for women 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= 100  ps=60  notes;

/*
        Women_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 need to point;
***                        parameters          to an appropriate sas file name;
***                                            or sas variable name;

%macro  Women_CRC_RAM     (SAS_File           =,

                           Race               =,
                           T1                 =,
                           T2                 =,

                           Sigmoid_Polyps     =,
                           Estrogen           =,
                           Fam_Hist_CRC       =,

                           NSaids             =,
                           Vigorus_Xrcis      =,
                           BMI                =,

                           Vegetable          =,

                           AbsRsk             =);

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

       SAS_File            points to a sas system file which contains rr covariates
                           for women under investiation i.e. women 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= 1  white
                           2= 2  african-american
                           3= 3  hispanic
                           4= 4  asian-american

       T1                  T1,   current age

       T2                  T2,   projection age

                                 constraints on ages:  40 <= T1 < T2 <= 85      (01/18/2019 email)

       Sigmoid_Polyps      0= 0  yes sigmoidoscopy and/or colonscopy last 10 years,  no history of polyps
                           1= 1   no sigmoidoscopy and/or colonscopy last 10 years
                           2= 2  yes sigmoidoscopy and/or colonscopy last 10 years, yes history of polyps
                           3= 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


       Estrogen            0= 0  yes estrogen use last 2 years              or
                             -9  still have period or period < 2 yrs ago        (10/16/2014 meeting)

                           1= 1  no estrogen use last 2 years


       Fam_Hist_CRC        0= 0  relatives  with CRC                        or
                             -9  dont know if any relatives has CRC             (10/16/2014 meeting)

                           1= 1  1 relative  with CRC                       or
                             -8  has relatives with CRC, dont know how many     (10/16/2014 metting)

                           2= 2  2 or more relatives with CRC


       NSaids              0= 0  regular user of aspirin and or NSaids      or
                             -9  dont know                                       (10/16/2014 meeting)

                           1= 1  non     user of aspirin and or NSaids


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

       BMI                 0= 0  BMI less    <  30
                           1= 1  BMI greater >= 30

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

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



       Note: For   NSaids,        dont know                                   =-9
                   Estrogen,      still have period  or  period < 2 yrs ago   =-9

                   Fam_Hist_CRC   dont know if any relatives has CRC          =-9
                   -9 is mapped to lowest applicable RR level of 0            (10/16,24/2014  meeting with R Pfeiffer)

                   Fam_Hist_CRC   has relatives with CRC,  dont know how many =-8
                   -8 is mapped to lowest applicable RR level of 1 relative.  (10/16,24/2014  meeting with R Pfeiffer)
*/


   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;

         SEQ_Num = _n_;

         title3 'Women_CRC_RAM  sas macro for Women 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  ---->";


         if (&T1 eq &T2) then &T1=&T1-1;                *** for example t1=57.5 t2=57.5;


         *** 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;     *** women must be ages ge 40 and le 85 01/28/2019; 
            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_Strogn = "Set to missing value . when Estorgen Use has error"
                Err_FamHx  = "Set to missing value . when Family History CRC has error"

                Err_NSad   = "Set to missing value . when NSaids use has error"
                Err_Xrcis  = "Set to missing value . when Vigorous Exercise has error"
                Err_BMI    = "Set to missing value . when Body Mass Index has error"

                Err_Veg    = "Set to missing value . when Vegetable Intake 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      (&Estrogen        eq  0  or
                  &Estrogen        eq  1) then Err_Strogn  = &Estrogen;
         else if (&Estrogen        eq -9) then Err_Strogn  =         0;      *** (10/16/2014 meeting);
         else                                  Err_Strogn  =         .;

         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;  *** (10/16/2014 meeting);
         else if (&Fam_Hist_CRC    eq -8) then Err_FamHx   =             1;  *** (10/16/2014 meeting);
         else                                  Err_FamHx   =             .;

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

         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     (&BMI             eq  0  or
                 &BMI             eq  1) then  Err_BMI     = &BMI;
         else                                  Err_BMI     =    .;

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


         if     (Err_Race        eq  .  or

                 Err_Sig_Pol     eq  .  or
                 Err_Strogn      eq  .  or
                 Err_FamHx       eq  .  or

                 Err_NSad        eq  .  or
                 Err_Xrcis       eq  .  or
                 Err_BMI         eq  .  or

                 Err_Veg         eq  .) then  Error_Ind   = 1;


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

             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_Strogn = Err_Strogn;


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


         NoNSAIDs  = Err_NSad;


         vigl_0Ind = 0;
         vigl_1Ind = 0;
         vigl_2Ind = 0;
         vigl_3Ind = 0;

         if      (Err_Xrcis eq 0) then  vigl_0Ind = 1;
         else if (Err_Xrcis eq 1) then  vigl_1Ind = 1;
         else if (Err_Xrcis eq 2) then  vigl_2Ind = 1;
         else if (Err_Xrcis eq 3) then  vigl_3Ind = 1;
         else if (Err_Xrcis eq .) then do;
                                        vigl_0Ind = .;
                                        vigl_1Ind = .;
                                        vigl_2Ind = .;
                                        vigl_3Ind = .;
         end;


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


         BMI_ge30     = Err_BMI;


         Veg_lt5      = Err_Veg;


         NumRelTrnd   = Err_FamHx;


         BMI_Strogn   = BMI_ge30*(1-No_Strogn);



         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      No  FamHst  NoUse  Vigor";
               put "   SEQ          Currn  Prjcn  Colysc  Strogn  0,1,2+  Asprn  Xrcis   BMI  Vgtble";
               put "     #    Race    Age    Age  Polyps last2yr  Reltvs  NSaid   0123  >=30   <5/wk";
               put " ";
            end;

            put
               SEQ_Num                  6.0

               &Race                    8.0
               &T1                      7.1
               &T2                      7.1

               &Sigmoid_Polyps          8.0
               &Estrogen                8.0
               &Fam_Hist_CRC            8.0

               &NSaids                  7.0
               &Vigorus_Xrcis           7.0
               &BMI                     6.0

               &Vegetable               8.0;


            put
               Err_Race                14.0
               set_T1_Missing           7.1
               set_T2_Missing           7.1

               Err_Sig_Pol              8.0
               Err_Strogn               8.0
               Err_FamHx                8.0

               Err_NSad                 7.0
               Err_Xrcis                7.0
               Err_BMI                  6.0

               Err_Veg                  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);

         *** women rectal cancer composite incidences;

         array  WhiteR_H1 (II)  WRH1_1-WRH1_10
        (0.000071019,0.000120561,0.000201265,0.000190476,0.000223127,0.000270298,
         0.000301295,0.000365468,0.000407899,0.000439046);     *** SEER18 incidence 2000-2015;

         array  BlackR_H1 (II)  BRH1_1-BRH1_10
        (0.00006072,0.000108651,0.000255288,0.000241062,0.000246203,0.000349144,
         0.000333772,0.000377542,0.000454859,0.00039187);                      

         array  HspncR_H1 (II)  HRH1_1-HRH1_10
        (0.000047705,0.000082482,0.000164631,0.00019936,0.000223059,0.000275521,
         0.000255463,0.000311729,0.000375954,0.000388896);                       

         array  AsianR_H1 (II)  ARH1_1-ARH1_10
        (0.00005416,0.000103437,0.000193699,0.000188894,0.000224919,0.000296103,
         0.00029365,0.000340634,0.000437537,0.000408177);                       


         *** women proximal cancer composite incidences;

         array  WhiteP_H1 (II)  WPH1_1-WPH1_10
        (0.00005168,0.000084588,0.000140409,0.000189659,0.000300741,0.000473394,
         0.000732341,0.00106824,0.001412928,0.001533784);        *** SEER18 incidence 2000-2015;

         array  BlackP_H1 (II)  BPH1_1-BPH1_10
        (0.000082017,0.000151052,0.000251306,0.000324584,0.000525027,0.000704279,
         0.000977218,0.001168896,0.001277825,0.001348032);                    

         array  HspncP_H1 (II)  HPH1_1-HPH1_10
        (0.000033674,0.000066388,0.00012389,0.000176053,0.000268004,0.000423231,
         0.000554567,0.000741255,0.001078394,0.001048329);                    

         array  AsianP_H1 (II)  APH1_1-APH1_10
        (0.000028163,0.000053616,0.000100086,0.000124659,0.000187112,0.000319276,
         0.000435739,0.000562046,0.000842051,0.00099709);                    


         *** women distal cancer composite incidences;

         array  WhiteD_H1 (II)  WDH1_1-WDH1_10
        (0.000057908,0.000094684,0.000143584,0.000145466,0.000163728,0.000224999,
         0.000291184,0.000355425,0.000442591,0.000430304);       *** SEER18 incidence 2000-2015;

         array  BlackD_H1 (II)  BDH1_1-BDH1_10
        (0.000060266,0.000109976,0.000189807,0.000216556,0.000256666,0.000346577,
         0.00039161,0.00050284,0.000468927,0.000503832);                      

         array  HspncD_H1 (II)  HDH1_1-HDH1_10
        (0.000040562,0.000069836,0.000147266,0.000153994,0.000196425,0.000221948,
         0.000254398,0.00030311,0.000330444,0.000367761);                       

         array  AsianD_H1 (II)  ADH1_1-ADH1_10
        (0.000052861,0.00008683,0.000172785,0.000157321,0.000205054,0.000276363,
         0.000341013,0.000379342,0.000456111,0.000503622);                       

         II  = .;
         Key = 1;
   run;


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

         array  White_H2 (II)  WH2_1-WH2_10
        (0.001592785,0.002455866,0.003675325,0.005172382,0.007422948,0.01151871,
         0.018519935,0.030772108,0.052985389,0.133512135);           *** new 2010-2015;

         array  Black_H2 (II)  BH2_1-BH2_10
        (0.002369189,0.003658655,0.005618185,0.008157399,0.011336293,0.015888539,
         0.023582038,0.035579973,0.056318842,0.120125854);                     

         array  Hspnc_H2 (II)  HH2_1-HH2_10
        (0.000935211,0.001489034,0.002339127,0.003524357,0.005507226,0.008407629,
         0.013350759,0.022836228,0.040383996,0.096770185);                    

         array  Asian_H2 (II)  AH2_1-AH2_10
        (0.000589401,0.000960654,0.001474481,0.002227591,0.003456956,0.00543539,
         0.00913529,0.016641935,0.031301192,0.081374093);                      

         II  = .;
         Key = 1;
   run;


   data  LN_RR;

         *** Women Rectal Model;

         array  Rectl_Beta (RR)   posi0R_Beta    posi1R_Beta    posi2R_Beta    noStroRBeta
                                  RelGE1_Beta    noNSADRBeta    vigl_0_Beta    vigl_1_Beta
                                  vigl_2_Beta    BMIge30RBeta
                                 (1.0026582386,  2.0981550009,  2.1632905529,  0.3993964523,
                                  0.4277258350,  0.3561877041,  0.4620742678,  0.0927765508,
                                  0.2250898517,  0.3388584757);

         array  Rectl_Name (RR) $ RBeta1-RBeta10 ('Posi0Ind', 'Posi1Ind', 'Posi2Ind', 'NoStrogn',
                                                  'Rel >= 1', 'noNSAIDs', ' Xrcis 0', ' Xrcis 2',
                                                  ' Xrics 4', 'BMI >=30');


         *** Women Proximal Model;

         array  Prxml_Beta (PP)   XrcsTrndBeta   VegLT5_Beta    noNSADPBeta    posi1P_Beta
                                  posi2P_Beta    posi3P_Beta    noStroPBeta    RelTrnPBeta
                                 (0.1458625273,  0.3270044567,  0.4661512378,  0.5995456257,
                                  0.9630810189,  0.6048391330,  0.3823321555,  0.4103522687);

         array  Prxml_Name (PP) $ PBeta1-PBeta8  ('XrcsTrnd', ' Veg < 5', 'noNSAIDs', 'Posi1Ind',
                                                  'Posi2Ind', 'Posi3Ind', 'NoStrogn', 'Fam Hist');



         *** Women Distal Model;

         array  Distl_Beta (DD)   noNSADDBeta    posi1D_Beta    posi2D_Beta    posi3D_Beta
                                  noStroDBeta    RelTrnDBeta    BMIge30DBeta   BMIStroBeta
                                 (0.3642435022,  1.2356446848,  1.4698425664,  1.1528664833,
                                  0.7421864593,  0.3693599570,  0.0771453279,  0.9873503366);

         array  Distl_Name (DD) $ DBeta1-DBeta8  ('noNSAIDs', 'Posi1Ind', 'Posi2Ind', 'Posi3Ind',
                                                  'NoStrogn', 'Fam Hist', 'BMI >=30', 'BMIStrog');

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

         Key = 1;
   run;


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

         Rectal_1_AR = 0.073455917024;
         Proxml_1_AR = 0.188610751158;
         Distl1_1_AR = 0.177983863475;                    *** age lt 65;
         Distl2_1_AR = 0.148075734041;                    *** age ge 65;

         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 Women";

            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 Women";

            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 Women";

            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 Women";

            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    noStroRBeta
                                  RelGE1_Beta    noNSADRBeta    vigl_0_Beta    vigl_1_Beta
                                  vigl_2_Beta    BMIge30RBeta;

         array  Rectl_Name (RR) $ RBeta1-RBeta10;   ***  ('Posi0Ind', 'Posi1Ind', 'Posi2Ind', 'NoStrogn',
                                                          'Rel >= 1', 'noNSAIDs', ' Xrcis 0', ' Xrcis 2',
                                                          ' Xrics 4', 'BMI >=30');

         array  Prxml_Beta (PP)   XrcsTrndBeta   VegLT5_Beta    noNSADPBeta    posi1P_Beta
                                  posi2P_Beta    posi3P_Beta    noStroPBeta    RelTrnPBeta;

         array  Prxml_Name (PP) $ PBeta1-PBeta8;    ***  ('XrcsTrnd', ' Veg < 5', 'noNSAIDs', 'Posi1Ind',
                                                          'Posi2Ind', 'Posi3Ind', 'NoStrogn', 'Fam Hist');

         array  Distl_Beta (DD)   noNSADDBeta    posi1D_Beta    posi2D_Beta    posi3D_Beta
                                  noStroDBeta    RelTrnDBeta    BMIge30DBeta   BMIStroBeta;

         array  Distl_Name (DD) $ DBeta1-DBeta8;    ***  ('noNSAIDs', 'Posi1Ind', 'Posi2Ind', 'Posi3Ind',
                                                          'NoStrogn', 'Fam Hist', 'BMI >=30', 'BMIStrog');


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

            do XX = 1 to 10;
               RR = XX;
               PP = XX;
               DD = XX;

               if      (XX le 8) then
                  put  Rectl_Name  $char9.
                       Rectl_Beta   14.7
                       "      "
                       Prxml_Name  $char9.
                       Prxml_Beta   14.7
                       "      "
                       Distl_Name  $char9.
                       Distl_Beta   14.7;
               else if (XX gt 8) then
                  put  Rectl_Name  $char9.
                       Rectl_Beta   14.7;

            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    age <65"  Distl1_1_AR  14.7;
            put "Distal Ca    age>=65"  Distl2_1_AR  14.7;
         end;

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



   *** for each women 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 women 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 women 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 women distal;

            j_intvl = 0;

            do II = 1 to 10;
               tmp1 =(Distl1_1_AR*(II le 5) + Distl2_1_AR*(II ge 6)) *
                      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 women 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 women 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 women distal;

            j_intvl = 0;

            do II = 1 to 10;
               tmp1 =(Distl1_1_AR*(II le 5) + Distl2_1_AR*(II ge 6)) *
                      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 women 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 women 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 women distal;

            j_intvl = 0;

            do II = 1 to 10;
               tmp1 =(Distl1_1_AR*(II le 5) + Distl2_1_AR*(II ge 6)) *
                      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 women 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 women 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 women distal;

            j_intvl = 0;

            do II = 1 to 10;
               tmp1 =(Distl1_1_AR*(II le 5) + Distl2_1_AR*(II ge 6)) *
                      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    noStroRBeta
                               RelGE1_Beta    noNSADRBeta    vigl_0_Beta    vigl_1_Beta
                               vigl_2_Beta    BMIge30RBeta;

         array  Prxml_Beta(PP) XrcsTrndBeta   VegLT5_Beta    noNSADPBeta    posi1P_Beta
                               posi2P_Beta    posi3P_Beta    noStroPBeta    RelTrnPBeta;

         array  Distl_Beta(DD) noNSADDBeta    posi1D_Beta    posi2D_Beta    posi3D_Beta
                               noStroDBeta    RelTrnDBeta    BMIge30DBeta   BMIStroBeta;

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

         LP_R = posi_0Ind    * posi0R_Beta  +
                posi_1Ind    * posi1R_Beta  +
                posi_2Ind    * posi2R_Beta  +
                No_Strogn    * noStroRBeta  +
                TotlRelge1   * RelGE1_Beta  +
                NoNSAIDs     * noNSADRBeta  +
                vigl_0Ind    * vigl_0_Beta  +
                vigl_1Ind    * vigl_1_Beta  +
                vigl_2Ind    * vigl_2_Beta  +
                BMI_ge30     * BMIge30RBeta;

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

         LP_P = XrcisHrsTrnd * XrcsTrndBeta +
                Veg_lt5      * VegLT5_Beta  +
                NoNSAIDs     * noNSADPBeta  +
                posi_1Ind    * posi1P_Beta  +
                posi_2Ind    * posi2P_Beta  +
                posi_3Ind    * posi3P_Beta  +
                No_Strogn    * noStroPBeta  +
                NumRelTrnd   * RelTrnPBeta;

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

         LP_D = NoNSAIDs     * noNSADDBeta  +
                posi_1Ind    * posi1D_Beta  +
                posi_2Ind    * posi2D_Beta  +
                posi_3Ind    * posi3D_Beta  +
                No_Strogn    * noStroDBeta  +
                NumRelTrnd   * RelTrnDBeta  +
                BMI_ge30     * BMIge30DBeta +
                BMI_Strogn   * BMIStroBeta;


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

         RRStar_R   = exp(LP_R);               *** RR woman of interest          rectal  cancer;
         RRStar_P   = exp(LP_P);               *** RR woman of interest          proximl cancer;
         RRStar_D   = exp(LP_D);               *** RR woman 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) then
           &AbsRsk = .;                                      *** erroneous input record
                                                                 set abs risk to missing;

         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"

                &AbsRsk      =  "Abs risk (%) of CRC in age interval [T1,T2)";

            *** PattrnNumber =  "Relative risk covariate pattern #";
   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')";


   *** deleting temporary variables created and used in the sas macro from the sas file &SAS_File;

   data &SAS_File  (drop = Set_T1_Missing -- Err_Veg
                           II             --  RR
                           RBeta1         --  PP
                           PBeta1         --  DD
                           DBeta1         --  DBeta8
                           AP1            --  I5
                           Strt_Intvl     --  NumbrIntvl
                           Cum_H          --  PI_j);
   set   TmpFile;
   run;
%mend;

