BookmarkSubscribeRSS Feed
Rohit12
Obsidian | Level 7

%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 

5 REPLIES 5
PaigeMiller
Diamond | Level 26
%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
Kurt_Bremser
Super User

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;
Astounding
PROC Star

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.

PaigeMiller
Diamond | Level 26

@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
Ksharp
Super User

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: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 5 replies
  • 1132 views
  • 0 likes
  • 5 in conversation