I am writing a macro program for creating a general survival data set into a multiple-record data set which can be analyzed by the piecewise exponential model. Here is my macro:
%macro piece(indata=, outdata=, interval=, survt=, cens=, fcode=1);
data &outdata;
set &indata;
a = ceil(&survt/&interval);
%do j = 1 %to a;
time = &interval;
event = 0;
%if j = a and &survt = &fcode %then %do;
event = 1;
time = &survt - &interval*(a-1);
%end;
output;
%end;
run;
%mend;
However, I got some error messages:
ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric
operand is required. The condition was: a
ERROR: The %TO value of the %DO J loop is invalid.
ERROR: The macro PIECE will stop executing.
I might imagine that the problem happens in the variable 'a' using the ceil() function, but I have no idea how to debug it. Hopefully someone can help this out. Any assistance is appreciated.
Because you chose a macro do loop %do J=1 %to a, macro assumes that the %to expression is a number. But it isn't. It's the letter A, which has no meaning to macro. Really this should be a regular data step do loop, not a macro %do.
Before making a macro, make a successful data step (including a do loop) that performs your task for one specific set of parameters. Then, assuming you will be rerunning this code for multiple parameters sets, make the macro with minimal changes from the original code.
I do write an original SAS code before writing the macro. Your suggest is all right. After using normal DO loop, the error code disappears. However, the two variables (time and event) have the same value across all records. Here is the new macro:
%macro piece(indata=, outdata=, interval=, survt=, cens=, fcode=1);
data &outdata;
set &indata;
a = ceil(&survt/&interval);
do j = 1 to a;
time = &interval;
event = 0;
%if j = a and &survt = &fcode %then %do;
event = 1;
time = &survt - &interval*(a-1);
%end;
output;
end;
run;
%mend;
I feel that the %IF-%THEN-%DO statement does not work. I alternatively used the normal IF-THEN-DO statement, no thing is changed (but at least no more error message). I still think something wrong in the variable 'a' in the IF-THEN-DO statement, but cannot find out any bug.
What did you think this block of code was going to do?
%if j = a and &survt = &fcode %then %do;
event = 1;
time = &survt - &interval*(a-1);
%end;
The letter j is never going to be equal to the letter a so the %IF statement will not generate any code to pass off to SAS to include in the data step.
Did you mean to write this IF statement instead?
if j = a and &survt = &fcode then do;
event = 1;
time = &survt - &interval*(a-1);
end;
I have found out the bug. Indeed, the % symbol should be removed for sure, and there is another error in the condition &survt=&fcode, which should be &cens=&fcode. This problem is not about the macro but a mistake in my logic thinking. Thus, the macro is working to generate a multiple-record data set for running a piecewise exponential model.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.