/******************************************************************************************
AARP_PLCO_BrCa_RAM_Model7: AARP PLCO (Br)east (Ca)ncer (R)isk (A)ssessment (M)acro (Model7)

Model 7  code is derived from  RelRisk___Final\ AARP_PLCO_BrCa_RAM_Model6  with inclusion
of Blk, Hispanic, API and Native American BrCa Incidences and Competing risks    8/26/2013

SAS macro to project for BrCa abs risk for white women based on the relative risk model 6
from a joint AARP/PLCO data set  (SAS file rfqdata.sas7bdat provided R Pfeiffer 11/17/11)
******************************************************************************************/

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

/*
        AARP_PLCO_BrCa_RAM_Model7  SAS macro which performs BrCa absolute risk projection
                                   based on  rr  model 6 developed 12/20/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

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

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

%macro  AARP_PLCO_BrCa_RAM_Model7 (SAS_File                  =,

                                   Woman_ID                  =,

                                   T1                        =,
                                   T2                        =,

                                   AgeMeno                   =,
                                   PreMeno                   =,

                                   EPTx_Model                =,
                                   Other_Horm                =,

                                   BBD_Model                 =,

                                   Age_1stChild_Brth         =,
                                   NumChild_0                =,

                                   FHbreast_Model            =,
                                   ALC_Model                 =,
                                   BMI_Model                 =,

                                   Race                      =,


                                   CharRace                  =,

                                   AbsRsk7                   =);


/*     MACRO PARAMETERS          DESCRIPTION/DEFINITION  OF  MACRO PARAMETERS
       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, current 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   50 <= T1 <  T2   (T2=projection age, see following)

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


                                 so  T1  and  T2           MUST SATISFY      following inequality

                                 !!!                   !!!!!!!!!!!!!!!!!!!                    !!!
                                 !!!                   50 <= T1 < T2 <= 90                    !!!
                                 !!!                   !!!!!!!!!!!!!!!!!!!                    !!!


       Age_Meno                  points to SAS variable which contains age at menopause
                                 Age_Meno           0=  le 49  or  pre-menopausal
                                                    1= [50:55)
                                                    2=  ge 55

       PreMeno                   points to SAS variable which contains pre-menopausal indicator
                                 PreMeno            0= Not pre-menopausal
                                                    1=     pre-menopausal

       EPTx_Model                points to SAS variable which contains Estrogen/Progesterone HRT duration
                                 EPTx_Model         0=    0 yrs of EP HRT   or   Other HRT
                                                    1= < 10 yrs
                                                    2= >=10 yrs

       Other_Horm                points to SAS variable which indicates  Other HRT
                                 Other_Horm         0= No
                                                    1= Yes

       BBD_Model                 points to SAS variable which contains Benign Breast Disease Diagnosed indicator
                                 BBD_Model          0= No
                                                    1= Yes

       Age_1stChild_Brth         points to SAS variable which contains Age of 1st Child Birth
                                 Age_1stChild_Brth  0=  lt 25 or NO live birth (i.e. nulliparous),
                                                    1= [25:30)
                                                    2=  ge 30

       NumChild_0                points to SAS variable which indicates no live birth i.e. nulliparous indicator
                                 NumChild_0         0= 1 or more live birth
                                                    1= No live birth, nulliparous indicator

       FHbreast_Model            points to SAS variable which contains family history of BrCa or OvCa indicator
                                 FHbreast_Model     0= No
                                                    1= Yes

       ALC_Model                 points to SAS variable which contains alcohol consumption
                                 ALC_Model          0=  doesn't drink
                                                    1= <1   drink/day
                                                    2=  1+  drink/day

       BMI_Model                 points to SAS variable which contains body mass index
                                 BMI_Model          0=          <25
                                                    1= >=25 and <30
                                                    2= >=30 and <35
                                                    3= >=35

       Race                      points to SAS variable which contains race of subject
                                 Race               1=White (SEER13 1992-2006 BrCa incidence)
                                                    2=African-American
                                                    3=Hispanic
                                                    4=Other (Native Americans, Unkn race)

                                                    5= Reserved for future expansion.  Invalid code in this version.

                                                    6=Asian, Pacific Islander
                                                    7=Asian, Pacific Islander, Native American

                                 !!! NOTE: that all races use   Relative Risk model 6  and Attributable Risk
                                 !!!       estimated from white women in the AARP/PLCO trial
                                 !!!
                                 !!!       BrCa incidences are race specific, ie each race BrCa incidences
                                 !!!       are based on races derived from the SEER registry for that race
                                 !!!
                                 !!!       Competing mortality rates are also race specific except for
                                 !!!       Hispanics, which share White competing mortality based on
                                 !!!       NCHS US mortality 1992-2006
                                 !!!
                                 !!!       Native American abs risk projection can be obtained by setting
                                 !!!       the Race to to either  4=Other  or
                                 !!!                              7=Asian, Pacific Islander, Native American
                                 !!!
                                 !!!       Asian, Pacific Islander abs risk projection can be obtained by
                                 !!!       setting the Race code to either  6 or 7
                                 !!!
                                 !!!       For both Native Americans and Asian, Pacific Islanders, different
                                 !!!       results for abs risk will be obtained by setting Race to the
                                 !!!       different codes

          All the previous listed variables are required inputs to       SAS Macro  AARP_PLCO_BrCa_RAM_Model7



          Following two variables are created and returned by            SAS Macro  AARP_PLCO_BrCa_RAM_Model7

       CharRace                  points to SAS variable which contains race as a character variable for each subject
                                 CharRace          " White"
                                                   "AfrAmr"
                                                   "Hispan"
                                                   "NatAmr"
                                                   "OtrWht"
                                                   "   API"
                                                   "API NA"

       AbsRsk7(%)                points to SAS variable which contains the projected abs risk in %
                                 Absolute Risk is from BrCa RR model 6
*/

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

         CharRace = "??????";

         title3 'AARP_PLCO_BrCa_RAM_Model7  sas macro for BrCa absolute risk based on PLCO/AARP BrCa RR Model 6';

                 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 (current age) and T2 (projection age)
         !!!                                                                       !!!
         !!!                        !!!!!!!!!!!!!!!!!!!                            !!!
         !!!                        50 <= T1 < T2 <= 90                            !!!
         !!!                        !!!!!!!!!!!!!!!!!!!                            !!!;


         set_T1_Missing = &T1;
         set_T2_Missing = &T2;

         if (&T1 lt 50) then do;                        *** current age must be ge 50;
            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;       *** 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 and  &Race ne 3 and  &Race ne 4 and
             &Race ne 6 and  &Race ne 7) 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;
         else if(&Race eq  3) then CharRace = "Hispan";    *** hispanic;
         else if(&Race eq  4) then CharRace = "NatAmr";    *** native american;

         else if(&Race eq  5) then CharRace = "Invald";    *** Reserved for future expansion
                                                               Invalid code for this version;

         else if(&Race eq  6) then CharRace = "   API";    *** Asian, Pacific Islander;
         else if(&Race eq  7) then CharRace = "API NA";    *** Asian, Pacific Islander, Native Amer;


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

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

                AMeno       = "Set to SAS missing value . when  AgeMeno has range error"
                PMeno       = "Set to SAS missing value . when  PreMeno has range error"

                EPT         = "Set to SAS missing value . when  EPTx_Model has range error"
                OHorm       = "Set to SAS missing value . when  Other_Horm has range error"

                BBD         = "Set to SAS missing value . when  BBD_Model has range error"

                A_FC        = "Set to SAS missing value . when  Age_1stChild_Brth has range error"
                Par0        = "Set to SAS missing value . when  NumChild_0 has range error"

                FamHist     = "Set to SAS missing value . when  Family History Model has range error"
                ALC         = "Set to SAS missing value . when  ALC Model has range error"
                BMI         = "Set to SAS missing value . when  BMI 4 levels has range error";


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

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

         if      (&AgeMeno*&PreMeno         ne 0) then do;              *** both variables can not be on at the same time;
                                                           AMeno =.;
                                                           PMeno =.;
                                                        end;


         if      (&EPTx_Model               eq 0) then  EPT      =0;
         else if (&EPTx_Model               eq 1) then  EPT      =1;
         else if (&EPTx_Model               eq 2) then  EPT      =2;
         else                                           EPT      =.;

         if      (&Other_Horm               eq 0) then  OHorm    =0;
         else if (&Other_Horm               eq 1) then  OHorm    =1;
         else                                           OHorm    =.;

         if      (&EPTx_Model*&Other_Horm   ne 0) then do;               *** both variables can not be on at the same time;
                                                          EPT    =.;
                                                          OHORM  =.;
                                                       end;


         if      (&BBD_Model                eq 0) then  BBD      =0;
         else if (&BBD_Model                eq 1) then  BBD      =1;
         else                                           BBD      =.;


         if      (&Age_1stChild_Brth        eq 0) then  A_FC     =0;
         else if (&Age_1stChild_Brth        eq 1) then  A_FC     =1;
         else if (&Age_1stChild_Brth        eq 2) then  A_FC     =2;
         else                                           A_FC     =.;

         if      (&NumChild_0               eq 0) then  Par0     =0;     *** NumChild_0 is indicator for nulliparious;
         else if (&NumChild_0               eq 1) then  Par0     =1;
         else                                           Par0     =.;

         if      (&NumChild_0               eq 1 and
                  &&Age_1stChild_Brth       ne 0) then do;
                                                          Par0     =.;   *** if NumChild_0=1 ---> nulliparious then;
                                                          A_FC     =.;   *** Age_1stChild_Brth must be 0  otherwise;
                                                       end;              *** error condition;


         if      (&FHbreast_Model           eq 0) then  FamHist  =0;
         else if (&FHbreast_Model           eq 1) then  FamHist  =1;
         else                                           FamHist  =.;


         if      (&ALC_Model                eq 0) then  ALC      =0;
         else if (&ALC_Model                eq 1) then  ALC      =1;
         else if (&ALC_Model                eq 2) then  ALC      =2;
         else                                           ALC      =.;


         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                                           BMI      =.;


         if (RacCat    eq  . or

             AMeno     eq  . or
             PMeno     eq  . or

             EPT       eq  . or
             OHorm     eq  . or

             BBD       eq  . or

             A_FC      eq  . or
             Par0      eq  . or

             FamHist   eq  . or
             ALC       eq  . or
             BMI       eq  .)    then  Error_Ind =1;

         Key = 1;
   run;


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

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

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


         array  White_H11 (II)  WH11_1-WH11_8
        (0.002669,  0.003317,  0.004015,  0.004580,  0.004952,  0.005171,
         0.004900,  0.004042);                        *** SEER13 incidence 1992-2006;


         array  White_H12 (II)  WH12_1-WH12_8
        (0.002429,  0.002974,  0.003617,  0.004177,  0.004555,  0.004816,
         0.004705,  0.004115);                        *** SEER13 incidence 19xx-xxxx;


         array  Black_H1  (II)  BH1_1-BH1_8
        (0.002249,  0.002741,  0.003081,  0.003424,  0.003694,  0.003915,
         0.003634,  0.003490);                        *** SEER13 incidence 1992-2006;


         array  Hspnc_H1  (II)  HH1_1-HH1_8
        (0.001680,  0.002054,  0.002437,  0.002711,  0.002878,  0.003023,
         0.002807,  0.002348);                        *** SEER13 incidence 1992-2006;


        array  Other_H1  (II)  OH1_1-OH1_8
        (0.001158,  0.001326,  0.001537,  0.001575,  0.001804,  0.001940,
         0.001886,  0.001559);                        *** SEER13 incidence 1992-2006;


         array  API_H1    (II)  AH1_1-AH1_8
        (0.001942,  0.002312,  0.002460,  0.002515,  0.002608,  0.002528,
         0.002266,  0.001794);                        *** SEER13 incidence 1992-2006;


         array  APINA_H1  (II)  ANH1_1-ANH1_8
        (0.001874,  0.002231,  0.002392,  0.002456,  0.002564,  0.002496,
         0.002245,  0.001779);                        *** SEER13 incidence 1992-2006;

         II  = .;
         Key = 1;
   run;


   data  H2;                                                   *** h2, Competing hazards;

         array  White_H21  (II) WH21_1-WH21_8
        (0.002856,  0.004577,  0.007749,  0.012679,  0.020765,  0.033464,
         0.057215,  0.13578);                          *** NCHS US mortality 1992-2006;


         array  White_H22  (II) WH22_1-WH22_8
        (0.002856,  0.004577,  0.007749,  0.012679,  0.020765,  0.033464,
         0.057215,  0.13578);                          *** NCHS US mortality 1992-2006;


         array  Black_H2  (II)  BH2_1-BH2_8
        (0.006495,  0.009410,  0.013924,  0.020513,  0.030192,  0.044099,
         0.066924,  0.132235);


         array  Hspnc_H2  (II)  HH2_1-HH2_8
        (0.002856,  0.004577,  0.007749,  0.012679,  0.020765,  0.033464,
         0.057215,  0.13578);                          *** NCHS US mortality 1992-2006  same as white;


         array  Other_H2  (II)  OH2_1-OH2_8
        (0.001991,  0.003220,  0.005193,  0.008142,  0.013140,  0.022174,
         0.039368,  0.095363);                         *** same as asian, pacific islander, native amr;


         array  API_H2    (II)  AH2_1-AH2_8
        (0.001991,  0.003220,  0.005193,  0.008142,  0.013140,  0.022174,
         0.039368,  0.095363);


         array  APINA_H2  (II)  ANH2_1-ANH2_8
        (0.001991,  0.003220,  0.005193,  0.008142,  0.013140,  0.022174,
         0.039368,  0.095363);

         II  = .;
         Key = 1;
   run;


   data  LN_RR;                                           *** BrCa Model RR 6 model;
                                                          *** beta from 12/20/2011;

         array  White_Beta (JJ)  AMeno_Beta
                                 PMeno_Beta

                                 EP_HRT_Beta
                                 OtrHRT_Beta

                                 BBD_Beta

                                 A_FC_Beta
                                 Par0_Beta

                                 FamHist_Beta
                                 ALC_Beta
                                 BMI4_Beta

                                (0.16259195966629,
                                 0.11413328275960,

                                 0.33943959445188,
                                 0.14937884641132,

                                 0.33818932124946,

                                 0.15283421452511,
                                 0.27379601548493,

                                 0.33194098826606,
                                 0.11449425026132,
                                 0.08135343130284);                                      *** lnRR;


         array  Beta_Name (JJ) $ CBeta1-CBeta10 ('Age Meno ',              /*  0.16259195966629 */
                                                 'Pre Meno ',              /*  0.11413328275960 */

                                                 'EP HRT   ',              /*  0.33943959445188 */
                                                 'OtrHRT   ',              /*  0.14937884641132 */

                                                 'B9 Br Dz ',              /*  0.33818932124946 */

                                                 'AgeFstLB ',              /*  0.15283421452511 */
                                                 'Nullipar ',              /*  0.27379601548493 */

                                                 'FamHstCa ',              /*  0.33194098826606 */
                                                 'Drnk/day ',              /*  0.11449425026132 */
                                                 'BMI4cats ' );            /*  0.08135343130284 */

         JJ  = .;
         Key = 1;
   run;


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

         White_1_AR = 0.4803239408;                       *** N/sum(RR due rr covarates only);

         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;


   data  TmpFile;
   set   TmpFile;

         array  White_H11  (II)    WH11_1-WH11_8;     *** h1star, BrCa composite incidences;

         array  White_H21  (II)    WH21_1-WH21_8;     *** h2,     Competing hazards;

         array  White_H12  (II)    WH12_1-WH12_8;
         array  White_H22  (II)    WH22_1-WH22_8;

         array  Black_H1   (II)    BH1_1-BH1_8;
         array  Black_H2   (II)    BH2_1-BH2_8;

         array  Hspnc_H1   (II)    HH1_1-HH1_8;
         array  Hspnc_H2   (II)    HH2_1-HH2_8;

         array  Other_H1   (II)    OH1_1-OH1_8;
         array  Other_H2   (II)    OH2_1-OH2_8;

         array  API_H1     (II)    AH1_1-AH1_8;
         array  API_H2     (II)    AH2_1-AH2_8;

         array  APINA_H1   (II)    ANH1_1-ANH1_8;
         array  APINA_H2   (II)    ANH2_1-ANH2_8;



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


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


         array  White_Beta (JJ)  AMeno_Beta
                                 PMeno_Beta

                                 EP_HRT_Beta
                                 OtrHRT_Beta

                                 BBD_Beta

                                 A_FC_Beta
                                 Par0_Beta

                                 FamHist_Beta
                                 ALC_Beta
                                 BMI4_Beta;


         *** 1-Attributable Risk = F(t)= White_1_AR=0.3116253437  N/sum(RR due rr covarates only);


         *** 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-Beta10;                  *** beta for this record;


         if      (&Race eq 1 or &Race eq 5) then do;               *** 1=white 1992-2006;
            do II = 1 to 8;                                        *** 5=white 19xx-xxxx;
               if      (&Race eq 1) then do;
                  tmp1 = White_H11;          *** SEER13 incidence  1992-2006;
                  tmp2 = White_H21;          *** NCHS US mortality 1992-2006;
               end;
               else if (&Race eq 5) then do;
                  tmp1 = White_H12;          *** SEER13 incidence  19xx-xxxx;
                  tmp2 = White_H22;          *** NCHS US mortality 1992-2006;
               end;

               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;                             *** 2=black 1992-2006;
            do II = 1 to 8;
               tmp1 = Black_H1;              *** SEER13 incidence  1992-2006;
               tmp2 = Black_H2;              *** NCHS US mortality 1992-2006;

               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 3) then do;                             *** 3=hispanic 1992-2006;
            do II = 1 to 8;
               tmp1 = Hspnc_H1;
               tmp2 = Hspnc_H2;

               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 4) then do;                             *** 4=other, natvmmr, unk 1992-2006;
            do II = 1 to 8;
               tmp1 = Other_H1;
               tmp2 = Other_H2;

               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 6) then do;                             *** 6=asian, pacisl 1992-2006;
            do II = 1 to 8;
               tmp1 = API_H1;
               tmp2 = API_H2;

               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 7) then do;                             *** 7=asian, pacisl, natvsmr 1992-2006;
            do II = 1 to 8;
               tmp1 = APINA_H1;
               tmp2 = APINA_H2;

               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 10;                                                        *** ln RR;
            Wrk_Beta = White_Beta;
         end;
   run;




   *** document all constants;

   data  Dummy;
   set   TmpFile;

         file print;

         array  White_Beta (JJ)  AMeno_Beta
                                 PMeno_Beta

                                 EP_HRT_Beta
                                 OtrHRT_Beta

                                 BBD_Beta

                                 A_FC_Beta
                                 Par0_Beta

                                 FamHist_Beta
                                 ALC_Beta
                                 BMI4_Beta;


         array  Beta_Name (JJ) $ CBeta1-CBeta10;


         array  White_H11  (II)    WH11_1-WH11_8;
         array  White_H21  (II)    WH21_1-WH21_8;

         array  White_H12  (II)    WH12_1-WH12_8;
         array  White_H22  (II)    WH22_1-WH22_8;

         array  Black_H1   (II)    BH1_1-BH1_8;
         array  Black_H2   (II)    BH2_1-BH2_8;

         array  Hspnc_H1   (II)    HH1_1-HH1_8;
         array  Hspnc_H2   (II)    HH2_1-HH2_8;

         array  Other_H1   (II)    OH1_1-OH1_8;
         array  Other_H2   (II)    OH2_1-OH2_8;

         array  API_H1     (II)    AH1_1-AH1_8;
         array  API_H2     (II)    AH2_1-AH2_8;

         array  APINA_H1   (II)    ANH1_1-ANH1_8;
         array  APINA_H2   (II)    ANH2_1-ANH2_8;

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

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



         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   AsnAmr,Pacf";
            put  "Beta              White      American      Hispanic    NativAmrcn    Pacf Islnd    NativAmrcn";
            put  " ";

            do JJ = 1 to 10;
               put  Beta_Name  $char9.
                    White_Beta   14.7
                    White_Beta   14.7
                    White_Beta   14.7
                    White_Beta   14.7
                    White_Beta   14.7
                    White_Beta   14.7;
            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   AsnAmr,Pacf";
            put  "                  White      American      Hispanic    NativAmrcn    Pacf Islnd    NativAmrcn";
            put  " ";
            put  "1-AR     "
                  White_1_AR   14.7
                  White_1_AR   14.7
                  White_1_AR   14.7
                  White_1_AR   14.7
                  White_1_AR   14.7
                  White_1_AR   14.7;




            put  " ";
            put  " ";
            put  "Breast Cancer Composite Incidences  (H1*)                                      SEER13 1992-06";
            put  " ";
            put  "5 year                        African                  Other, Unk    AsianAmrcn   AsnAmr,Pacf";
            put  "AgeGrp            White      American      Hispanic    NativAmrcn    Pacf Islnd    NativAmrcn";
            put  " ";

            do II = 1 to 8;
               put "[" Ages 2.0 ":"  AgesP5 2.0 ")"
                   White_H11  16.6
                   Black_H1   14.6
                   Hspnc_H1   14.6
                   Other_H1   14.6
                   API_H1     14.6
                   APINA_H1   14.6;
                   ***White_H21 16.6;
            end;




            put  " ";
            put  " ";
            put  "Competing Mortality  (H2)   Hispanic share White competing mortality     US mortality 1992-06";
            put  " ";
            put  "5 year                        African                  Other, Unk    AsianAmrcn   AsnAmr,Pacf";
            put  "AgeGrp            White      American      Hispanic    NativAmrcn    Pacf Islnd    NativAmrcn";
            put  " ";

            do II = 1 to 8;
               put "[" Ages 2.0 ":"  AgesP5 2.0 ")"
                   White_H21  16.6
                   Black_H2   14.6
                   Hspnc_H2   14.6
                   Other_H2   14.6
                   API_H2     14.6
                   APINA_H2   14.6;
            end;
         end;

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




   *** BrCa 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;

          ***  tmp sas
               var names
               created in
               this macro
               VVVVV      VVVVVVVV;

         LP6 = AMeno    * AMeno_Beta     +
               PMeno    * PMeno_Beta     +

               EPT      * EP_HRT_Beta    +
               OHorm    * OtrHRT_Beta    +

               BBD      * BBD_Beta       +

               A_FC     * A_FC_Beta      +
               Par0     * Par0_Beta      +

               FamHist  * FamHist_Beta   +
               ALC      * ALC_Beta       +
               BMI      * BMI4_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_Star6   = exp(LP6);               *** RRstar woman of interest at ages ge 50;

         One_AR_RR6 = White_1_AR*RR_Star6;    ***(1-AR)*rr=(1-AR)*exp(xstar*) age ge 50;

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

         if      (Error_Ind eq 1) then
           &AbsRsk7 = .;                                     *** erroneous input record
                                                                 set abs risk to missing;

         else if (Error_Ind eq 0) then do;                   *** error free input record;
           &AbsRsk7 = 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));
              &AbsRsk7 = &AbsRsk7 + PI_j;
               Cum_H   = Cum_H  + Hj*IntgrlLngth;
            end;
         end;


         &AbsRsk7 = 100*&AbsRsk7;


         Key = 1;


         label White_1_AR    =  "1-Attributable risk BrCa Model 6"
               RR_Star6      =  "Relative risk BrCa Model 6"
               One_AR_RR6    =  "(1-AR)*RelRsk BrCa Model 6"
              &AbsRsk7       =  "Abs risk (%) of BrCa in age interval [T1,T2) - Model 7";

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


   proc  means  data=TmpFile  mean std n nmiss  maxdec=5;
   var   Error_Ind
        &AbsRsk7         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"
           " 'AbsRsk7' 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
                            II -- NumbrIntvl
                            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          Duratn                    Age 1st           Fam           BodyMass " /
                   "                              MenoPause          EP HRT                  LiveBirth          Hist   Drnks      Index " /
                   "                              0= PreMen    Pre   0=Othr           B9Br   0= Nullip          BrCa    /day   0=   <25 " /
                   "                                or <=49   Meno    or 0y   Other     Dz     or  <25  Nulli   OvCa    0= 0   1=[25:30)" /
                   "      ID Currn  Proj          1=  50:54  Pause   1=<10y     HRT   0= n   1=  25:29  parus   0= n    1=<1   2=[30:35)" /
                   "       #   Age   Age    Race  2=   >=55    Ind   2=10+y  Indcat   1= y   2=   >=30    Ind   1= y    2=1+   3=  >=35)" /
                   "                                                                                                                    " ;
            end;


            Put
            &Woman_ID                        8.0
            &T1                              6.1
            &T2                              6.1

            &Race                            8.0

            &AgeMeno                        11.0
            &PreMeno                         7.0

            &EPTx_Model                      9.0
            &Other_Horm                      8.0

            &BBD_Model                       7.0

            &Age_1stChild_Brth              12.0
            &NumChild_0                      7.0

            &FHbreast_Model                  7.0
            &ALC_Model                       8.0
            &BMI_Model                      11.0;


            Put
            set_T1_Missing                  14.1
            set_T2_Missing                   6.1

            RacCat                           8.0

            AMeno                           11.0
            PMeno                            7.0

            EPT                              9.0
            OHorm                            8.0

            BBD                              7.0

            A_FC                            12.0
            Par0                             7.0

            FamHist                          7.0
            ALC                              8.0
            BMI                             11.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 "      -------------------------------------------"
                "----------------------------";
         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;
