Search code examples
sassas-macro

Including Proc lifetest in Do loop macro in SAS


I am trying to develop a do loop in SAS in order to create several survival plots. Assume this is my dataset:

DATA Flr;
  INPUT T F AGE;
CARDS;
31  0   52
29  1   52
13  1   22
21  0   21
19  0   21
9   0   54
9   1   17

;
RUN;

My expected output is the result of executing the following piece of code:

proc lifetest data=Flr plots=survival(nocensor cb=hw cl atrisk=0 to 60 by 5);
    strata age(40);
    time T*F(0);
run;

proc lifetest data=Flr plots=survival(nocensor cb=hw cl atrisk=0 to 60 by 5);
    strata age(20);
    time T*F(0);
run;

Instead of calling proc lifetest twice, I am intended to use a do loop. This is what I have developed. But it does not work and I can not figure out where the problem is.

%macro Create(a, b);
    %Do i=a %to b %by 10;
        proc lifetest data=data plots=survival(nocensor cb=hw cl atrisk=0 to 60 by 5);
            strata age(i);
            time T*F(0);
        run;
    %end;
%mend Create;

%Create(20, 40)

Solution

  • You are accessing your variables inside a macro, so you need to add a leading ampersand to the variables. Also i am not sure about hgage, is this a typo? I changed it to age:

    %macro Create(a, b);
    %Do i=&a %to &b %by 10;
        proc lifetest data=data plots=survival(nocensor cb=hw cl atrisk=0 to 60 by 5);
            strata age(&i);
            time T*F(0);
        run;
    %end;
    %mend Create;
    
    %Create(20, 40)