03-05-2018 08:33 PM
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.
03-05-2018 08:40 PM
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.
03-05-2018 09:17 PM
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.
03-05-2018 11:35 PM
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;
03-06-2018 01:02 PM
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.