DATA Step, Macro, Functions and more

%if statement not working in macro

Reply
Contributor
Posts: 48

%if statement not working in macro

%let x=a,b;

%put &x;

 

%macro new;

%do i=1 %to 2;

%let y=%scan(&x,&i,',');

%put &y;

%end;

%mend;

 

%new

 

ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric

       operand is required. The condition was: b

ERROR: Argument 2 to macro function %SCAN is not a number.

ERROR: The macro NEW will stop executing.

 

 

why I am getting this error .how can I solve this 

Respected Advisor
Posts: 3,005

Re: %if statement not working in macro

[ Edited ]
%let y=%scan(%quote(&x),&i,',');

The %quote is needed if &x can have characters like a comma (and others) that would otherwise have special meaning in SAS.

 

Also, why make your life difficult by putting a comma in &x? If the comma isn't there, and a blank is there, then things get a little simpler

 

%let y=%scan(&x,&i,%str( ));
--
Paige Miller
Super User
Posts: 10,238

Re: %if statement not working in macro

When &x is resolved, you get this call of the %scan fuinction:

%scan(a,b,&i,',')

Since the second argument to %scan needs to be numeric, this call is invalid.

It's always the same old, same old:

DO NOT USE THE MACRO LANGUAGE FOR DATA!

Use data steps for data, even if that data is temporarily kept in a macro variable:

%let x=a,b;

data _null_;
y = scan("&x",1,',');
call symputx('y',y);
run;

%put y=&y;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 6,769

Re: %if statement not working in macro

It's %SCAN that is getting confused.  It sees this:

 

%let y=%scan(a,b,1,',');

 

It doesn't know that the first comma is just text.  It thinks the first comma marks the end of the first parameter to %SCAN, and "b" is the second parameter for %SCAN.

 

If you have control over it, the simplest solution is to use a different delimiter when creating &x:

 

%let x = a|b;

 

Then:

 

%let y=%scan(&x,&i,|);

 

If you can't control that, there are more complex approaches that use macro quoting functions.

Respected Advisor
Posts: 3,005

Re: %if statement not working in macro

Posted in reply to Astounding

@Astounding wrote:

It's %SCAN that is getting confused.  It sees this:

 

%let y=%scan(a,b,1,',');

 

It doesn't know that the first comma is just text.  It thinks the first comma marks the end of the first parameter to %SCAN, and "b" is the second parameter for %SCAN.

 

If you have control over it, the simplest solution is to use a different delimiter when creating &x:

 

%let x = a|b;

 

Then:

 

%let y=%scan(&x,&i,|);

 

If you can't control that, there are more complex approaches that use macro quoting functions.


This never occurred to me, I always use a blank as a separator, but using | is actually less typing.

--
Paige Miller
Super User
Posts: 10,778

Re: %if statement not working in macro

There is a special character - comma . Mask it by %bquote() .

 

%let x=a,b;

%put &x;

 

%macro new;
%do i=1 %to 2;
%let y=%scan(%bquote(&x),&i,%str(,));
%put &y;
%end;
%mend;

 

%new
Ask a Question
Discussion stats
  • 5 replies
  • 63 views
  • 0 likes
  • 5 in conversation