%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
%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( ));
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;
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.
@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.
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
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.