DATA Step, Macro, Functions and more

How to scan comma delimited macro variable list?

Reply
Contributor
Posts: 50

How to scan comma delimited macro variable list?

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.

Valued Guide
Posts: 3,208

Re: How to scan comma delimited macro variable list?

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 --<-----
Respected Advisor
Posts: 3,777

Re: How to scan comma delimited macro variable list?

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(,));
Super User
Posts: 5,082

Re: How to scan comma delimited macro variable list?

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(, )

Respected Advisor
Posts: 3,777

Re: How to scan comma delimited macro variable list?

Am I?

Super User
Super User
Posts: 6,500

Re: How to scan comma delimited macro variable list?

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|

Contributor
Posts: 50

Re: How to scan comma delimited macro variable list?

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(,));

Super User
Posts: 10,500

Re: How to scan comma delimited macro variable list?

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.

Contributor
Posts: 50

Re: How to scan comma delimited macro variable list?

Thanks ballardw. But why

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

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

Super User
Super User
Posts: 6,500

Re: How to scan comma delimited macro variable list?

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, (,));

Contributor
Posts: 70

Re: How to scan comma delimited macro variable list?

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

Ask a Question
Discussion stats
  • 10 replies
  • 9015 views
  • 6 likes
  • 7 in conversation