BookmarkSubscribeRSS Feed
Kabuto
Obsidian | Level 7

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.

4 REPLIES 4
mkeintz
PROC Star

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.

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Kabuto
Obsidian | Level 7

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. 

Tom
Super User Tom
Super User

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;
Kabuto
Obsidian | Level 7

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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 5740 views
  • 2 likes
  • 3 in conversation