DATA Step, Macro, Functions and more

A bug about %EVAL function or %IF condition

Reply
Occasional Contributor
Posts: 9

A bug about %EVAL function or %IF condition

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.

Trusted Advisor
Posts: 1,312

Re: A bug about %EVAL function or %IF condition

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.

 

 

Occasional Contributor
Posts: 9

Re: A bug about %EVAL function or %IF condition

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. 

Super User
Super User
Posts: 7,938

Re: A bug about %EVAL function or %IF condition

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;
Occasional Contributor
Posts: 9

Re: A bug about %EVAL function or %IF condition

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.

Ask a Question
Discussion stats
  • 4 replies
  • 114 views
  • 2 likes
  • 3 in conversation