BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
bhanuprakash
Obsidian | Level 7

By using the below I am passing one macro variable. It has multiple words. Based on number of words loop will execute and each time one word should be resolved and that should be the macro variable.

 

Please see the below codes and let me know why second one is not working.


* Working ;

%macro test(var_names=) ;
%do i = 1 %to %sysfunc(countw(&var_names.)) ;
%put "&i." ;
var_main = scan(&var_names.,&i.) ; output ;
%end ;
%mend test ;

 

data testing ;
%test(var_names = %str("bhanu prakash")) ;
run ;

 

* Not working ;

 

%macro test(var_names=) ;
%do i = 1 %to %sysfunc(countw(&var_names.)) ;
%put "&i." ;
%let var_main = %scan(&var_names.,&i.) ;
%put &var_main. ;
%end ;
%mend test ;

 

data testing ;
%test(var_names = %str("bhanu prakash")) ;
run ;

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Maxim 2: read the log. In this context, "read" also means "understand and interpret".

37         data testing ;
38         %test(var_names = %str("bhanu prakash")) ;
"1"
ERROR: Literal contains unmatched quote.
ERROR: The macro TEST will stop executing.
39         run ;

Keep in mind that the macro preprocessor is a pure text processor; everything is text to it, including quotes(!).

%scan dissects the string with the masked quotes at the blank, and so you get

"bhanu

as the first result, which is a literal with an unmatched quote.

Text strings in macros need no quotes, try this:

%macro test(var_names=) ;
%do i = 1 %to %sysfunc(countw(&var_names.)) ;
%put "&i." ;
%let var_main = %scan(&var_names.,&i.) ;
%put &var_main. ;
%end ;
%mend test ;

%test(var_names = bhanu prakash) ;

Since the macro does not create any data step code, the data step itself is not needed.

View solution in original post

5 REPLIES 5
Kurt_Bremser
Super User

@bhanuprakash wrote:

By using the below I am passing one macro variable. It has multiple words. Based on number of words loop will execute and each time one word should be resolved and that should be the macro variable.

 

Please see the below codes and let me know why second one is not working.


* Working ;

%macro test(var_names=) ;
%do i = 1 %to %sysfunc(countw(&var_names.)) ;
%put "&i." ;
var_main = scan(&var_names.,&i.) ; output ;
%end ;
%mend test ;

 

data testing ;
%test(var_names = %str("bhanu prakash")) ;
run ;

 

* Not working ;

 

%macro test(var_names=) ;
%do i = 1 %to %sysfunc(countw(&var_names.)) ;
%put "&i." ;
%let var_main = %scan(&var_names.,&i.) ;
%put &var_main. ;
%end ;
%mend test ;

 

data testing ;
%test(var_names = %str("bhanu prakash")) ;
run ;


Maxim 1: read the documentation, in this case of the %put macro statement.

All it does is write text to the log; if &var_main is not already present in the global symbol table, it will be local to the macro, and its contents lost when the macro stops executing.

The first macro is actually creating something, namely the code that writes observations to the dataset. The second macro creates nothing, so you end up with this data step code:

data testing ;
run ;
bhanuprakash
Obsidian | Level 7

Hi KurtBremser

 

 

 

 

Kurt_Bremser
Super User

Maxim 2: read the log. In this context, "read" also means "understand and interpret".

37         data testing ;
38         %test(var_names = %str("bhanu prakash")) ;
"1"
ERROR: Literal contains unmatched quote.
ERROR: The macro TEST will stop executing.
39         run ;

Keep in mind that the macro preprocessor is a pure text processor; everything is text to it, including quotes(!).

%scan dissects the string with the masked quotes at the blank, and so you get

"bhanu

as the first result, which is a literal with an unmatched quote.

Text strings in macros need no quotes, try this:

%macro test(var_names=) ;
%do i = 1 %to %sysfunc(countw(&var_names.)) ;
%put "&i." ;
%let var_main = %scan(&var_names.,&i.) ;
%put &var_main. ;
%end ;
%mend test ;

%test(var_names = bhanu prakash) ;

Since the macro does not create any data step code, the data step itself is not needed.

bhanuprakash
Obsidian | Level 7
Sorry for the silly question and Thank you so much for the brief explanation.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 1613 views
  • 1 like
  • 2 in conversation