/****************************************************************************************

NCI Hispanic BrCa Risk Assessment Macro (NCI Hisp BCRAM)  Version 2.0           7/17/2015
NCI Hispanic BrCa Risk Assessment Macro (NCI Hisp BCRAM)  Version 2.1           7/16/2016

This version of the absolute risk SAS macro for Hispanic women reflects the changes to
the definition of  Age 1st live Birth, where Nullparous women have been grouped with
Age 1st live birth of 30+.  Due to this change in definition of the covariate,
the lnRR, 1-AR in this version are based on the updated/modified model.

HispBrCa_RAM_Ver2_1  SAS macro to project for absolute risk of BrCa for Hispanic women as
                     well as providing estimates for 95%CI of the projected absolute risk
                     Bootstrap input from flat files, not SAS system files as in Ver2.0

This SAS Macro consists of two parts:

(1)  Readme section describes:

        o required input variables
        o valid values for input variables
        o restrictions  on input variables
        o error codes
        o recoding rules for relative risk covariates

(2)  The SAS Macro itself starting after the Readme section
****************************************************************************************



*****************************************************************************************
Readme section:


Input data:
----------
In_File= should "point" to a SAS data set containing all the required input data
items needed  to perform risk projections, such as initial age, projection age, BrCa
relative risk covariates and birth locality.  See the paragraph "Input data items ... "
below, for a detailed description of all required data items.


Output data:
-----------
Out_File= should "point" to a SAS output data set which will contain the projected
absolute risk of BrCa, SD of projected risk and 95% CI, as well as the original input
data items.


Macro structure:
---------------
        Macro                Macro            "points" to
        name                 parameters        SAS data files or SAS variables

%macro  HispBrCa_RAM_Ver2_1 (In_File     =,    name of input  sas data set
                             Out_File    =,    name of output sas data set

                             NumBoots    =,    # of bootstrap resmpling for StdErr and 95% CI

                                               NumBoots=0    AbsRisk estimated,
                                                             Bootstrap not performed

                                               NumBoots=1-29 Invalid selections for program.
                                                             Program aborts
                                                             i.e. bootstraps procedure
                                                                  have to specify ge 30
                                                                  bootstrap replications

                                               IF (NumBoots ge   30 and NumBoots le   999)
                                                   AbsRisk estimated
                                                   AbsRisk StdErr is estimated
                                                   95%CI = AbsRisk +- 1.96*StdErr

                                               IF (NumBoots ge 1000 and NumBoots le 10000)
                                                   AbsRisk estimated
                                                   AbsRisk StdErr is estimated
                                                   95%CI obtained from Bootstrap distribution

                                                   the larger NumBoots is, the more resources
                                                   needed in terms of computer memory and time

                             WID         =,    woman's ID #    1,2,.... postive integers

                             T1          =,    initial age,    age at beginning of BrCa
                                                               projection interval

                             T2          =,    projection age, age at end of       BrCa
                                                               projection interval

                             Biopsy      =,    indicator of biopsy performed
                             HyperP      =,    did biopsy exhibit atypical hyperplasia?
                             AgeMen      =,    age at menarchy  (lnRR=0 for US born)
                             Age1st      =,    age at 1st live birth
                             FstRels     =,    indicator of 1st degree relatives with brca


                             US_Born     =,    US born indicator  =1 US born  0=Foreign born

                             all the above quantities are required inputs to HispBrCa_RAM_Ver2_1


                             following quantities are returned by HispBrCa_RAM_Ver2_1

                             RR_Star1    =,    rel risk  age lt 50
                             RR_Star2    =,    rel risk  age ge 50

                             AbsRsk      =,    women's absolute risk of BrCa in %
                             LowrCL      =,    lower 95% CI of abs risk in %
                             UpprCL      =,    upper 95% CI of abs risk in %
                             SD_AR       =);   SD of abs risk in %

appropriate sas file names or sas variable names must be associated with all macro
parameters on the invocation of the sas macro  "HispBrCa_RAM_Ver2_1".

For example by coding  "In_File = DataIn"  informs the macro that the user created
sas file with a file name of  "DataIn"  is to be used for input of variables.
Similarly, coding  "Biopsy = BiopsInd",  lets the macro know that the sas variable
with name of  "BiopsInd"  in the sas input file  "DataIn"  contains the indicator
of whether biopsy was performed or not.

To involke the sas macro in your sas program, an  %include  statement must be coded in
your sas program, which points to  HispBrCa_RAM_Ver2_1  and the folder where it is stored.

For example:

the statement: %include "HispBrCa_RAM_Ver2_1";         points to the sas macro  HispBrCa_RAM_Ver2_1
                                                       residing in your current directory

the statement: %include "c:\macro\HispBrCa_RAM_Ver2_1";points to the sas macro  HispBrCa_RAM_Ver2_1
                                                       stored in the directory c:\macro




Input data items needed to project for Hispanic women BrCa absolute risk:

Macro
parameter      Definition                    Valid values

NumBoots       # of bootstrap resampling     must be 0 or between [30, 10000]

                                             NumBoots = 0  projected AbsRisk is obtained
                                                           95%CI and StdErr not obtained

                                             Suggest NumBoots ge 2000 for 95%CI from bootstrap distribution function
                                                     NumBoots ge  100 for 95%CI from normal theory


WID            Woman's ID                    positive integers 1,2,3...

T1             Initial         age           all real numbers T1 in [20,90)
T2             BrCa projection age           all real numbers T2 such that  T1 < T2 <= 90

               CONSTRAINT on T1 and T2:      20 <= T1 < T2 <= 90

Biopsy         Indicator Biopsy performed    0=no,
                                             1=yes,
                                            99=unknown  (99 recoded to 0)

HyperP         Atypical Hyperplasia Ind      0=no,
                                             1=yes,
                                            99=unknown (this is the default value)

               Biopsy = 0  or  99  then  HyperP DEFAULTS to = 99  (inflates RR by 1.00)

               Biopsy = 1          then  HyperP             =  0, (inflates RR by 0.93) or
                                                               1, (inflates RR by 1.82) or
                                                              99  (inflates RR by 1.00)

AgeMen         Age at menarchy               0=age menarchy  ge 14
                                             1=age menarchy  12:13
                                             2=age menarchy  lt 12
                                            99=unknown (99 recoded to 0)

Age1st         Age at first live birth       0=age 1st live birth  lt 20
                                             1=age 1st live birth     20-29
                                             2=age 1st live birth  ge 30   or  nulliparous
                                            99=unknown (99 recoded to 0)

FstRels        1st degree relative with BrCa 0=no,
                                             1=yes,
                                            99=unknown (99 recoded to 0)

              3         Born in the US?               0=no,
                                             1=yes,
                                            99=unknown (99 recoded to 1)


Statistical issues  should be directed to:  Dr. Mitchell Gail    gailm@exchange.nih.gov
Technical   details should be directed to:  Mr. David Pee        djkento88@yahoo.com


End of Readme section.
****************************************************************************************/


options  nocenter  ls=120  ps=9999  notes;
options  FORMCHAR="|----|+|---+=|-/\<>*";


***     HispBrCa_RAM_Ver2_1   start of macro which performs BrCa absolute risk projection

        Modification/change of any code from this point onward should be avoided,
        i.e. from  "%macro    HispBrCa_RAM_Ver2_1"  to the line which reads
        "%mend    HispBrCa_RAM_Ver2_1".  roughly 750 lines of code,  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           all macro parameters need to "point" to appropriate
                             parameters      sas file or sas variable names upon invocation;

%macro  HispBrCa_RAM_Ver2_1 (In_File         =,
                             Out_File        =,

                             NumBoots        =,

                             WID             =,

                             T1              =,
                             T2              =,

                             Biopsy          =,
                             HyperP          =,
                             AgeMen          =,
                             Age1st          =,
                             FstRels         =,

                             US_Born         =,

                             RR_Star1        =,
                             RR_Star2        =,

                             AbsRsk          =,
                             LowrCL          =,
                             UpprCL          =,
                             SD_AR           =);

   ***   sas system dataset which contains rr covariates for women of interest
         i.e. women whom we wish to project absolute risk based on their relative
         risk covariate values, initial age and projection age;
   data  TmpFile;      *** temporary working sas file containing rr covariate data;
   set  &In_File;      *** original  input   sas file containing rr covariate data;

         *** set raw rr covariate value file indicator to default value of 1;
         Raw_Ind = 1;


         title4 'HispBrCa_RAM_Ver2_1,  sas macro to project for Hispanic BrCa absolute risk with 95%CI';

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


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

         if (&T1 lt 20 or &T1 ge 90) then do;
            set_T1_Missing = .;
            Error_Ind      = 1;
         end;

         if (&T2 gt 90) then do;
            set_T2_Missing = .;
            Error_Ind      = 1;
         end;

         if (&T1 ge &T2) then do;
            set_T1_Missing = .;
            set_T2_Missing = .;
            Error_Ind      = 1;
         end;



         *** # of bootstrap samples;
                                                                                                NBoots = &NumBoots;
         if (&NumBoots lt 0 or (&NumBoots ge 1 and &NumBoots le 29) or &NumBoots gt 10000) then NBoots = .;


         if (Raw_Ind eq 1) then do;
         *** rr covariates are in raw/original format;

            *** US Born indicator;

            US_Brn = &US_Born;

            if  (&US_Born eq 99                       ) then do;
               US_Brn = 1;                                        *** defaults to US born;
            end;
            else if (&US_Born ne 0  and  &US_Born ne 1) then do;
               US_Brn = .;
            end;



            *** codes to test for consistency of Biopsy (#biopsies) and Hyperplasia;
            BI_Cat = -1;                          ***set BI_Cat to default value -1;

            if      ((&Biopsy eq 0  or &Biopsy eq 99) and &HyperP ne 99)
            then do;
               BI_Cat             = .a;                          *** see REQUIREMENT (A);
               Error_Ind          =  1;
            end;
            else if ((&Biopsy gt 0 and &Biopsy lt 99) and (&HyperP ne  0 and
                                                           &HyperP ne  1 and
                                                           &HyperP ne 99))
            then do;
               BI_Cat             = .b;                          *** see REQUIREMENT (B);
               Error_Ind          =  1;
            end;

            *** editing and recoding for Biopsy (#biopsies);
            if (BI_Cat eq -1) then do;   *** BI_Cat=-1 implies no REQUIREMENT violations;
               if      (&Biopsy eq  0  or  &Biopsy eq 99)  then  BI_Cat=0;
               else if (&Biopsy ge  1 and  &Biopsy ne 99)  then  BI_Cat=1;
               else                                              BI_Cat=.;
            end;


            *** Atypical Hyperplasia,  RR multiplicative factor;
            if      (BI_Cat eq 0) then do;     *** # Biopsy=0 or 99 (unk);
                                                        R_Hyp = 1.00;
            end;
            else if (BI_Cat eq 1) then do;     *** # Biopsy>0;
               if      (&HyperP eq  0)            then  R_Hyp = 0.93;
               else if (&HyperP eq  1)            then  R_Hyp = 1.82;
               else if (&HyperP eq 99)            then  R_Hyp = 1.00;
               else                                     R_Hyp =  .  ;
            end;


            *** editing and recoding for AgeMen;
            if     ((&AgeMen ge 14 and &AgeMen le &T1) or &AgeMen eq 99)  then AM_Cat=0;
            else if( &AgeMen ge 12 and &AgeMen lt  14)                    then AM_Cat=1;
            else if( &AgeMen gt  0 and &AgeMen lt  12)                    then AM_Cat=2;
            else                                                               AM_Cat=.;

            if     (&US_Born eq  1                   )                    then AM_Cat=0;


            *** editing and recoding for Age1st;
            if     (&Age1st lt &AgeMen and &AgeMen ne 99)                 then AF_Cat=.;
            else if(&Age1st gt &T1 and &Age1st lt     98)                 then AF_Cat=.;
            else if(&Age1st le  19  or &Age1st eq     99)                 then AF_Cat=0;
            else if(&Age1st ge  20 and &Age1st le     29)                 then AF_Cat=1;
            else if(&Age1st ge  30  or &Age1st eq     98)                 then AF_Cat=2;
            else                                                               AF_Cat=.;


            *** editing and recoding for FstRels;
            if     ( &FstRels eq  0  or &FstRels eq 99)                   then FR_Cat=0;
            else if( &FstRels ge  1 and &FstRels ne 99)                   then FR_Cat=1;
            else                                                               FR_Cat=.;
         end;
         else if (Raw_Ind eq 0) then do;
         *** RR covariates have already been re-coded to 0, 1, 2 or 3(when necessary).
             NOTE: edit/consistency checks for all relative four risk covariates
                   not performed when Raw_Ind=0.  use this option at your own risk;

            NBoots = &NumBoots;
            BI_Cat = &Biopsy;
            R_HYP  = &HyperP;
            AM_Cat = &AgeMen;
            AF_Cat = &Age1st;
            FR_Cat = &FstRels;
            US_Brn = &US_Born;
         end;




         if (NBoots eq .  or
             BI_Cat eq .  or
             R_Hyp  eq .  or
             AM_Cat eq .  or
             AF_Cat eq .  or
             FR_Cat eq .  or
             US_Brn eq .) then  Error_Ind = 1;

         Key = 1;
   run;




   *** proc  means  data=tmpfile;
   *** vars  &T1   &T2   ERROR_Ind;
   *** 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;

/*  invasive BrCa incidence rates from NCI BrCa Risk Disk

         array  Hspnc_H1 (II)  HH1_1-HH1_14
        (0.00002000, 0.00007100, 0.00019700, 0.00043800, 0.00081100, 0.00130700,
         0.00157400, 0.00185700, 0.00215100, 0.00251200, 0.00284600, 0.00275700,
         0.00252300, 0.00203900);                                 *** SEER hspan 1990:96;
                                                                  *** NCI Risk Disk;
*/


         array  FB_Hspnc_H1 (II) FBH1_1-FBH1_14
        (0.0000102,  0.0000531,  0.0001578,  0.0003602,  0.0007617,  0.0011599,
         0.0014111,  0.0017245,  0.0020619,  0.0023603,  0.0025575,  0.0028227,
         0.0028295,  0.0025868);                                 *** SEER Ca Hisp 1995-2004;


         array  USB_Hspnc_H1 (II) USBH1_1-USBH1_14
        (0.0000166,  0.0000741,  0.0002740,  0.0006099,  0.0012225,  0.0019027,
         0.0023142,  0.0028357,  0.0031144,  0.0030794,  0.0033344,  0.0035082,
         0.0025308,  0.0020414);                                 *** SEER Ca Hisp 1995-2004;

         array  Ages     (II)  A1-A14
        (20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85);

         II  = .;
         Key = 1;
   run;


   data  H2;                                               *** h2,     Competing hazards;

/*  competing mortality from NCI BrCa Risk Disk

         array  Hspnc_H2 (II)  HH2_1-HH2_14
        (0.00043700, 0.00053300, 0.00070000, 0.00089700, 0.00116300, 0.00170200,
         0.00264600, 0.00421600, 0.00696000, 0.01086700, 0.01685800, 0.02515600,
         0.04186600, 0.08947600);                                 *** NCHS hspan 1990:96;
                                                                  *** NCI Risk Disk;
*/


         array  FB_Hspnc_H2 (II)  FBH2_1-FBH2_14
        (0.0003129,  0.0002908,  0.0003515,  0.0004943,  0.0007807,  0.0012840,
         0.0020325,  0.0034533,  0.0058674,  0.0096888,  0.0154429,  0.0254675,
         0.0448037,  0.1125678);                                 *** SEER CA Hisp 1995:2004;

         array  USB_Hspnc_H2 (II) USBH2_1-USBH2_14
        (0.0003561,  0.0004038,  0.0005281,  0.0008875,  0.0013987,  0.0020769,
         0.0030912,  0.0046960,  0.0076050,  0.0120555,  0.0193805,  0.0288386,
         0.0429634,  0.0740349);                                 *** SEER CA Hisp 1995:2004;

         II  = .;
         Key = 1;
   run;


   data  LN_RR;                                    *** beta=lnRR,   from Gail/CARE model;

/*  relative risk from NCI BrCa Risk Disk

         array  USHspnc_Beta (JJ)  USH_BI_Beta      USH_AM_Beta     USH_AF_Beta     USH_FR_Beta
                                   USH_AC_NB_Beta   USH_AF_NR_Beta
                                  (0.5292641686,    0.0940103059,   0.2186262218,   0.9583027845,
                                  -0.2880424830,   -0.1908113865);       *** US Born Hisp lnRR;

         array  FRHspnc_Beta (JJ)  FRH_BI_Beta      FRH_AM_Beta     FRH_AF_Beta     FRH_FR_Beta
                                   FRH_AC_NB_Beta   FRH_AF_NR_Beta
                                  (0.5292641686,    0.0940103059,   0.2186262218,   0.9583027845,
                                  -0.2880424830,   -0.1908113865);       *** Foreign Born Hisp lnRR;

         array  Beta_Name  (JJ) $ CBeta1-CBeta6 ('Biopsy', 'AgeMen', 'AgeFst',  'FstRel'
                                                 'A50*NB', ' AF*NR');
*/

         array  USHspnc_Beta (JJ)  USH_BI_Beta      USH_AM_Beta     USH_AF_Beta     USH_FR_Beta
                                   USH_AC_NB_Beta   USH_AF_NR_Beta
                                  (0.0970783641,    0.0000000000,    0.2318368334,   0.166685441,
                                   0.0000000000,    0.0000000000);       *** US Born Hisp lnRR;

         array  FRHspnc_Beta (JJ)  FRH_BI_Beta      FRH_AM_Beta     FRH_AF_Beta     FRH_FR_Beta
                                   FRH_AC_NB_Beta   FRH_AF_NR_Beta
                                  (0.4798624017,    0.2593922322,   0.4669246218,   0.9076679727,
                                   0.0000000000,    0.0000000000);       *** Foreign Born Hisp lnRR;

         array  Beta_Name  (JJ) $ CBeta1-CBeta6 ('Biopsy', 'AgeMen', 'AgeFst',  'FstRel'
                                                 'A50*NB', ' AF*NR');

         JJ  = .;
         Key = 1;
   run;


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

/*  attributable risk from NCI BrCa Risk Disk

         array  USHspnc_1_AR (KK)  USH_1_AR_1  USH_1_AR_2    (0.5788413,      0.5788413);
         array  FRHspnc_1_AR (KK)  FRH_1_AR_1  FRH_1_AR_2    (0.5788413,      0.5788413);
*/

         array  USHspnc_1_AR (KK)  USH_1_AR_1  USH_1_AR_2    (0.749294788397, 0.778215491668);
         array  FRHspnc_1_AR (KK)  FRH_1_AR_1  FRH_1_AR_2    (0.428864989813, 0.450352338746);

         KK  = .;
         Key = 1;
   run;




   ***   The following 5 data steps, gathers all the needed parameters/constants
         and merges them into the input data set 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;
   merge TmpFile  FofT;
   by    Key;
   run;


   data  TmpFile;
   set   TmpFile;

         array  FB_Hspnc_H1  (II)  FBH1_1-FBH1_14;
         array  USB_Hspnc_H1 (II) USBH1_1-USBH1_14;

         array  FB_Hspnc_H2  (II)  FBH2_1-FBH2_14;
         array  USB_Hspnc_H2 (II) USBH2_1-USBH2_14;


         array  Ages         (II)      A1-A14;
         array  AgesP5       (II)     AP1-AP14;

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


         array  USHspnc_Beta (JJ) USH_BI_Beta      USH_AM_Beta     USH_AF_Beta     USH_FR_Beta
                                  USH_AC_NB_Beta   USH_AF_NR_Beta;

         array  FRHspnc_Beta (JJ) FRH_BI_Beta      FRH_AM_Beta     FRH_AF_Beta     FRH_FR_Beta
                                  FRH_AC_NB_Beta   FRH_AF_NR_Beta;

         array  Beta_Name  (JJ) $ CBeta1-CBeta6;


         *** 1-Attributable Risk = F(t);
         array  USHspnc_1_AR (KK)  USH_1_AR_1  USH_1_AR_2;
         array  FRHspnc_1_AR (KK)  FRH_1_AR_1  FRH_1_AR_2;


         *** 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;                            *** [20:25);
         array  WrkH1_2    (I5)  W1_02_1-W1_02_5;                            *** [25:30);
         array  WrkH1_3    (I5)  W1_03_1-W1_03_5;                            *** [30:35);
         array  WrkH1_4    (I5)  W1_04_1-W1_04_5;                            *** [35:40);
         array  WrkH1_5    (I5)  W1_05_1-W1_05_5;                            *** [40:45);
         array  WrkH1_6    (I5)  W1_06_1-W1_06_5;                            *** [45:50);
         array  WrkH1_7    (I5)  W1_07_1-W1_07_5;                            *** [50:55);
         array  WrkH1_8    (I5)  W1_08_1-W1_08_5;                            *** [55:60);
         array  WrkH1_9    (I5)  W1_09_1-W1_09_5;                            *** [60:65);
         array  WrkH1_10   (I5)  W1_10_1-W1_10_5;                            *** [65:70);
         array  WrkH1_11   (I5)  W1_11_1-W1_11_5;                            *** [70:75);
         array  WrkH1_12   (I5)  W1_12_1-W1_12_5;                            *** [75:80);
         array  WrkH1_13   (I5)  W1_13_1-W1_13_5;                            *** [80:85);
         array  WrkH1_14   (I5)  W1_14_1-W1_14_5;                            *** [85:90);

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


         array  WrkH2_1    (I5)  W2_01_1-W2_01_5;                            *** [20:25);
         array  WrkH2_2    (I5)  W2_02_1-W2_02_5;                            *** [25:30);
         array  WrkH2_3    (I5)  W2_03_1-W2_03_5;                            *** [30:35);
         array  WrkH2_4    (I5)  W2_04_1-W2_04_5;                            *** [35:40);
         array  WrkH2_5    (I5)  W2_05_1-W2_05_5;                            *** [40:45);
         array  WrkH2_6    (I5)  W2_06_1-W2_06_5;                            *** [45:50);
         array  WrkH2_7    (I5)  W2_07_1-W2_07_5;                            *** [50:55);
         array  WrkH2_8    (I5)  W2_08_1-W2_08_5;                            *** [55:60);
         array  WrkH2_9    (I5)  W2_09_1-W2_09_5;                            *** [60:65);
         array  WrkH2_10   (I5)  W2_10_1-W2_10_5;                            *** [65:70);
         array  WrkH2_11   (I5)  W2_11_1-W2_11_5;                            *** [70:75);
         array  WrkH2_12   (I5)  W2_12_1-W2_12_5;                            *** [75:80);
         array  WrkH2_13   (I5)  W2_13_1-W2_13_5;                            *** [80:85);
         array  WrkH2_14   (I5)  W2_14_1-W2_14_5;                            *** [85:90);

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


         array  USWrk_Beta (JJ)  USBeta1-USBeta6;               *** beta for US      Born;
         array  FRWrk_Beta (JJ)  FRBeta1-FRBeta6;               *** beta for Foreign Born;
         array    Wrk_Beta (JJ)    Beta1-Beta6;                 *** wrking betas;

         array    Wrk_1_AR (KK)  One_AR1 One_AR2;               *** wrking 1-AR for this record;


         ***  initialize rate vectors with the correct rates for each women under study;
         do II = 1 to 14;
            if      (US_Brn eq 1) then do;
               tmp1 = USB_Hspnc_H1;     *** brca incidence for the 5yr age cat;
               tmp2 = USB_Hspnc_H2;     *** competing hazs for the 5yr age cat;
            end;
            else if (US_Brn eq 0) then do;
               tmp1 =  FB_Hspnc_H1;     *** brca incidence for the 5yr age cat;
               tmp2 =  FB_Hspnc_H2;     *** competing hazs for the 5yr age cat;
            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;

         do JJ = 1 to 6;                                                    *** ln RR;
            if      (US_Brn eq 1)  then  Wrk_Beta = USHspnc_Beta;
            else if (US_Brn eq 0)  then  Wrk_Beta = FRHspnc_Beta;
         end;

         do KK = 1 to 2;                                                    *** 1- AR;
            if      (US_Brn eq 1)  then  Wrk_1_AR = USHspnc_1_AR;
            else if (US_Brn eq 0)  then  Wrk_1_AR = FRHspnc_1_AR;
         end;
   run;




   *** document all constants;
   data  Dummy;
   set   TmpFile;

         file print;

         array  USHspnc_Beta (JJ)  USH_BI_Beta      USH_AM_Beta     USH_AF_Beta     USH_FR_Beta
                                   USH_AC_NB_Beta   USH_AF_NR_Beta;

         array  FRHspnc_Beta (JJ)  FRH_BI_Beta      FRH_AM_Beta     FRH_AF_Beta     FRH_FR_Beta
                                   FRH_AC_NB_Beta   FRH_AF_NR_Beta;

         array  Beta_Name (JJ) $   CBeta1-CBeta6;

         array  USHspnc_1_AR (KK)  USH_1_AR_1      USH_1_AR_2;
         array  FRHspnc_1_AR (KK)  FRH_1_AR_1      FRH_1_AR_2;


         if (_N_ eq 1) then do;

            put "# of boostrap samples requested for this projection is      &Numboots";
            put "# of boostrap samples requested must be equal to    0  or  between 30  and  10,000";
            put " ";
            put "If # of bootstrap samples is greater or equal to   30 StdErr are";
            put "estimated from the boostrap distribution";
            put " ";
            put "If # of bootstrap samples is greater or equal to   30 and ";
            put "                             less  than equal to  999 95% CI are";
            put "estimated as AbsRisk +- 1.96*StdErr";
            put " ";
            put "If # of bootstrap samples is greater or equal to 1000 95% CI are ";
            put "estimated from the bootstrap empirical distribution";
            put " ";
            put " ";

            put "|-------------- US Born -------------|       |-------------- Foreign -------------|";

            put " ";
            put "  Beta       lnRR     AgeGrp      1-AR         Beta       lnRR     AgeGrp      1-AR";


            put " ";

        *** do JJ = 1 to 6;        *** code for NCI Risk Disk;
            do JJ = 1 to 4;
               if      (JJ eq 1) then put  Beta_Name  $char6.
                                           USHspnc_Beta   11.6
                                          "    Age <50"
                                           USH_1_AR_1     10.6

                                          "       "
                                           Beta_Name  $char6.
                                           FRHspnc_Beta   11.6
                                          "    Age <50"
                                           FRH_1_AR_1     10.6;

               else if (JJ eq 2) then put  Beta_Name  $char6.
                                           USHspnc_Beta   11.6
                                          "    Age>=50"
                                           USH_1_AR_2     10.6

                                          "       "
                                           Beta_Name  $char6.
                                           FRHspnc_Beta   11.6
                                          "    Age>=50"
                                           FRH_1_AR_2     10.6;

               else if (JJ ge 3) then put  Beta_Name  $char6.
                                           USHspnc_Beta   11.6
                                          "                     "
                                          "       "
                                           Beta_Name  $char6.
                                           FRHspnc_Beta   11.6;
            end;
         end;


         array  FB_Hspnc_H1  (II)  FBH1_1-FBH1_14;
         array  USB_Hspnc_H1 (II) USBH1_1-USBH1_14;

         array  FB_Hspnc_H2  (II)  FBH2_1-FBH2_14;
         array  USB_Hspnc_H2 (II) USBH2_1-USBH2_14;


         array  Ages         (II)      A1-A14;
         array  AgesP5       (II)     AP1-AP14;

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

         if (_N_ eq 1) then do;
            put " ";
            put "               BrCa          BrCa                          BrCa          BrCa";
            put "          Incidence     CompetRsk                     Incidence     CompetRsk";
            put "            SEER CA       SEER CA                       SEER CA       SEER CA";
            put "    5yr   1995:2004     1995:2004                     1995:2004     1995:2004";
            put " AgeGrp    Hispanic      Hispanic                      Hispanic      Hispanic";
            put " ";

            do II = 1 to 14;
               put "[" Ages 2.0 ":"  AgesP5 2.0 ")"
                    USB_Hspnc_H1  12.7    USB_Hspnc_H2  14.7
                   "                  "
                    FB_Hspnc_H1   12.7    FB_Hspnc_H2   14.7;
            end;
         end;


      *** program aborts when only 1 bootstrap resampling specified;

      NBoots = &NumBoots;

      if (NBoots gt 0 and NBoots le 29) then do;
         file print;
         put " ";
         put " ";
         put "**********   # of bootstrap replications specified for this run is " NBoots 3.0;
         put "**********   Program is aborting because less than 30 bootstraps repetitions specified";
         put "**********   Valid number of bootstrap repetitions to be specified is 0 or between 30 and 10,000";
         abort;

      title2  " ";
   end;

   run;



   *** BrCa risk projection;

   *** libname ThisPath  '.';


   %let  NumBetas = %eval(&Numboots+1);   *** NumBetas = Original Beta + # of boots Betas;

   ***   US Born Hispanics - Input orignal Beta and Bootstrap Betas;

   data  AgeMen_Beta (keep = AgeMen_Beta)
         Age1st_Beta (keep = Age1st_Beta)
         FamHst_Beta (keep = FamHst_Beta)
         Biopsy_Beta (keep = Biopsy_Beta)
         One_AR1     (keep = One_AR1    )
         One_AR2     (keep = One_AR2    )
         XBoot       (keep = xBoot      );

         ***    set   ThisPath.hispusb_lnrr_1_ar;
         infile 'HispUSB_lnRR_1_AR.fil'   firstobs=5;

         input   AgeMen_Beta
                 Age1st_Beta
                 FamHst_Beta
                 Biopsy_Beta
                 One_AR1
                 One_AR2
                 xBoot;

         xBoot = xBoot + 1;

   ***;  if (_n_ le &NumBetas);          *** for selecting # of boots;

         output AgeMen_Beta;
         output Age1st_Beta;
         output FamHst_Beta;
         output Biopsy_Beta;
         output One_AR1    ;
         output One_AR2    ;
         output XBoot      ;
   run;

   proc  transpose  data=AgeMen_Beta
                     out=AgeMen_Beta_wide
                     prefix=USB_AM_;
   run;

   proc  transpose  data=Age1st_Beta
                     out=Age1st_Beta_wide
                     prefix=USB_A1_;
   run;

   proc  transpose  data=FamHst_Beta
                     out=FamHst_Beta_wide
                     prefix=USB_FH_;
   run;

   proc  transpose  data=Biopsy_Beta
                     out=Biopsy_Beta_wide
                     prefix=USB_BI_;
   run;

   proc  transpose  data=One_AR1
                     out=One_AR1_wide
                     prefix=USB_AR1_;
   run;

   proc  transpose  data=One_AR2
                     out=One_AR2_wide
                     prefix=USB_AR2_;
   run;

   proc  transpose  data=XBoot
                     out=XBoot_wide
                     prefix=BT_;
   run;


   data  USB_LNrr_1_AR_wide (drop=_name_);
   merge AgeMen_Beta_wide
         Age1st_Beta_wide
         FamHst_Beta_wide
         Biopsy_Beta_wide
         One_AR1_wide
         One_AR2_wide
         XBoot_wide;

         key = 1;
   run;


***proc  print      data=USB_LNrr_1_AR_wide;
***run;


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





   ***   Foreign Born Hispanics - Input orignal Beta and Bootstrap Betas;

   data  AgeMen_Beta (keep = AgeMen_Beta)
         Age1st_Beta (keep = Age1st_Beta)
         FamHst_Beta (keep = FamHst_Beta)
         Biopsy_Beta (keep = Biopsy_Beta)
         One_AR1     (keep = One_AR1    )
         One_AR2     (keep = One_AR2    )
         XBoot       (keep = xBoot      );

         ***    set   ThisPath.hispforeign_lnrr_1_ar;
         infile 'HispForeign_lnRR_1_AR.fil'   firstobs=5;

         input   AgeMen_Beta
                 Age1st_Beta
                 FamHst_Beta
                 Biopsy_Beta
                 One_AR1
                 One_AR2
                 xBoot;

         xBoot = xBoot + 1;

   ***;  if (_n_ le &NumBetas);          *** for selecting # of boots;

         output AgeMen_Beta;
         output Age1st_Beta;
         output FamHst_Beta;
         output Biopsy_Beta;
         output One_AR1    ;
         output One_AR2    ;
         output XBoot      ;
   run;

   proc  transpose  data=AgeMen_Beta
                    out=AgeMen_Beta_wide
                    prefix=FRB_AM_;
   run;

   proc  transpose  data=Age1st_Beta
                    out=Age1st_Beta_wide
                    prefix=FRB_A1_;
   run;

   proc  transpose  data=FamHst_Beta
                    out=FamHst_Beta_wide
                    prefix=FRB_FH_;
   run;

   proc  transpose  data=Biopsy_Beta
                    out=Biopsy_Beta_wide
                    prefix=FRB_BI_;
   run;

   proc  transpose  data=One_AR1
                    out=One_AR1_wide
                    prefix=FRB_AR1_;
   run;

   proc  transpose  data=One_AR2
                    out=One_AR2_wide
                    prefix=FRB_AR2_;
   run;

   proc  transpose  data=XBoot
                    out=XBoot_wide
                    prefix=FRB_BT_;
   run;


   data  FRB_LNrr_1_AR_wide (drop=_name_);
   merge AgeMen_Beta_wide
         Age1st_Beta_wide
         FamHst_Beta_wide
         Biopsy_Beta_wide
         One_AR1_wide
         One_AR2_wide;
   ***   XBoot_wide;

         key = 1;
   run;


***proc  print      data=FRB_LNrr_1_AR_wide;
***run;


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


   data   TmpFile;
   merge  TmpFile
          USB_LNrr_1_AR_wide
          FRB_LNrr_1_AR_wide;
   by     key;

          array  USB_BI_Vec   (Ib)    USB_BI_1-USB_BI_&NumBetas;
          array  USB_AM_Vec   (Ib)    USB_AM_1-USB_AM_&NumBetas;
          array  USB_AF_Vec   (Ib)    USB_A1_1-USB_A1_&NumBetas;
          array  USB_FR_Vec   (Ib)    USB_FH_1-USB_FH_&NumBetas;

          array  USB_AR1_Vec  (Ib)    USB_AR1_1-USB_AR1_&NumBetas;
          array  USB_AR2_Vec  (Ib)    USB_AR2_1-USB_AR2_&NumBetas;


          array  FRB_BI_Vec   (Ib)    FRB_BI_1-FRB_BI_&NumBetas;
          array  FRB_AM_Vec   (Ib)    FRB_AM_1-FRB_AM_&NumBetas;
          array  FRB_AF_Vec   (Ib)    FRB_A1_1-FRB_A1_&NumBetas;
          array  FRB_FR_Vec   (Ib)    FRB_FH_1-FRB_FH_&NumBetas;

          array  FRB_AR1_Vec  (Ib)    FRB_AR1_1-FRB_AR1_&NumBetas;
          array  FRB_AR2_Vec  (Ib)    FRB_AR2_1-FRB_AR2_&NumBetas;

          array  XBoot_Vec    (Ib)    BT_1-BT_&NumBetas;

          array  AbsRsk_Vec   (Ib_1)  AbsRsk_1-AbsRsk_&NumBoots;


          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    W1_09_1-W1_09_5
                               W1_10_1-W1_10_5    W1_11_1-W1_11_5    W1_12_1-W1_12_5
                               W1_13_1-W1_13_5    W1_14_1-W1_14_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    W2_09_1-W2_09_5
                               W2_10_1-W2_10_5    W2_11_1-W2_11_5    W2_12_1-W2_12_5
                               W2_13_1-W2_13_5    W2_14_1-W2_14_5;


          array    Wrk_Beta (JJ)    Beta1-Beta6;                 *** wrking betas;
          array    Wrk_1_AR (KK)  One_AR1 One_AR2;               *** 1-AR for this record;


          *** (1-AR)*RR  for  ages  20,21...49,50,51...90;
          array  One_AR_RR (j_intvl)  One_AR_RR_01-One_AR_RR_70;

          array PI_j(j_intvl) Pi1-Pi70;

 ***
          <----- slow ----- fast ------> (changes)
          Biopsy, AgeMen, Age1st, FstRels
          BI_Cat, AM_Cat, AF_Cat, FR_Cat

               2*      3*      4*      2=   48 patterns

           BI_Cat  2 levels      2*3*4*2 =  48 patterns
           AM_Cat  3 levels        3*4*2 =  24 patterns
           AF_Cat  4 levels          4*2 =   8 patterns
           FR_Cat  2 levels            2 =   2 patterns

          PattrnNumber = BI_Cat * 24  +
                         AM_Cat *  8  +
                         AF_Cat *  2  +
                         FR_Cat *  1  +  1
 *** ;


          PattrnNumber = BI_Cat * 24  +
                         AM_Cat *  8  +
                         AF_Cat *  2  +
                         FR_Cat *  1  +  1;


/*
&T1    = 48;
&T2    = 53;
US_Brn =  0;

BI_Cat =  0;
AM_Cat =  2;
AF_Cat =  1;
FR_Cat =  0;
*/


          do Ib = 1 to &NumBetas;         *** Bootstrap loop.  Ib=1 is original data  Ib>1 are bootstrapped data;

             Ib_1 = Ib - 1;

             if      (US_Brn eq 1)  then  do;   *** setting Beta=lnRR and 1-AR to US values for US born women;
                Beta1   = USB_BI_Vec;
                Beta2   = USB_AM_Vec;
                Beta3   = USB_AF_Vec;
                Beta4   = USB_FR_Vec;

                Beta5   = 0.0000000000;
                Beta6   = 0.0000000000;

                One_AR1 = USB_AR1_Vec;
                One_AR2 = USB_AR2_Vec;
             end;
             else if (US_Brn eq 0)  then  do;   *** setting Beta=lnRR and 1-AR to foreign values for foreign born women;
                Beta1   = FRB_BI_Vec;
                Beta2   = FRB_AM_Vec;
                Beta3   = FRB_AF_Vec;
                Beta4   = FRB_FR_Vec;

                Beta5   = 0.0000000000;
                Beta6   = 0.0000000000;

                One_AR1 = FRB_AR1_Vec;
                One_AR2 = FRB_AR2_Vec;
             end;


             LP1  = BI_Cat       * Beta1 +
                    AM_Cat       * Beta2 +
                    AF_Cat       * Beta3 +
                    FR_Cat       * Beta4 +
                    log(R_Hyp);              *** Linr Prdctr woman of interest at ages lt 50;

             LP2  = LP1;                     *** Linr Prdctr woman of interest at ages ge 50;



*** NCIRisk  LP1  = BI_Cat       * Beta1 +
                    AM_Cat       * Beta2 +
                    AF_Cat       * Beta3 +
                    FR_Cat       * Beta4 +
                    AF_Cat*FR_Cat* Beta6 +
                    log(R_Hyp);              *** Linr Prdctr woman of interest at ages lt 50
                                                 For NCI Breast Cancer Risk Disk;

*** NCIRisk  LP2  = LP1                  +
                    BI_Cat       * Beta5;    *** Linr Prdctr woman of interest at ages ge 50
                                                 For NCI Breast Cancer Risk Disk;


             RRStar1    = exp(LP1);               *** RRstar woman of interest at ages lt 50;
             RRStar2    = exp(LP2);               *** RRstar woman of interest at ages ge 50;


             iloop = 1;

             *** for a research tool, all we want is to find the absolute risk for each woman based
                 on her current age, projection age and relative risk covariate values.  we are not
                 interested in risk for the average women or lifetime risk for this women

                 That is why iloop is ONLY tested to be <= to 1.  the loop which returns abs risk;

             do while (iloop <= 1);               *** iloop = 1, do       abs risk
                                                            = 2, do avg   abs risk
                                                            = 3, LifeTime abs risk
                                                            = 4, LT  avg  abs risk;

                if      (iloop = 1) then do;
                   One_AR_RR1 = One_AR1 * RRStar1;       ***(1-AR)*rr=(1-AR)*exp(xstar*) age lt 50;
                   One_AR_RR2 = One_AR2 * RRStar2;       ***(1-AR)*rr=(1-AR)*exp(xstar*) age ge 50;
                end;
                else if (iloop = 2) then do;
                   One_AR_RR1 =                 1;       ***(1-AR)*rr=1         rr set to 1;
                   One_AR_RR2 =                 1;       ***(1-AR)*rr=1       1-AR set to 1;
                end;
                else if (iloop = 3) then do;
                   &T2        = 90;
                   One_AR_RR1 = One_AR1 * RRStar1;       ***(1-AR)*rr=(1-AR)*exp(xstar*) age lt 50;
                   One_AR_RR2 = One_AR2 * RRStar2;       ***(1-AR)*rr=(1-AR)*exp(xstar*) age ge 50;
                end;
                else if (iloop = 4) then do;
                   &T2        = 90;
                   One_AR_RR1 =                 1;       ***(1-AR)*rr=1         rr set to 1;
                   One_AR_RR2 =                 1;       ***(1-AR)*rr=1       1-AR set to 1;
                end;


                do j_intvl = 1 to 70;
                   if      (j_intvl le 30) then One_AR_RR = One_AR_RR1;
                   else if (j_intvl gt 30) then One_AR_RR = One_AR_RR2;
                end;


/*
file  print;
put  " ";
PUT  "T1        T2      US_Born       "  &T1          14.0  &T2      14.0  US_Brn 14.0;
put  "BI_Cat    AM_Cat  AF_Cat  FR_Cat"  BI_Cat       14.0  AM_Cat   14.0  AF_Cat 14.0  FR_Cat 14.0;
put  "Beta1     Beta2   Beta3   Beta4 "  Beta1        14.7  Beta2    14.7  Beta3  14.7  Beta4  14.7;
put  "LP1       LP2                   "  LP1          14.7  LP2      14.7;
put  "RRStar1   RRStar2               "  RRStar1      14.7  RRStar2  14.7;
put  "One_AR1   One_AR2               "  One_AR1      14.7  One_Ar2  14.7;
put  "One_AR_RR                       "  One_AR_RR_01 14.7  One_AR_RR_29 14.7  One_AR_RR_30 14.7
                                         One_AR_RR_31 14.7  One_AR_RR_32 14.7  One_AR_RR_70 14.7;
*/


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

                Strt_Intvl = floor(&T1) - 20 + 1;     *** example:  T1=21 T2=23, Strt_Intvl=21-20+1= 2;
                Endd_Intvl =  ceil(&T2) - 20 + 0;     ***                        EndD_Intvl=23-20+0= 3;
                NumbrIntvl =  ceil(&T2) - floor(&T1); ***                        NumbrIntvl=23-21  = 2;

                if      (Error_Ind eq 1) then
                   RskWrk = .;                                      *** erroronous input record
                                                                        set abs risk to missing;

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


/*
put " ";
put "NumbrIntvl Strt_Intvl           " NumbrIntvl 14.0  Strt_Intvl 14.0;
put " ";
*/


                   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));
                      RskWrk =  RskWrk + PI_j;
                      Cum_H  =  Cum_H  + Hj*IntgrlLngth;

/*
put "jj       j_intvl     IntgrlLngth" jj 14.0  j_intvl 14.0  IntgrlLngth 14.0;
put "Hj      h1       ONE_AR_RR    h2"  Hj 14.7  h1 14.7  ONE_AR_RR 14.7  h2 14.7;
put "PI_j    RskWrk   Cum_H          "  PI_j 14.7  RskWrk 14.7  Cum_H 14.7;
*/

                   end;
                end;

***             Key    = 1;
***abort;

                RskWrk = 100* RskWrk;


                if      (iloop eq 1) then do;
                   if      (Ib gt 1) then      AbsRsk_Vec = RskWrk;
                   else if (Ib eq 1) then do;
                                              &AbsRsk     = RskWrk;   *** setting original data set abs risk as the returned value;
                                              &RR_Star1   = RRStar1;
                                              &RR_Star2   = RRStar2;
                                          end;
                end;

***             else if (iloop eq 2) then do;
***                &AbsRskAvg = RskWrk;
***             end;


                if (&NumBoots eq 41) then do;           *** code for testing  #betas=42,  #boots=41;
                   if (_n_ lt 2 and Ib le 42) then do;
                      file print;
                      put " ";
                      put "    ID # "        _n_  9.0   "     Iboot "         Ib 9.0;
                      put "     LP1 "        LP1  9.6   "       LP2 "        LP2 9.6;
                      put " RRStar1 "    RRStar1  9.6   "   RRStar2 "    RRStar2 9.6;
                      put " One_AR1 "    One_AR1  9.6   "   One_AR2 "    One_AR2 9.6;
                      put "1_ar1_r1 " One_AR_RR1  9.6   "  1_ar2_r2 " One_AR_RR2 9.6;
                      if      (Ib gt 1) then
                      put "Abs Risk "     RskWrk  9.6   " AbsRskVec " AbsRsk_Vec 9.6;
                      else if (Ib eq 1) then
                      put "Abs Risk "     RskWrk  9.6;
                   end;
                end;

                iloop = iloop + 1;
             end;

             Key    = 1;

             label Beta1         =  "ln RR of biopsy indicator"
                   Beta2         =  "ln RR of age at menarchy"
                   Beta3         =  "ln RR of age at 1st live birth"
                   Beta4         =  "ln RR of 1st degree rel with BrCa"

                   Beta5         =  "ln RR of age1st*1stdegRel"
                   Beta6         =  "ln RR of biopsy*age ge 50"

                   R_Hyp         =  "RR multiplicative factor for Hyperplasia"

                   One_AR1       =  "1-Attributable risk age lt 50"
                   One_AR2       =  "1-Attributable risk age ge 50"

                  &RR_Star1      =  "BrCa Relative risk  age lt 50"
                  &RR_Star2      =  "BrCa Relative risk  age ge 50"

                   One_AR_RR1    =  "(1-AR)*RelRsk   Age lt 50"
                   One_AR_RR2    =  "(1-AR)*RelRsk   Age ge 50"

                  &AbsRsk        =  "Absolute Risk of Breast Cancer"
                  &SD_AR         =  "Standard deviation of Absolute Risk"
                  &LowrCL        =  "95% Lower confidence level of Absolute Risk"
                  &UpprCL        =  "95% Upper confidence level of Absolute Risk";

          end;                                                   *** end of bootstrap loop;


          *** StdErr estimation;
                                             &SD_AR = .;
          if      (&Numboots ge 30) then     &SD_AR = std (of AbsRsk_Vec(*));


          *** 95% CI estimation;

          if      (&Numboots le  29) then do;       *** 95%CI not estimated;
             &LowrCL = .;
             &UpprCL = .;
              loIndx = .;
              hiIndx = .;
          end;
          else if (&Numboots ge  30) then do;       *** 95%CI     estimated;

             call sortn(of AbsRsk_Vec(*));          *** sorting the bootstrapped AbsRisks;

***          if                           (&Numboots ge   41) then do;   *** code for testing;

             if                           (&Numboots ge 1000) then do;
                Ib_1   = floor(&Numboots*0.025);
                loIndx = Ib_1;
               &LowrCL = AbsRsk_Vec;

                Ib_1   = ceil(&Numboots*0.975);
                hiInDx = Ib_1;
               &UpprCL = AbsRsk_Vec;
             end;
             else if (&Numboots ge  30 and &Numboots le  999) then do;
               &LowrCL = &AbsRsk - 1.96*&SD_AR;
               &UpprCL = &AbsRsk + 1.96*&SD_AR;
                loIndx = .;
                hiIndx = .;
             end;
          end;

          if (_n_ le -9999999999) then do;
             file print;
             if (_n_ eq 1) then
             put "                           AbsRisk       AbsRisk" /
                 "   WID       AbsRisk       loLimit       hiLimit    loIndx    hiIndx" ;
             put &WID     6.0
                 &AbsRsk 14.10
                 &LowrCL 14.10
                 &UpprCL 14.10
                  loIndx 10.0
                  hiIndx 10.0;
          end;
      run;


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

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


   ***   Error listing file when errors detected as well as writing out a sas file which
         keeps the abs risk, std abs risk, 95% CI as well as all the original input variables;

      data &Out_File (keep = &WID      &T1    &T2        &Biopsy    &HyperP  &AgeMen  &Age1st  &FstRels
                             &US_Born  R_Hyp  &RR_Star1  &RR_Star2  &AbsRsk  &SD_AR   &LowrCL  &UpprCL
                             CaseCtrl  ProjtnIntrvl);

      set   TmpFile;
      by    Key;

            retain  Count_Error  0;

            file print;

            if (_N_ eq 1) then do;
               put " ";
               put "     ID             #Boot     #  Hypr  Hypr  Age  Age    #  USBorn"
                   "        RR        RR               ";
               put "      #   T1   T2  Sample  Biop  plas    RR  Men  1st  Rel     Ind"
                   "    Age<50   Age>=50      AbsRsk(%)";

               put " ";
            end;

            Tmp_NumBoots = &NumBetas - 1;

            if (Error_Ind eq 1) then do;

              put &WID            7.0

                  &T1             5.1
                  &T2             5.1
                   Tmp_NumBoots   8.0

                 &Biopsy          6.0
                 &HyperP          6.0
                  R_Hyp           6.2

                 &AgeMen          5.0
                 &Age1st          5.0
                 &FstRels         5.0

                 &US_Born         8.0

                 &RR_Star1       10.2
                 &RR_Star2       10.2
                 &AbsRsk         12.0;

               put
                 "       "
                  set_T1_Missing  5.1
                  set_T2_Missing  5.1
                  NBoots          8.0

                  BI_Cat          6.0
                 "            "

                  AM_Cat          5.0
                  AF_Cat          5.0
                  FR_Cat          5.0

                  US_Brn          8.0;
               put " ";

               Count_Error = Count_Error + 1;
            end;

            if (last.Key and Count_Error eq 0) then do;
               put " ";
               put "      No errors detected for initial age, projection age "
                   "and relative risk covaraites";
               put "      ---------------------------------------------------"
                   "----------------------------";
               put "      |                                                  "
                   "                           |";
               put "      |                                                  "
                   "                           |";
               put " ";
               put "   ";
            end;

       title4  "Error listing for the input file";
   run;


***      proc contents data=&Out_File  position;
***      run;


%mend    HispBrCa_RAM_Ver2_1;          *** end of SAS macro to perform abs risk projection.
                                           End of restricted code area, where changes or
                                           modifications to the code should be avoided;
;


