/***********************************************************************************************
AARP_PLCO_EnCa_RAM_Model6: AARP PLCO (En)dometrial (Ca)ncer (R)isk (A)ssessment (M)acro (Model6)

Model 6  code is derived from RelRisk___Final\EndoCa\AARP_PLCO_BrCa_RAM_Model5_1  with inclusion
of African-American EnCa Incidences and Competing risks (e-mail R Pfeiffer 9/17/2013)  9/20/2013

SAS macro to project for EnCa abs risk for white women based on the relative risk model 5
from a joint AARP/PLCO data set  (SAS transport file riskmodel.pee.sep08.d021810.cpt)
using 4/13/2012 UterusNOS_Dave.xls H1star from R Pfeiffer
****************************************************************************************/

option   nocenter  ls=125  ps=80  notes;
OPTIONS  FORMCHAR="|----|+|---+=|-/\<>*";

/*
        AARP_PLCO_EnCa_RAM_model6    SAS macro which performs EnCa absolute risk projection
                                     from a rr model developed 12/28/2011

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

        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

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

        RR model for Model 3,   Model 4  are the same
        RR model for Model 5.x =Model 4 + Horm_ModelThin  x=0,1

        White:
           H1* and H2 rates used for Model 3 differs from Model 4
           (see  Untiled e-mail of 4/13/2012 from R Pfeiffer)
           H1* and H2 rates used for Model 4 and Model 5.0 are the same
           H1* and H2 rates used for Model 5.1 are given by e-mail of 12/08/2012

        African-American:
           H1* and H2 rates used for Model 6  (BLACKuterus.xls  e-mail R Pfeiffer 9/17/2013)
*/

***                                macro                       upon invocation all macro parameters need to point;
***                                parameters                  to appropriate sas file or sas variable names or constants;

%macro  AARP_PLCO_EnCa_RAM_model6   (SAS_File                  =,

                                     Woman_ID                  =,

                                     T1                        =,
                                     T2                        =,

                                     OC_Model                  =,

                                     AMeno                     =,
                                     PMeno                     =,

                                     Parity_Model              =,

                                     Cig_1                     =,
                                     Cig_3                     =,

                                     BMI_Model                 =,
                                     Horm_Model                =,

                                     HRT_Thin                  =,

                                     Race                      =,
                                     CharRace                  =,

                                     RelRsk                    =,
                                     AbsRsk                    =);

/*
       SAS_File                  points to a sas system file which contains rr covariates for women under
                                 investiation i.e. white women whom we wish to project absolute risk based
                                 on their relative risk covariate values, initial age and projection age.
                                 This file (i.e. SAS_File) must be created prior to involking this SAS macro

       Woman_ID                  Woman's unique ID

       T1                        points to SAS variable which contains woman's current age
                                 valid ages are   T1 >= 50

       T2                        points to SAS variable which contains women's projection age
                                 valid ages are   T1  < T2  <= 90


                                 ***  constraint on T1 (current age) and T2 (projection age)   ***
                                 ***                     !!!!!!!!!!!!!!!!!!!                   !!!
                                 !!!                     50 <= T1 < T2 <= 90                   !!!
                                 !!!                     !!!!!!!!!!!!!!!!!!!                   !!!


       OC_Model                  points to SAS variable which contains Oral Contraceptive Use
                                 OC_Model     0=  > 1
                                              1=  0,1

       AMeno                     points to SAS variable which contains age at menopause
                                 AMeno        0= <= 49   or  pre-menopausal
                                              1= 50:54
                                              2=    55+

       PMeno                     points to SAS variable which contains pre-menopausal indicator
                                 PMeno        0 = not pre-menopausal
                                              1 =     pre-menopausal

       Parity_Model             points to SAS variable which contains Parity at BFC
                                Parity_Model  0= ge 3
                                              1=  1:2
                                              2=    0

       Cig_1                     points to SAS variable which contains    Non Smoker Indicator
                                 Cig_1        0= not Non Smkr
                                              1=     Non Smkr

       Cig_3                     points to SAS variable which contains Former Smoker Indicator
                                 Cig_3        0= not Former Smoker
                                              1=     Former Smoker

       BMI_Model                 points to SAS variable which contains body mass index
                                 BMI_Model    0=    <25
                                              1= [25,30)
                                              2= [30,35)
                                              3= [35,40)
                                              4=   >=40

       Horm_Model                points to SAS variable which contains duration of HRT
                                 Horm_Model   0=    0y
                                              1=  <10y
                                              2= >=10y


       HRT_Thin                  points to SAS variable  duration of HRT*(BMI<25)  created by macro
                                 HRT_Thin     0=    0y     Horm_Model=0      and any value of BMI or
                                                           Horm_Model=1,2    and (BMI>=25)
                                              1=  <10y     Horm_Model=1      and (BMI< 25)
                                              2= >=10y     Horm_Model=2      and (BMI< 25)

       Race                      points to SAS variable which contains race of subject
                                 Race         1=White                  (SEER13 1992-2006 BrCa incidence)
                                              2=African-American       (BLACKuterus.xls  e-mail R Pfeiffer 9/17/2013)

                                              3=Hispanic                Future implementation. Invalid for this version
                                              4=Other (Natv Amrn, Unk)  Future implementation. Invalid for this version

                                              5=???                     Future implementation. Invalid for this version.

                                              6=Asian/Pacific Islander  Future implementation. Invalid for this version


       Previous listed variables are required inputs to               SAS Macro  AARP_PLCO_EnCa_RAM_Model6



       Following variables are created and returned by                SAS Macro  AARP_PLCO_BrCa_RAM_Model6

       CharRace                  points to SAS variable which contains race as a character variable for each subject
                                 CharRace     " White"
                                              "AfrAmr"

       RelRsk (%)                points to SAS variable  relative risk in %    created by macro

       AbsRsk (%)                points to SAS variable which contains the projected abs risk in %
                                 Absolute Risk is from Endometrial Cancer model 6  created by macro
*/


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


         title3 'AARP_PLCO_EnCa_RAM_model6  sas macro for EnCa absolute risk Model 6 based on PLCO/AARP EnCa RR Model 5';

                 SEQ_Num = _n_;

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


         ***  constrant on  T1 (current age)  and  T2 (projection age)
         !!!                                                           !!!
         !!!                  !!!!!!!!!!!!!!!!!!!                      !!!
         !!!                  50 <= T1 < T2 <= 90                      !!!
         !!!                  !!!!!!!!!!!!!!!!!!!                      !!!;


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

         if (&T1 lt 50) then do;                        *** woman must be age ge 50;
            set_T1_Missing = .;
            Error_Ind      = 1;
         end;

         if (&T2 gt 90) then do;                        *** projection age must ge le 90;
            set_T2_Missing = .;
            Error_Ind      = 1;
         end;

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


         *** error checking on race code;

         RacCat = 1;
         if (&Race ne 1 and  &Race ne 2) then  RacCat = .; *** Race=5  White other rates left off deliberatly;


         *** making 6 character labels for race code;

                                   CharRace = "Invald";
         if     (&Race eq  1) then CharRace = " White";    *** white SEER13 1992-2006 BrCa Rate;
         else if(&Race eq  2) then CharRace = "AfrAmr";    *** african-american;


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

         label  SEQ_Num= "Sequence number of woman on data file"

                OC     = "Set to SAS missing value . when OC_Model has range error"

                AM     = "Set to SAS missing value . when AMeno has range error"
                PM     = "Set to SAS missing value . when PMeno has range error"

                Par    = "Set to SAS missing value . when Parity_Model has range error"

                CIG1   = "Set to SAS missing value . when Non Smoker Indicator has range error"
                CIG3   = "Set to SAS missing value . when Former Smoker Indicator has range error"

                BMI    = "Set to SAS missing value . when BMI 5 levels has range error"
                Horm   = "Set to SAS missing value . when Horm Model has range error";


         if      (&OC_Model          eq  0) then   OC    =0;
         else if (&OC_Model          eq  1) then   OC    =1;
         else                                      OC    =.;

         if      (&AMeno             eq  0) then   AM    =0;
         else if (&AMeno             eq  1) then   AM    =1;
         else if (&AMeno             eq  2) then   AM    =2;
         else                                      AM    =.;

         if      (&PMeno             eq 0) then    PM    =0;
         else if (&PMeno             eq 1) then    PM    =1;
         else                                      PM    =.;

         if      (&AMeno*&PMeno      ne 0) then  do;             *** cannot both be switched on;
                                                   AM    =.;
                                                   PM    =.;
                                                 end;


         if      (&Parity_Model       eq 0) then   Par   =0;
         else if (&Parity_Model       eq 1) then   Par   =1;
         else if (&Parity_Model       eq 2) then   Par   =2;
         else                                      Par   =.;


         if      (&Cig_1              eq 0) then   CIG1  =0;
         else if (&Cig_1              eq 1) then   CIG1  =1;
         else                                      CIG1  =.;

         if      (&Cig_3              eq 0) then   CIG3  =0;
         else if (&Cig_3              eq 1) then   CIG3  =1;
         else                                      CIG3  =.;

         if      (&Cig_1*&Cig_3       ne 0) then do;              *** cannot both be switched on;
                                                   CIG1  =.;
                                                   CIG3  =.;
                                                 end;

         if      (&BMI_Model          eq 0) then   BMI   =0;
         else if (&BMI_Model          eq 1) then   BMI   =1;
         else if (&BMI_Model          eq 2) then   BMI   =2;
         else if (&BMI_Model          eq 3) then   BMI   =3;
         else if (&BMI_Model          eq 4) then   BMI   =4;
         else                                      BMI   =.;

         if      (&Horm_Model         eq 0) then   Horm  =0;
         else if (&Horm_Model         eq 1) then   Horm  =1;
         else if (&Horm_Model         eq 2) then   Horm  =2;
         else                                      Horm  =.;

                                                  &HRT_Thin =           0;
         if      (&BMI_Model          eq 0) then  &HRT_Thin = &Horm_Model;  *** HRT x (BMI<25);


         if (RacCat eq  .   or

             OC     eq  .   or

             AM     eq  .   or
             PM     eq  .   or

             Par    eq  .   or

             CIG1   eq  .   or
             CIG3   eq  .   or

             BMI    eq  .   or
             Horm   eq  .)  then  Error_Ind =1;

         Key = 1;
   run;




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

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

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


         array  White_H1 (II)  WH1_1-WH1_8
        (0.0007623792, 0.0012755102, 0.0017471759, 0.0019367158,
         0.0019996332, 0.0019635253, 0.0017235483, 0.0011809037);  *** Dec 6 2012 h1* from R Pfeiffer;

         array  Black_H1 (II)  BH1_1-BH1_8
        (       43.12,       84.45,        145.74,       170.24,
               151.92,      133.72,        125.77,       103.73);  *** (BLACKuterus.xls  e-mail R Pfeiffer 9/17/2013);

         BH1_1 = BH1_1/100000;
         BH1_2 = BH1_2/100000;
         BH1_3 = BH1_3/100000;
         BH1_4 = BH1_4/100000;
         BH1_5 = BH1_5/100000;
         BH1_6 = BH1_6/100000;
         BH1_7 = BH1_7/100000;
         BH1_8 = BH1_8/100000;

         II  = .;
         Key = 1;
   run;


   data  H2;                                       *** h2, Competing hazards;

         array  White_H2 (II)   WH2_1-WH2_8
        (0.0032784, 0.0051238, 0.0085181, 0.0136584,
         0.0219604, 0.0349759, 0.0591399, 0.1396195);              *** Dec 6 2012 h2 from R Pfeiffer;

         array  Black_H2  (II)  BH2_1-BH2_8
        (   705.64,   1014.56,   1457.62,   2123.72,
           3097.85,   4498.35,   6815.27,  13381.46);              *** (BLACKuterus.xls  e-mail R Pfeiffer 9/17/2013);

         BH2_1 = BH2_1/100000;
         BH2_2 = BH2_2/100000;
         BH2_3 = BH2_3/100000;
         BH2_4 = BH2_4/100000;
         BH2_5 = BH2_5/100000;
         BH2_6 = BH2_6/100000;
         BH2_7 = BH2_7/100000;
         BH2_8 = BH2_8/100000;

         II  = .;
         Key = 1;
   run;


   data  LN_RR;                                           *** EnCa Model 5;
                                                          *** beta from 11/27/2012;
         array  White_Beta (JJ)  OC_Beta        AgeMeno_Beta   PreMeno_Beta
                                 Parity_Beta    Cig1_Beta      Cig3_Beta
                                 BMI5_Beta      Horm_Beta      HRT_Thin_Beta

                                (0.3657670277,

                                 0.2294336266,
                                 0.2516031668,

                                 0.1909347182,

                                 0.3854292580,
                                 0.1914167376,

                                 0.5429326917,
                                 0.1398432722,
                                 0.4758918014);                               *** lnRR5;


         array  Beta_Name (JJ) $ CBeta1-CBeta9 ('OC use   ', 'Age Meno ', 'Pre Meno ',
                                                'Parity   ', 'nonSmokr ', 'FormSmkr ',
                                                'BMI5 cat ', 'Horm     ', 'HRT Thin ');

         JJ  = .;
         Key = 1;
   run;


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

         White_1_AR = 0.187059011;

         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  TmpFile1;
   merge TmpFile   H1_Star;
   by    Key;
   run;


   data  TmpFile2;
   merge TmpFile1  H2;
   by    Key;
   run;


   data  TmpFile3;
   merge TmpFile2  LN_RR;
   by    Key;
   run;


   data  TmpFile4  (drop=Key);
   merge TmpFile3  FofT;
   by    Key;
   run;


   data  TmpFile;
   set   TmpFile4;

         array  White_H1  (II)    WH1_1-WH1_8;     *** white  h1star, EnCa composite incidences;
         array  White_H2  (II)    WH2_1-WH2_8;     *** white  h2,     Competing hazards;


         array  Black_H1  (II)    BH1_1-BH1_8;     *** black  h1star, EnCa composite incidences;
         array  Black_H2  (II)    BH2_1-BH2_8;     *** black  h2,     Competing hazards;


         array  Ages      (II)    A1-A8;
         array  AgesP5    (II)    AP1-AP8;


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


         array  White_Beta (JJ)  OC_Beta        AgeMeno_Beta   PreMeno_Beta
                                 Parity_Beta    Cig1_Beta      Cig3_Beta
                                 BMI5_Beta      Horm_Beta      HRT_Thin_Beta;


         *** 1-Attributable Risk = F(t)= White_1_AR=0.187059011;


         *** arrays to hold each 5yr age cat rate for each year in the 5yr age cat;
         array  WrkH1_1    (I5)  W1_01_1-W1_01_5;                            *** [50:55);
         array  WrkH1_2    (I5)  W1_02_1-W1_02_5;                            *** [55:60);
         array  WrkH1_3    (I5)  W1_03_1-W1_03_5;                            *** [60:65);
         array  WrkH1_4    (I5)  W1_04_1-W1_04_5;                            *** [65:70);
         array  WrkH1_5    (I5)  W1_05_1-W1_05_5;                            *** [70:75);
         array  WrkH1_6    (I5)  W1_06_1-W1_06_5;                            *** [75:80);
         array  WrkH1_7    (I5)  W1_07_1-W1_07_5;                            *** [80:85);
         array  WrkH1_8    (I5)  W1_08_1-W1_08_5;                            *** [85:90);

         array  Wrk_H1     (II)  WrkH1_1-WrkH1_8;               *** h1   for this record;


         array  WrkH2_1    (I5)  W2_01_1-W2_01_5;                            *** [50:55);
         array  WrkH2_2    (I5)  W2_02_1-W2_02_5;                            *** [55:60);
         array  WrkH2_3    (I5)  W2_03_1-W2_03_5;                            *** [60:65);
         array  WrkH2_4    (I5)  W2_04_1-W2_04_5;                            *** [65:70);
         array  WrkH2_5    (I5)  W2_05_1-W2_05_5;                            *** [70:75);
         array  WrkH2_6    (I5)  W2_06_1-W2_06_5;                            *** [75:80);
         array  WrkH2_7    (I5)  W2_07_1-W2_07_5;                            *** [80:85);
         array  WrkH2_8    (I5)  W2_08_1-W2_08_5;                            *** [85:90);

         array  Wrk_H2     (II)  WrkH2_1-WrkH2_8;               *** h2   for this record;


         array  Wrk_Beta   (JJ)  Beta1-Beta9;                   *** beta for this record;


         if      (&Race eq 1) then do;
            do II = 1 to 8;
               tmp1 = White_H1;                        *** Dec 6 2012 h1* from R Pfeiffer;
               tmp2 = White_H2;                        *** Dec 6 2012 h2  from R Pfeiffer;

               do I5 = 1 to 5;   *** fill in each yr of the 5yr cat with the correct rate;
                  Wrk_H1 = tmp1;
                  Wrk_H2 = tmp2;
               end;
            end;
         end;

         else if (&Race eq 2) then do;
            do II = 1 to 8;
               tmp1 = Black_H1;        *** (BLACKuterus.xls  e-mail R Pfeiffer 9/17/2013);
               tmp2 = Black_H2;        *** (BLACKuterus.xls  e-mail R Pfeiffer 9/17/2013);

               do I5 = 1 to 5;  *** fill in each yr of the 5yr cat with the correct rate;
                  Wrk_H1 = tmp1;
                  Wrk_H2 = tmp2;
               end;
            end;
         end;


         do JJ = 1 to 9;                                                        *** ln RR;
            Wrk_Beta = White_Beta;
         end;
   run;





   *** document all constants;

   data  Dummy;
   set   TmpFile;

         file print;

         array  White_H1  (II)  WH1_1-WH1_8;       *** white  h1star  EnCa composite incidences;
         array  White_H2  (II)  WH2_1-WH2_8;       *** white  h2      Competing hazards;

         array  Black_H1  (II)  BH1_1-BH1_8;       *** black  h1star  EnCa composite incidences;
         array  Black_H2  (II)  BH2_1-BH2_8;       *** black  h2,     Competing hazards;

         array  Ages      (II)  A1-A8;
         array  AgesP1    (II)  AP1-AP8;

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


         array  White_Beta (JJ)  OC_Beta        AgeMeno_Beta   PreMeno_Beta
                                 Parity_Beta    Cig1_Beta      Cig3_Beta
                                 BMI5_Beta      Horm_Beta      HRT_Thin_Beta;


         array  Beta_Name (JJ) $ CBeta1-CBeta9;


         if (_N_ eq 1) then do;
            put  " ";
            put  "Ln Relative Risk from Cox regressions:   All races use lnRR developed for White AARP/PLCO women";
            put  " ";
            put  "                              African                  Other, Unk    AsianAmrcn";
            put  "Beta              White      American      Hispanic    NativAmrcn    Pacf Islnd";
            put  "             (RR Model5)   (RR Model5)                                         ";
            put  " ";

            do JJ = 1 to 9;
               put  Beta_Name  $char9.
                    White_Beta   14.7
                    White_Beta   14.7
                    "   Preparation"
                    "   Preparation"
                    "   Preparation";
            end;



            put  " ";
            put  " ";
            put  "1-Attributable Risk:   All races use 1-AR developed for White AARP/PLCO women      F(t)";
            put  " ";
            put  "                              African                  Other, Unk    AsianAmrcn";
            put  "                  White      American      Hispanic    NativAmrcn    Pacf Islnd";
            put  "             (RR Model5)   (RR Model5)                                         ";
            put  " ";
            put  "1-AR     "
                  White_1_AR   14.7
                  White_1_AR   14.7
                  "   Preparation"
                  "   Preparation"
                  "   Preparation";



            put  " ";
            put  " ";
            put  "Endometrial Cancer Composite Incidences  (H1*)";
            put  " ";
            put  "5 year                        African                  Other, Unk    AsianAmrcn";
            put  "AgeGrp            White      American      Hispanic    NativAmrcn    Pacf Islnd";
            PUT  "             (12/6/2012)   (9/17/2013)                                         ";
            put  " ";

            do II = 1 to 8;
               put "[" Ages 2.0 ":"  AgesP5 2.0 ")"
                   White_H1   16.7
                   Black_H1   14.7
                   "   Preparation"
                   "   Preparation"
                   "   Preparation";
            end;



            put  " ";
            put  " ";
            put  "Competing Mortality  (H2)";
            put  " ";
            put  "5 year                        African                  Other, Unk    AsianAmrcn";
            put  "AgeGrp            White      American      Hispanic    NativAmrcn    Pacf Islnd";
            PUT  "             (12/6/2012)   (9/17/2013)                                         ";
            put  " ";

            do II = 1 to 8;
               put "[" Ages 2.0 ":"  AgesP5 2.0 ")"
                   White_H2   16.7
                   Black_H2   14.7
                   "   Preparation"
                   "   Preparation"
                   "   Preparation";
            end;
         end;

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



   *** EnCa risk projection;

   data  TmpFile;
   set   TmpFile;

         array  h1 (j_intvl) W1_01_1-W1_01_5  W1_02_1-W1_02_5  W1_03_1-W1_03_5
                             W1_04_1-W1_04_5  W1_05_1-W1_05_5  W1_06_1-W1_06_5
                             W1_07_1-W1_07_5  W1_08_1-W1_08_5;

         array  h2 (j_intvl) W2_01_1-W2_01_5  W2_02_1-W2_02_5  W2_03_1-W2_03_5
                             W2_04_1-W2_04_5  W2_05_1-W2_05_5  W2_06_1-W2_06_5
                             W2_07_1-W2_07_5  W2_08_1-W2_08_5;

         array  One_AR_RR (j_intvl)  One_AR_RR_01-One_AR_RR_40;

         array  PI_j(j_intvl) Pi1-Pi40;


          ***   temporary sas
                var names
                created in
                this macro
                VVVV     VVVVVVVVVVVV;

         LP5 =  OC       * OC_Beta        +

                AM       * AgeMeno_Beta   +
                PM       * PreMeno_Beta   +

                Par      * Parity_Beta    +

                CIG1     * Cig1_Beta      +
                CIG3     * Cig3_Beta      +

                BMI      * BMI5_Beta      +
                Horm     * Horm_Beta      +

               &HRT_Thin * HRT_Thin_Beta;



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

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

         RR_Star5   = exp(LP5);               *** RRstar woman of interest at ages ge 50;
        &RelRsk     = RR_Star5;               *** returned back to the calling program;

         One_AR_RR5 = White_1_AR*RR_Star5;    ***(1-AR)*rr=(1-AR)*exp(xstar*) age ge 50;

         do j_intvl = 1 to 40;
            One_AR_RR = One_AR_RR5;
         end;

         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 from;
            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*ONE_AR_RR + h2;

               PI_j   = ((One_AR_RR*h1/Hj)*exp(-Cum_H)) * (1-exp(-Hj*IntgrlLngth));
              &AbsRsk = &AbsRsk + PI_j;
               Cum_H  = Cum_H  + Hj*IntgrlLngth;
            end;
         end;


         &AbsRsk = 100*&AbsRsk;


         Key = 1;


         label White_1_AR    =  "1-Attributable risk EndoCa Model 5"
               One_AR_RR5    =  "(1-AR)*RelRsk EndoCa RR Model 5"
              &RelRsk        =  "Relative risk EndoCa RR Model 5"
              &AbsRsk        =  "Abs risk (percent) of EnCa in age interval [T1,T2) - Model 6";

           *** PattrnNumber  =  "Relative risk covariate pattern #";
   run;


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

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


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


   ***   Error listing file when errors detected as well as writing out a sas file which
         contains the projectd absolute risk as well as all the original input variables
         entryage -- Error_Ind ;

   data &SAS_File   (drop = set_T1_Missing  set_T2_Missing
                            OC  AM  PM  PAR   CIG1  CIG3  BMI  HORM
                            II -- NumbrIntvl  RR_Star5
                            Cum_H -- Key  Count_Error);
   set   TmpFile;
   by    Key;

         retain             Count_Error   0;

         file print;

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


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

            Count_Error = Count_Error + 1;

            if (Count_Error eq 1) then do;
               put "                                                                                                 " /
                   "                                         Age of                                     BMI          " /
                   "                                Oral  MenoPause    Age              Cig   Cig  0=   <25    Duratn" /
                   "                             Contrac  0= PreMen    Pre    Parity    Use   Use  1=[25,30)      HRT" /
                   "                                 Use    or <=49   Meno    0= >=3    Non  Frmr  2=[30,35)  0=   0y" /
                   "          Ntry  Exit          0= > 1  1=  50:54  Pause    1= 1:2  Smokr Smokr  3=[35,40)  1= <10y" /
                   "      ID   Age   Age    Race  1= 0,1  2=   >=55    Ind    2=   0    Ind   Ind  4=  >=40   2= 10+y" /
                   " ";
            end;

            Put
               &Woman_ID                        8.0
               &T1                              6.1
               &T2                              6.1

               &Race                            8.0

               &OC_Model                        8.0
               &PMeno                           7.0

               &AMeno                          11.0

               &Parity_Model                   10.0

               &Cig_1                           7.0
               &Cig_3                           6.0

               &BMI_Model                      10.0
               &Horm_Model                     10.0;


            Put
               set_T1_Missing                  14.1
               set_T2_Missing                   6.1

               RacCat                           8.0

               OC                               8.0

               AM                              11.0
               PM                               7.0

               Par                             10.0

               CIG1                             7.0
               CIG3                             6.0

               BMI                             10.0
               Horm                            10.0;

            Put " ";
         end;



         Label  Count_Error = "Counts # of records with edit errors in input file ";

         if (last.Key and Count_Error eq 0) then do;
            put " ";
            put "      -------------------------------------------"
                "----------------------------";
            put " ";
            put "      No errors detected for entry age, exit age "
                "and relative risk covaraites";
            put " ";
            put "      -------------------------------------------"
                "----------------------------";
            put "      -                                          "
                "                           -";
            put "      -                                          "
                "                           -";
         end;
         else if (last.Key and Count_Error gt 0) then do;
            put " ";
            put "There are " Count_Error 6.0 " records with errors in covariate values on input file";
            put " ";
            put " ";
         end;
   run;


   data &SAS_File;
   set  &SAS_File;
   run;
%mend;


