BookmarkSubscribeRSS Feed
Macro
Obsidian | Level 7

Hi,

To scan the second word var2 in macro var qtnames, I used the following:

%let qtnames = var1, var2, var3;

%put %scan(&qtnames, 2, ",");

The above has the error:

Macro function %SCAN has too many arguments.

How to fix the problem? Why? Thanks.

11 REPLIES 11
jakarman
Barite | Level 11

Would advice to get the why-s about macro quoting. SAS(R) 9.4 Macro Language: Reference

using learning SAS macros for programming is alo requiring to understand how something works. In this case the macro parser.
It has some conflicting approaches to other parts as being a programming language on its own.

---->-- ja karman --<-----
data_null__
Jade | Level 19

If you're going to use commas to delimit your list you will need follow advice and learn macro quoting.

%let qtnames = var1, var2, var3;

%put %scan(%superQ(qtnames), 2, %str(,));
Astounding
PROC Star

Also note, since you are treating both commas and blanks as delimiters, you might want to add blanks to the list of legitimate delimiters by including it in the %str function:

%str(, )

Tom
Super User Tom
Super User

Whether extra spaces that are retained from the source string have an impact depends on how you use the result of the %SCAN() function. 

Normally SAS will ignore unquoted spaces at the start and end of a string. 

%let qtnames = var1, var2, var3;

So for the %PUT function they are ignored.

%put %scan(%superQ(qtnames), 2, %str(,));

var2


But if you put them in the middle of a longer string they will stay.

%put |%scan(%superQ(qtnames), 2, %str(,))|;

| var2|

Also for %LET the extra spaces are normally ignored.

%let x= %scan(%superQ(qtnames), 2, %str(,));

%put |&x| ;

|var2|

But you can use %QSCAN() if you want them to stay.

%let x= %qscan(%superQ(qtnames), 2, %str(,));

%put |&x| ;

| var2|

Macro
Obsidian | Level 7

Thanks Data_null_.  But why the following does not work? I used %str() to tell SAS ot ignore the commas in the string. What is the difference between %str and %superQ? When should I use at compilation quote function, and when should I use at execution quote function, it appears there is no difference?

%put %scan(%str(&qtnames), 2, %str(,));

DrAbhijeetSafai
Pyrite | Level 9
This was really helpful! 🙂
Dr. Abhijeet Safai
Associate Data Analyst
Actu-Real
ballardw
Super User

The simple answer to "Why?" is that the %scan function is seeing

%scan( var1, var2, var3, 2,",") after resolving Qtnames.

So you need some way to tell the function to ignore the commas in the string.

Macro
Obsidian | Level 7

Thanks ballardw. But why

%scan("&qtnames", 2, ",");

also works, but I did not use any macro quoting function.

Tom
Super User Tom
Super User

The commas did not cause errors because you quoted the commas with regular quoting.

The reason that the scanning still works is because you also included quote character in the list of delimiters.

You could get the same effect by using parentheses around the string value and in the delimiter list.

%scan((&qtnames), 2, (,));

yaswanthj
Calcite | Level 5

WE can use the %Scan many ways in macro like suggested above..as per rules.

method 1:

%let qtnames = %str(var1, var2, var3);

%put %scan(&qtnames, 2, ",");


method 2:

%let qtnames = "var1, var2, var3";

%put %scan(&qtnames, 2, ",");

method 3:


%let qtnames = var1, var2, var3;

%put %scan("&qtnames", 2, ",");

etc...

As suggested by : SAS(R) 9.2 Macro Language: Reference : you can get clear idea by reading this..

Thanks ,

Yaswanth

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 11 replies
  • 23250 views
  • 8 likes
  • 8 in conversation