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

AABrCa_RAM    SAS macro for projecting African-American Women Absolute Risk of Breast
              Cancer with 95% CI based on the Gail Model RR estimates from the CARE study
              
             "Projecting Individualized Absolute Invasive Breast Cancer Risk in African 
              American Women"   Gail, Costantino, Pee et al.   JNCI 2007  vol 99:1782-92 

NCI African-American Women BrCa Risk Assessment Macro          Version 1.0       09/29/10

SAS macro to project for African-American women absolute risk and 95% CI based on CARE
data Gail RR model.  1-AR, composite breast cancer incidences, competing hazards handling
of missing covariate values and covariate editing procedures follow NCI BrCa Risk 
Assessment Tool (NCI BCRAT).


This program consists of two parts:

(1)  Readme section (the next 250 plus lines of comments) describes:

        o required input variables,
        o valid values for input variables,
        o restrictions  on input variables,
        o error indicator,
        o recoding rules for relative risk covariates,
        o structure of the sas macro,
        o listing of a sample input data set,
        o listing of the output data set obtained by applying the macro to the sample
          input data set.

(2)  The sas macro itself starting after the Readme section and which consists of 
     roughly 700 lines of sas code.




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 current age, projection age,  BrCa
relative risk covariates and race.  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 contains the 
projected absolute risk of BrCa, 95% CI as well as the original input data items.


Macro input structure:
---------------------
                       Required
        Macro          Macro                        "points" to SAS data files or
        name           parameters                               SAS variables
        
%macro  AABrCa_RAM    (In_File     =,                name of input  sas data set

                       Out_File    =,                name of output sas data set

                       T1          =,                current age,    age at beginning of
                                                                     projection interval

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

                       NBiops      =,                # biopsies performed
                       HyperPlas   =,                atypical hyperplasia?

                       AgeMen      =,                age at menarche
                       NumRel      =,                # 1st degree relatives with brca
                       
                       Race        =,                race of woman

                       AbsRsk      =,                projected absolute risk of brca
                       se_AbsRsk   =,                se of absolute risk
                       Lo_95CI     =,                lower 95% confidence interval
                       Hi_95CI     =,                upper 95% confidence interval
                       
                       Error_Ind   =,                error indicator for observation
                       IDD         =);               woman's ID    

appropriate sas file/sas variable names must be associated with all macro parameters  
upon the invocation of the sas macro  "AABrCa_RAM".

For example by coding  "In_File = "AfAM_RR"  tells the macro that the user created
sas file  "AfAM_RR"  is to be used for input of variables.  Similarly, coding
"NBiops = Num_Biops",  lets the macro know that the sas variable "Num_Biops" in the sas
input file  "AfAM_RR"  contiains the count of the # of biopsies performed.

To involke the sas macro in your sas program, an "#include" statement must be coded in 
your sas program, which points to the directory where the sas macro is stored.  

For example:

the statement: %include "AABrCa_RAM";                includes the sas macro  AABrCa_RAM
                                                     residing in your current directory

the statement: %include "c:\sas.macro\AABrCa_RAM";   includes the sas macro  AABrCa_RAM
                                                     stored in the directory c:\sas.macro





Input data items needed to project for BrCa absolute risk and consistency requirements:

Macro
parameter      Definition                    Valid values

T1             Current         age           all real numbers T1 in [20,85]
T2             BrCa projection age           all real numbers T2 such that T1 < T2 <= 90

NBiops         # of biopsies                 0,1,2 ...       99=unk (99 recoded to 0)

HyperPlasia    Did biopsy display            0=no,  1=yes,   99=unk or no biopsy
               atypical hyperplasia?

AgeMen         Age at menarche               integer age less than or equal to T1, 
                                             99(unk,  recoded to 0)
                                             

NumRel         # 1st degree relatives        0,1,2 ...       99=unk (99 recoded to 0)
               with BrCa

Race           Race                          1=white 1983-87 SEER rates,
                                             2=black,
                                             3=hispanc,
                                             4=other
                                             5=white 1995-03 SEER rates


Recoding and checking of relative risk covariate values performed in this program:

                                     raw value                       recoded to

NBiops: # biopsies                   0 or 99(unk)                             0
                                     1                                        1
                                     2,3,4 ... 98                             2


AgeMen: age at menarche              14,15,16 ... 99(unk)                     0
                                     13 and younger                           1
                                     


N_Rels: # 1st degree rel with BrCa   0 or 99(unk)                             0
                                     1                                        1
                                     2,3,4 ... 98                             2


Consistency checks for T1, T2, AgeMen and Race :

20 <= T1 < T2 <= 90,                  OTHERWISE abs risk = sas missing value of  "."
AgeMen <= T1 and AgeMen ne 99(unk),   OTHERWISE abs risk = sas missing value of  "."
if Race not 2,                                  abs risk = sas missing value of  "."


Consistency patterns for  # of Biopsies and Hyperplasia:

NBiops = 0  or   99  then  Hyperplasia  MUST = 99(not applicable)
NBiops > 0 and < 99  then  Hyperplasia       =  0, 1 or 99(unk)
            
if ANY of the above 2 REQUIREMENTS are violated, the absolute risk will be set  
to the sas missing value ".".  

The consequences to the relative risk (RR) of the above two requirements is:

(A) # biopsies = 0 or   99  &  Hyperplasia  =99 (not applcbl)inflate  RR  by  1.00

(B) # biopsies > 0 and <99  &  Hyperplasia  = 0 ( no hyprpl) inflate  RR  by  0.93
                                            = 1 (yes hyprpl) inflate  RR  by  1.82
                                            =99 (unk hyprpl) inflate  RR  by  1.00
                                          

Edit checking for N_Rels:

N_Rels:  # of 1st degree relatives with BrCa must be the non-negative integers 
         0,1,2...99(unk)




Following is a listing of the sample input raw data set  "AfAM_RR.fil".
note the two extraneous covariates Age>=50 and Age1st not needed for
African-American BrCa Abs Risk Projection.  Also all relative risk
covariates upon input are their RAW VALUES.  The sas macro  "AABrCa_RAM"
will recode the raw values to values needed for abs risk projections.
(column heading included for clarity):

  Age   Num   Age   Age   Num   Hyper
 >=50  Biop   Men   1st   Rel  Plasia  Race     T1     T2   IDD  

    1    99    10    20     1      99     2   45.2   53.3     1     
    1    99    10    20     1       1     2   45.2   53.3     2  
    1    99    10    20     1       0     2   45.2   53.3     3  
    1     0    10    20     1      99     2   45.2   53.3     4  
    1     1    10    20     1      99     2   45.2   53.3     5  
    1     1    14    19     1      99     2   45.2   53.3     6  
    1    99    99    19     1      99     2   45.2   53.3     7  
    1     1    14    19     1       1     2   45.2   53.3     8  
    1    99    14    99     1       1     2   45.2   53.3     9  
    1     1    14    19     1       0     2   45.2   53.3    10  
    1    99    99    99     1       0     2   45.2   53.3    11  
    1     0    14    19     1       0     2   45.2   53.3    12  
    1     0    10    20     1      99     2   45.2   53.3    13  
    1     0    10    20     1       1     2   45.2   53.3    14  
    1     0    10    20     1       0     2   45.2   53.3    15  
    1     1    10    20     1       0     2   45.2   53.3    16  
    0     4    11    25     0      99     2   35.0   40.0    17  
    0     4    11    98     0      99     2   35.0   40.0    18  
    0     4    11    10     0      99     2   35.0   40.0    19  
    0     4    36    25     0      99     2   35.0   40.0    20  
    0    99    13    22     0      99     2   27.0   90.0    21  
    0    99    13    22    99      99     2   27.0   90.0    22  
    0    99    13    22    99      99     2   18.0   26.0    23  
    0    99    13    22    99      99     2   27.0   26.0    24  
    0    99    13    22    99      99     2   85.0   91.0    25  
    0    99    13    22    99      99     2   86.0   90.0    26  



Following is a listing of the consequence of applying the macro to the input file
Note the women with missing Absolute Risk (AbsRsk) projections.  This is due to violation
of edit/consistency requirements of the program.  Note the raw values for the RR
covaraties are listed, not the recoded values of 0,1,2 or 3.

AfAMAbsRsk.sas   African-American Women BrCa Abs Risk with 95% CI                   09/28/10
First 50 women projected absolute risk (%) with SE and 95% CI

         Hypr  Num   Age  Num  Currn  Proj     CARE           SE     95% CI     95% CI
     ID  Plas Biop   Men  Rel    Age   Age   AbsRsk(%)    AbsRsk     LowLim     UprLim  Race

      1    99   99    10    1   45.2  53.3   2.534700   0.233289   2.115479   3.034423     2
      2     1   99    10    1   45.2  53.3    .          .          .          .           2
      3     0   99    10    1   45.2  53.3    .          .          .          .           2
      4    99    0    10    1   45.2  53.3   2.534700   0.233289   2.115479   3.034423     2
      5    99    1    10    1   45.2  53.3   2.884603   0.281463   2.381262   3.490534     2
      6    99    1    14    1   45.2  53.3   2.216145   0.257992   1.763055   2.782379     2
      7    99   99    99    1   45.2  53.3   1.946453   0.214616   1.567476   2.414809     2
      8     1    1    14    1   45.2  53.3   3.994372   0.252993   3.526962   4.520823     2
      9     1   99    14    1   45.2  53.3    .          .          .          .           2
     10     0    1    14    1   45.2  53.3   2.062729   0.258423   1.612667   2.635029     2
     11     0   99    99    1   45.2  53.3    .          .          .          .           2
     12     0    0    14    1   45.2  53.3    .          .          .          .           2
     13    99    0    10    1   45.2  53.3   2.534700   0.233289   2.115479   3.034423     2
     14     1    0    10    1   45.2  53.3    .          .          .          .           2
     15     0    0    10    1   45.2  53.3    .          .          .          .           2
     16     0    1    10    1   45.2  53.3   2.685592   0.282078   2.184719   3.297425     2
     17    99    4    11    0   35.0  40.0   0.460443   0.080164   0.327245   0.647504     2
     18    99    4    11    0   35.0  40.0   0.460443   0.080164   0.327245   0.647504     2
     19    99    4    11    0   35.0  40.0   0.460443   0.080164   0.327245   0.647504     2
     20    99    4    36    0   35.0  40.0    .          .          .          .           2
     21    99   99    13    0   27.0  90.0   9.889364   0.295462   9.325163  10.483754     2
     22    99   99    13   99   27.0  90.0    .          .          .          .           1
     23    99   99    13   99   18.0  26.0    .          .          .          .           2
     24    99   99    13   99   27.0  26.0    .          .          .          .           2
     25    99   99    13   99   85.0  91.0    .          .          .          .           2
     26    99   99    13   99   86.0  90.0    .          .          .          .           2

In the above, examples of violation of editing and recoding rules for race, the relative  
risk covariates and or projection age variables are sprinkled throughout. Note that any 
women who violates any of the editing/consistency rules for any of the variables will 
receive a missing absolute risk estimate  i.e. AbsRsk=".".   For these instances, a 
check of all variables for the affected women should be performed and any errors should be 
corrected prior to re-obtaining absolute risk estimates.  


********************************************************************************************
* note that it is still the user's responsibility to ensure that all input variables are   *
* correct and consistent e.g. one can't have current age > projection age or race ne 2 etc *
********************************************************************************************


After the absolute risks have been generated, descriptive statistics by applying PROC
MEANS to the quantities Error_Ind, AbsRsk is performed.  When the
mean and standard deviation for the variable  "Error_Ind"  is 0, implies that no errors
have not been found.  Otherwise when the mean and std for  "Error_Ind" is not 0, implies
that errors have been found.  When errors are found, the # of records with errors is
the count asscociated with "ABSOLUTE_RISK" listed under NMiss (# of missing).


AfAMAbsRsk.sas                                                     September 27, 2010
Quick check for errornous records on input file
 
IF MEAN OF  ERROR_IND   EQUALS  0   ==>   Input file is  ERROR  FREE
Otherwise input file HAS records with Editing Errors

(# of records with errors is the # listed under the NMiss column in the 'AbsRsk' line)

                                                                                    N
Variable        Label                                                Mean    N   Miss
-------------------------------------------------------------------------------------
ERROR_IND       If mean not 0, implies Bad Records in file         0.5000   26      0
ABSOLUTE_RISK                                                      2.6665   13     13
-------------------------------------------------------------------------------------

Since NMiss=13 for ABSOLUTE_RISK,  implies 13 records with editing errors on input file



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


End of Readme section.

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

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


***     AABrCa_RAM  start of SAS macro which performs African-American Women BrCa  
                    Abs Risk projection with 95% confidence intervals

        Modification/change of any code from this point onward should be avoided,
        i.e. from  "%macro    AABrCa_RAM"  to end of the  MACRO.
        Any changes to these codes are done at the users risk, since changes may 
        inadvertenly cause the macro to be fundamentally altered resulting in 
        incorrect projections and/or abnormal termination

                     required
                     macro           all macro parameters need to "point" to appropriate
                     parameters      sas file or sas variable names upon invocation;
%macro  AABrCa_RAM  (
                     In_File         =,
                     Out_File        =,

                     T1              =,
                     T2              =,

                     NBiops          =,
                     HyprPlas        =,

                     AgeMen          =,
                     NumRel          =,
                
                     Race            =,

                     AbsRsk          =,
                     se_AbsRsk       =,
                     Lo_95CI         =,
                     Hi_95CI         =,
                     
                     Error_Ind       =,
                     IDD             =);
                     
   proc iml;

       ***
       Module for Eq 2.3 as stated by Benichou and Gail Biometrics 46,813-826 1990
       from the document c:\ABSRSK_2.3eq                                   (04/17/93)
          
       This modules performs the integration for Black Women absolute risk of BrCa
       based on RR estimates of the Gail Model derived on Black Women in the CARE study;
       
       
       start  Eq2_3(Parms)  global(&T1, &T2, h1, h2); ***define integration module Eq2_3;
       
          One_AR_rr  = Parms;         *** (1-AR)*rr*rr_hyp = (1-AR)*exp(xstar*)*rr_Hypr;
          
          Strt_Intvl = floor(&T1) - 20 + 1;       *** T1=21 T2=23, Strt_Intvl=21-20+1= 2;
          End__Intvl =  ceil(&T2) - 20 + 0;       ***              End__Intvl=23-20+0= 3;
          Num__Intvl =  ceil(&T2) - floor(&T1);   ***              Num__Intvl=23-21  = 2;
                             
                                        *** test case T1=35 T2=40  Strt_Intvl=35-20+1=16;
                                        ***                        End__Intvl=40-20+0=20;
                                        ***                        Num__Intvl=40-35  = 5;
          
          first_Intvl = 1 - (&T1-floor(&T1));***length of constant hazard for  1st intvl;
           last_Intvl = 1 - (ceil(&T2)-&T2); ***length of constant hazard for last intvl;
           
          *** Lngth_vec is the length of integeration domain for each year in the [T1,T2)
              it is usually of length 1 except maybe at the end points represented by
              first_Intvl and last_Intvl which could be fractions of 1;
          
          if      (Num__Intvl  > 2) then
             Lngth_vec = first_Intvl // j(Num__Intvl-2, 1) // 
                         last_Intvl;                  *** > 2 integration 5 yr intervals;   
                       
          else if (Num__Intvl  = 2) then
             Lngth_vec = first_Intvl // last_Intvl;   *** = 2 integration 5 yr intervals;
                    
          else if (Num__Intvl  = 1) then
             Lngth_vec = &T2 - &T1;                   *** = 1 integration 5 yr intervals;
          
          
          PI_j    = j(Num__Intvl,1);                  *** holds each age interval part of ;

          G_vec   = ((h1[ 1:30]#ONE_AR_rr[1])//(h1[31:70]#ONE_AR_rr[2])) + h2;
      
          &AbsRsk = 0;
          Cum_G   = 0;
          j_intvl = 1;
      
          do while (j_intvl <= Num__Intvl);
      
             intvl_indx    = Strt_Intvl + (j_intvl-1);
             
             Gj            = G_vec[intvl_indx];
             h1_j          = h1[intvl_indx];
      
             if      (intvl_indx <= 30) then                                        
                Tmp1_AR_rr = ONE_AR_rr[1];                                 *** age < 50;
             else if (intvl_indx  > 30) then                                    
                Tmp1_AR_rr = ONE_AR_rr[2];                                 *** age  50;
      
             PI_j[j_intvl] = ((Tmp1_AR_rr*h1_j*exp(-Cum_G))/Gj)*(1-exp(-Gj));
      
             &AbsRsk       = &AbsRsk + Lngth_vec[j_intvl]*PI_j[j_intvl];
      
             Cum_G         = Cum_G + Gj;
             j_intvl       = j_intvl + 1;
          end;

          return(&AbsRsk);                                          *** returns abs risk;
          
       finish  Eq2_3;                                      *** end of integration module;


       *** row1    Age50 Ind       set to zero by default

           row2    # Biospy         0,1,2                      
           row3    Age menarche     0,1     (also includes 2 in original gail model)
           row4    Age 1st Birth    0,1,2,3 (not in CARE RR model)
           row5    # Rel with BrCa  0,1,2                      
    
           row6    Age50*#Biop     0,1,2                      
           row7    Age1stBrth*#Rel  0,1,2,3,4,5,6;
       BETA        ={0.0334703319,   
   
                     0.1822121131,
                     0.2672530336,
                     0.0000000000,
                     0.4757242578,
   
                    -0.1119411682,
                     0.0000000000};
       BETA[1]     = 0.0000000000 ;


       *** row1  1-AR age < 50
           row2  1-AR age  50;
       ONE_AR      ={0.72949880,
                     0.74397137};
   
   
       *** SEER 11 1994-98 Black Brca Counts
           m gail 0703.xls on m:\Sas.Progs\CAREData
           malignant only no in-situ;
       h1COUN      = 21.0*j(5,1) //
                    103.0*j(5,1) //
                    305.0*j(5,1) //
                    637.0*j(5,1) //
                    974.0*j(5,1) //
                   1243.0*j(5,1) //
   
                   1186.0*j(5,1) //
                   1101.0*j(5,1) //
                    977.0*j(5,1) //
                   1041.0*j(5,1) //
                    919.0*j(5,1) //
                    752.0*j(5,1) //
                    479.0*j(5,1) //
                    386.0*j(5,1); 

       *** SEER 11 1994-98 Black Popultn Counts
           m gail 0703.xls on m:\Sas.Progs\CAREData
           malignant only no in-situ;
       h1POP       = 778913.0*j(5,1) //
                     911925.0*j(5,1) //
                     980882.0*j(5,1) //
                     941769.0*j(5,1) //
                     815445.0*j(5,1) //
                     663308.0*j(5,1) //
  
                     491090.0*j(5,1) //
                     378205.0*j(5,1) //
                     315032.0*j(5,1) //
                     283992.0*j(5,1) //
                     233764.0*j(5,1) //
                     183885.0*j(5,1) //
                     120718.0*j(5,1) //
                     106128.0*j(5,1); 
              
       h1          = h1COUN/h1POP; 
              
              
       *** NCHS 1996-00 Black Women Competing Counts
           req001t02.xls on m:\Sas Progs\Hodgkins Breast;
       h2COUN       =   5129.0*j(5,1) //
                        7231.0*j(5,1) //
                       10824.0*j(5,1) //
                       16726.0*j(5,1) //
                       22386.0*j(5,1) //
                       26522.0*j(5,1) //
                       28469.0*j(5,1) //
                       33617.0*j(5,1) //
                       43159.0*j(5,1) //
                       55198.0*j(5,1) //
                       71368.0*j(5,1) //
                       78320.0*j(5,1) //
                       78190.0*j(5,1) //
                      146599.0*j(5,1); 

       *** NCHS 1996-00 Black Women Population Counts
           req001t02.xls on m:\Sas Progs\Hodgkins Breast;
       h2POP        =6898123.0*j(5,1) //
                     7110252.0*j(5,1) //
                     7416923.0*j(5,1) //
                     7745920.0*j(5,1) //
                     7104932.0*j(5,1) //
                     5909817.0*j(5,1) //
                     4502590.0*j(5,1) //
                     3490726.0*j(5,1) //
                     2932359.0*j(5,1) //
                     2608227.0*j(5,1) //
                     2185157.0*j(5,1) //
                     1716006.0*j(5,1) //
                     1143934.0*j(5,1) //
                     1104635.0*j(5,1); 
              
       h2           = h2COUN/h2POP;
    
       
       file print;
       current_date = date(  );
       put "AfAMAbsRsk.sas   African-American Women BrCa Abs Risk with"
           " 95% CI             "  current_date mmddyy8.;
       put " ";
       put "Constants used for African-American Women Breast Cancer"
           " Absolute Risk Projection";
       put " ";
       put " ";
       put "5 year    SEER11 1994:98        NCHS 1996:2000";
       put "AgeGrp    BrCa Incidencs        Comptng Hazrds";
       put " ";
       
       put "[20:25)" (h1[ 1]) 17.10  (h2[ 1]) 22.10;
       put "[25:30)" (h1[ 6]) 17.10  (h2[ 6]) 22.10;
       put "[30:35)" (h1[11]) 17.10  (h2[11]) 22.10;
       put "[35:40)" (h1[16]) 17.10  (h2[16]) 22.10;
       put "[40:45)" (h1[21]) 17.10  (h2[21]) 22.10;
       put "[45:50)" (h1[26]) 17.10  (h2[26]) 22.10;
       put "[50:55)" (h1[31]) 17.10  (h2[31]) 22.10;
       put "[55:60)" (h1[36]) 17.10  (h2[36]) 22.10;
       put "[60:65)" (h1[41]) 17.10  (h2[41]) 22.10;
       put "[65:70)" (h1[46]) 17.10  (h2[46]) 22.10;
       put "[70:75)" (h1[51]) 17.10  (h2[51]) 22.10;
       put "[75:80)" (h1[56]) 17.10  (h2[56]) 22.10;
       put "[80:85)" (h1[61]) 17.10  (h2[61]) 22.10;
       put "[85:90)" (h1[66]) 17.10  (h2[66]) 22.10;
       put " ";
       put " ";
       put "                  ln   RR                                   1-AR";
       put " ";
       put "# Biops  "   (Beta[2]) 16.10  "             Age lt 50 " (ONE_AR[1]) 16.10;
       put "Age Men  "   (Beta[3]) 16.10  "             Age ge 50 " (ONE_AR[2]) 16.10;   
       put "# Reltv  "   (Beta[5]) 16.10;   
 
       put "AgeGE50 *";
       put "# Biops  "   (Beta[6]) 16.10;

       put " ";
       put " ";
       put "Age Flb      Not in model";
       put " ";
       put "Age Flb *";
       put "# Reltv      Not in model";
       put " ";
       
       
       *** normalization factor of n/n-1 row order is ordering of:
                                         36*NBiops      3 levels
                                         12*AgeMen      3 levels
                                          3*AgeFlb      4 levels
                                          1*NumRel + 1  3 levels
                                         =      Risk Pattern ID 
                                     
           #include BT_TSVar_AR.gau          retrieves the Delta method covariance of
                                            (1-AR)*exp(Xstar*Beta) as well as the
                                             RelRisk for all 108 patterns in matrix
                                             ARrrstar_CovARrrstar

           following matrix is output from CARE_Var1_ArRR.gau as file BT_TSVar.AR
           Reduced Model  AGEFLB=0,  AGEMEN level 2 recoded to level 1  
           ARrrstar_CovARrrstar:
  
         variance    variance  covariance      Age LT      Age GE
      (1-AR1)*RR1 (1-AR2)*RR2 (1-AR1)*RR1          50          50
                              (1-AR2)*RR2         RR1         RR2;
       ARrrstar_CovARrrstar =     
                              
      {0.00299192  0.00288409  0.00260553  1.00000000  1.00000000
       0.01799226  0.01768855  0.01698170  1.60917924  1.60917924
       0.14544749  0.14749597  0.14423965  2.58945781  2.58945781
   
       0.00299192  0.00288409  0.00260553  1.00000000  1.00000000
       0.01799226  0.01768855  0.01698170  1.60917924  1.60917924
       0.14544749  0.14749597  0.14423965  2.58945781  2.58945781
   
       0.00299192  0.00288409  0.00260553  1.00000000  1.00000000
       0.01799226  0.01768855  0.01698170  1.60917924  1.60917924
       0.14544749  0.14749597  0.14423965  2.58945781  2.58945781
   
       0.00299192  0.00288409  0.00260553  1.00000000  1.00000000
       0.01799226  0.01768855  0.01698170  1.60917924  1.60917924
       0.14544749  0.14749597  0.14423965  2.58945781  2.58945781
   
   
   
       0.00078787  0.00119939  0.00041961  1.30637096  1.30637096
       0.02108622  0.02217263  0.02014649  2.10218502  2.10218502
       0.22735754  0.23517090  0.22740892  3.38279249  3.38279249
   
       0.00078787  0.00119939  0.00041961  1.30637096  1.30637096
       0.02108622  0.02217263  0.02014649  2.10218502  2.10218502
       0.22735754  0.23517090  0.22740892  3.38279249  3.38279249
   
       0.00078787  0.00119939  0.00041961  1.30637096  1.30637096
       0.02108622  0.02217263  0.02014649  2.10218502  2.10218502
       0.22735754  0.23517090  0.22740892  3.38279249  3.38279249
   
       0.00078787  0.00119939  0.00041961  1.30637096  1.30637096
       0.02108622  0.02217263  0.02014649  2.10218502  2.10218502
       0.22735754  0.23517090  0.22740892  3.38279249  3.38279249
   
   
   
       0.00078787  0.00119939  0.00041961  1.30637096  1.30637096
       0.02108622  0.02217263  0.02014649  2.10218502  2.10218502
       0.22735754  0.23517090  0.22740892  3.38279249  3.38279249
   
       0.00078787  0.00119939  0.00041961  1.30637096  1.30637096
       0.02108622  0.02217263  0.02014649  2.10218502  2.10218502
       0.22735754  0.23517090  0.22740892  3.38279249  3.38279249
   
       0.00078787  0.00119939  0.00041961  1.30637096  1.30637096
       0.02108622  0.02217263  0.02014649  2.10218502  2.10218502
       0.22735754  0.23517090  0.22740892  3.38279249  3.38279249
   
       0.00078787  0.00119939  0.00041961  1.30637096  1.30637096
       0.02108622  0.02217263  0.02014649  2.10218502  2.10218502
       0.22735754  0.23517090  0.22740892  3.38279249  3.38279249
   
   
   
       0.00868179  0.00536501  0.00364495  1.19986867  1.07279881
       0.03544014  0.02317122  0.02043464  1.93080376  1.72632557
       0.22945820  0.17060657  0.17633880  3.10700932  2.77796726
   
       0.00868179  0.00536501  0.00364495  1.19986867  1.07279881
       0.03544014  0.02317122  0.02043464  1.93080376  1.72632557
       0.22945820  0.17060657  0.17633880  3.10700932  2.77796726
   
       0.00868179  0.00536501  0.00364495  1.19986867  1.07279881
       0.03544014  0.02317122  0.02043464  1.93080376  1.72632557
       0.22945820  0.17060657  0.17633880  3.10700932  2.77796726
   
       0.00868179  0.00536501  0.00364495  1.19986867  1.07279881
       0.03544014  0.02317122  0.02043464  1.93080376  1.72632557
       0.22945820  0.17060657  0.17633880  3.10700932  2.77796726
   
   
   
       0.00810334  0.00459033  0.00065636  1.56747359  1.40147321
       0.04534826  0.02959166  0.02251659  2.52234596  2.25522159
       0.35822946  0.27022880  0.27425253  4.05890674  3.62905576
   
       0.00810334  0.00459033  0.00065636  1.56747359  1.40147321
       0.04534826  0.02959166  0.02251659  2.52234596  2.25522159
       0.35822946  0.27022880  0.27425253  4.05890674  3.62905576
   
       0.00810334  0.00459033  0.00065636  1.56747359  1.40147321
       0.04534826  0.02959166  0.02251659  2.52234596  2.25522159
       0.35822946  0.27022880  0.27425253  4.05890674  3.62905576
   
       0.00810334  0.00459033  0.00065636  1.56747359  1.40147321
       0.04534826  0.02959166  0.02251659  2.52234596  2.25522159
       0.35822946  0.27022880  0.27425253  4.05890674  3.62905576
   
   
   
       0.00810334  0.00459033  0.00065636  1.56747359  1.40147321
       0.04534826  0.02959166  0.02251659  2.52234596  2.25522159
       0.35822946  0.27022880  0.27425253  4.05890674  3.62905576
   
       0.00810334  0.00459033  0.00065636  1.56747359  1.40147321
       0.04534826  0.02959166  0.02251659  2.52234596  2.25522159
       0.35822946  0.27022880  0.27425253  4.05890674  3.62905576
   
       0.00810334  0.00459033  0.00065636  1.56747359  1.40147321
       0.04534826  0.02959166  0.02251659  2.52234596  2.25522159
       0.35822946  0.27022880  0.27425253  4.05890674  3.62905576
   
       0.00810334  0.00459033  0.00065636  1.56747359  1.40147321
       0.04534826  0.02959166  0.02251659  2.52234596  2.25522159
       0.35822946  0.27022880  0.27425253  4.05890674  3.62905576
   
   
   
       0.03967112  0.01897201  0.00513131  1.43968484  1.15089729
       0.11880612  0.05694755  0.02463801  2.31671095  1.85200002
       0.49919575  0.26735689  0.21541253  3.72800315  2.98019998
   
       0.03967112  0.01897201  0.00513131  1.43968484  1.15089729
       0.11880612  0.05694755  0.02463801  2.31671095  1.85200002
       0.49919575  0.26735689  0.21541253  3.72800315  2.98019998
   
       0.03967112  0.01897201  0.00513131  1.43968484  1.15089729
       0.11880612  0.05694755  0.02463801  2.31671095  1.85200002
       0.49919575  0.26735689  0.21541253  3.72800315  2.98019998
   
       0.03967112  0.01897201  0.00513131  1.43968484  1.15089729
       0.11880612  0.05694755  0.02463801  2.31671095  1.85200002
       0.49919575  0.26735689  0.21541253  3.72800315  2.98019998
   
   
   
       0.05732379  0.02679946  0.00110511  1.88076246  1.50349880
       0.17911674  0.08489448  0.02487262  3.02648390  2.41939905
       0.79910403  0.43001467  0.32998658  4.87015505  3.89324671
   
       0.05732379  0.02679946  0.00110511  1.88076246  1.50349880
       0.17911674  0.08489448  0.02487262  3.02648390  2.41939905
       0.79910403  0.43001467  0.32998658  4.87015505  3.89324671
   
       0.05732379  0.02679946  0.00110511  1.88076246  1.50349880
       0.17911674  0.08489448  0.02487262  3.02648390  2.41939905
       0.79910403  0.43001467  0.32998658  4.87015505  3.89324671
   
       0.05732379  0.02679946  0.00110511  1.88076246  1.50349880
       0.17911674  0.08489448  0.02487262  3.02648390  2.41939905
       0.79910403  0.43001467  0.32998658  4.87015505  3.89324671
   
   
   
       0.05732379  0.02679946  0.00110511  1.88076246  1.50349880
       0.17911674  0.08489448  0.02487262  3.02648390  2.41939905
       0.79910403  0.43001467  0.32998658  4.87015505  3.89324671
   
       0.05732379  0.02679946  0.00110511  1.88076246  1.50349880
       0.17911674  0.08489448  0.02487262  3.02648390  2.41939905
       0.79910403  0.43001467  0.32998658  4.87015505  3.89324671
   
       0.05732379  0.02679946  0.00110511  1.88076246  1.50349880
       0.17911674  0.08489448  0.02487262  3.02648390  2.41939905
       0.79910403  0.43001467  0.32998658  4.87015505  3.89324671
   
       0.05732379  0.02679946  0.00110511  1.88076246  1.50349880
       0.17911674  0.08489448  0.02487262  3.02648390  2.41939905
       0.79910403  0.43001467  0.32998658  4.87015505  3.89324671};

   
       N_Rows = ncol(ARrrstar_CovARrrstar)/5;  *** 108 covariate patterns in order of
                                                   #Biop(3),AgeMen(3),Age1st(4),#Rel(3);
   
       ARrrstar_CovARrrstar = shape(ARrrstar_CovARrrstar, N_Rows, 5);
   
       *** row 109 of missing values to be used with records with editing errors;
       ARrrstar_CovARrrstar = ARrrstar_CovARrrstar // (j(1,5,.));

       RRofXStar   = ARrrstar_CovARrrstar[1:109,4:5];    
       CovARrrstar = ARrrstar_CovARrrstar[1:109,1:3];  
       
       
       *** column heading for listing output;
       file print;
       put "AfAMAbsRsk.sas   African-American Women BrCa Abs Risk with 95% CI                   "  
            current_date mmddyy8.;
       put "First 30 women projected absolute risk (%) with SE and 95% CI";
       put " ";
       put "         Num  Hypr   Age  Num  Currn  Proj     CARE           SE     95% CI     95% CI";
       put "     ID Biop  Plas   Men  Rel    Age   Age   AbsRsk(%)    AbsRsk     LowLim     UprLim  Race";
       put " ";


       use     &In_File;                                      *** open SAS input dataset;
       
       *** sas file with african-american rr covariates, abs risk and 95%ci;
       create  &Out_File  var {&NBiops  &AgeMen    &NumRel   &HyprPlas  &T1  &T2   &IDD
                               &AbsRsk  &se_AbsRsk &Lo_95ci  &Hi_95ci   &Error_Ind &Race};
       
       &AbsRsk    = j(1,1,.);
       &se_AbsRsk = j(1,1,.);
       &Lo_95ci   = j(1,1,.);
       &Hi_95ci   = j(1,1,.);
       
       irows = 1;
       setin   &In_File  point 0;
       
       do data; ***do data will not read past end of file  p96 SAS/IML Users Guide Ver8;
       
          read  next  var {&NBiops  &AgeMen  &NumRel  &HyprPlas  &Race  &T1  &T2  &IDD};
          
          
          &Error_Ind = 0;   *** edit error indicator  Error_Ind=1 ==>;
                            *** abs risk will not be calculated;
                            
          *** check for race eq 2 i.e. african-american;
          
          if (&Race ^= 2) then &Error_Ind = 1;


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

          if (&T1 < 20) then do; 
             set_T1_Missing = .;
             &Error_Ind     = 1;
          end;
          
          if (&T1 > 85) then do;
             set_T1_Missing = .;
             &Error_Ind     = 1;
          end;

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

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


          *** rr covariates are in raw/original format;
          *** codes to test for consistency of N_Biop (#biopsies) and HyprPlas;
          NB_Cat = -1;                          ***set NB_Cat to default value -1;

          if      ((&NBiops = 0  |  &NBiops = 99)  &  &HyprPlas ^= 99)    
          then do;
             NB_Cat             = .a;                            *** see REQUIREMENT (A);
             &Error_Ind         =  1;
          end;
          else if ((&NBiops > 0  &  &NBiops < 99)  &  (&HyprPlas ^=  0  & 
                                                       &HyprPlas ^=  1  &
                                                       &HyprPlas ^= 99))  
          then do;
             NB_Cat             = .b;                            *** see REQUIREMENT (B);
             &Error_Ind         =  1;
          end;

          *** editing and recoding for NBiops;
          if (NB_Cat = -1) then do;      *** NB_Cat=-1 implies no REQUIREMENT violations;
             if      (&NBiops  =  0  |  &NBiops  = 99)  then  NB_Cat=0;
             else if (&NBiops  =  1                  )  then  NB_Cat=1;
             else if (&NBiops >=  2  &  &NBiops ^= 99)  then  NB_Cat=2;
             else                                             NB_Cat=.;
          end;

          *** editing and recoding for AgeMen  NOTE for CARE RR AGEMEN=0,1   not 0,1,2;
          if     ((&AgeMen >= 14  &  &AgeMen <=  &T1)  |  &AgeMen  = 99) then AM_Cat=0;
          else if(                   &AgeMen <=   13)                    then AM_Cat=1;
          else                                                                AM_Cat=.;
          
                                                                             AF_Cat=0;

          *** editing and recoding for NumRel;
          if     ( &NumRel  =  0  |  &NumRel = 99)                      then NR_Cat=0;
          else if( &NumRel  =  1                 )                      then NR_Cat=1;
          else if( &NumRel >=  2  &  &NumRel < 99)                      then NR_Cat=2;
          else                                                               NR_Cat=.;
          
          *** setting RR multiplicative factor for atypical hyperplasia;
          if      (NB_Cat  =  0)         then  rrHypr = 1.00;          *** # Biopsy=0;
          else if (NB_Cat  >  0) then do;                              *** # Biopsy>0;
             if      (&HyprPlas  =  0)   then  rrHypr = 0.93;
             else if (&HyprPlas  =  1)   then  rrHypr = 1.82;
             else if (&HyprPlas  = 99)   then  rrHypr = 1.00;
          end;


          set_HyprP_Missing = &HyprPlas;
          set_R_Hyp_Missing =    rrHypr;

          if      (NB_Cat = .a)          then do;
             set_HyprP_Missing =    .a;
             set_R_Hyp_Missing =    .a;
          end;
          else if (NB_Cat = .b)          then do;
             set_HyprP_Missing =    .b;
             set_R_Hyp_Missing =    .b;
          end;


          if (NB_Cat = .  |
              AM_Cat = .  |
              NR_Cat = .) then  &Error_Ind = 1;
          

          if      (&Error_Ind = 1) then do;                  *** errornous input record;
             &AbsRsk    = .;           
             &se_AbsRsk = .;
             &Lo_95ci   = .;
             &Hi_95ci   = .;

             Pattrn_ID  = .;             
          end;                                       
          else if (&Error_Ind = 0) then do;                  *** error free input record;
          
             Pattrn_ID = NB_Cat * 36 +
                         AM_Cat * 12 +
                         AF_Cat *  3 +
                         NR_Cat *  1 + 1;
           
             *** rr1=exp(Xstar*Beta)*rrHypr  &  rr2=exp(Xstar*Beta)*rrHypr;
             RR_1_AR_Hypr= One_AR` # RRofXStar[Pattrn_ID, 1:2] # rrHypr;
          
             *** involking defined module "Eq2_3";
             &AbsRsk = Eq2_3(RR_1_AR_Hypr);
          
             call nlpfdd(crit, Gradient, hessian, "Eq2_3", RR_1_AR_Hypr);
          
             *** covariance of (1-AR)*RRstar;
             Cov_AR_RR      =(CovARrrstar[Pattrn_ID,1] ||
                              CovARrrstar[Pattrn_ID,3])//
                             (CovARrrstar[Pattrn_ID,3] ||
                              CovARrrstar[Pattrn_ID,2]);
          
             Varnc_PI_A     = Gradient*Cov_AR_RR*(Gradient`);          *** variance of PI;
             &se_AbsRsk     = sqrt(Varnc_PI_A);
          
             *** find logit transform and the taylor variance, 95%CI of the logit;

             YYY        = log(&AbsRsk/(1-&AbsRsk));
             Var_YYY    =( 1/(&AbsRsk*(1-&AbsRsk))) * Varnc_PI_A * 
                         ( 1/(&AbsRsk*(1-&AbsRsk)));
             ci_YYY     =(YYY - 1.96*sqrt(VAR_YYY)) || (YYY + 1.96*sqrt(VAR_YYY));

             *** 95%CI of the inverse logit;

             &Lo_95ci = (exp(ci_YYY[1])/(1+exp(ci_YYY[1]))); 
             &Hi_95ci = (exp(ci_YYY[2])/(1+exp(ci_YYY[2])));
          
             &AbsRsk    = 100 * &AbsRsk ;
             &se_AbsRsk = 100 * &se_AbsRsk;
             &Lo_95ci   = 100 * &Lo_95ci;
             &Hi_95ci   = 100 * &Hi_95ci;
          end;
          
          
          file print;

          if (irows <= 30) then
          
             put (      &IDD)    7.0
                 (   &NBiops)    5.0 
                 ( &HyprPlas)    6.0
                 (   &AgeMen)    6.0
                 (   &NumRel)    5.0     
                 (       &T1)    7.1
                 (       &T2)    6.1
                 (   &AbsRsk)   11.6
                 (&se_AbsRsk)   11.6
                 (  &Lo_95ci)   11.6
                 (  &Hi_95ci)   11.6
                 (     &Race)    6.0;
          
          append var {&NBiops  &AgeMen    &NumRel   &HyprPlas  &T1  &T2   &IDD 
                      &AbsRsk  &se_AbsRsk &Lo_95ci  &Hi_95ci   &Error_Ind &Race};
          
          irows = irows + 1;
       end;
   quit;


   data  &Out_File;
   set   &Out_File;
   
         label  &Error_Ind = "If mean not 0, implies Bad Records in file"
                &AbsRsk    = "Absolute risk of BrCa (%)";
   run;
   

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

   title3  "Quick check for errornous records on input file";
   title4  " ";
   title5  "IF MEAN OF  &Error_Ind   EQUALS  0   ==>   Input file is  ERROR  FREE";
   title6  "Otherwise bad records in input file";
   title8  "(# of records with errors is the # listed under the NMiss column in the"
           " 'AbsRsk' line)";
   
   
%mend    AABrCa_RAM;                 *** end of SAS macro to perform abs risk projection.
                                         End of restricted code area, where changes or
                                         modifications to the code should be avoided;
run;
                     
