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 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 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 ;
Hi KurtBremser,
Thank you so much for the reply.
I don't have any issue with put statement or data step.
It is the issue with scan function. when I used scan function in %let statement then that is not working.
I would like to know the reason why that is not working or am I missing anything.
Thanks & Regards,
Bhanuprakash Pala
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 wrote:
Sorry for the silly question and Thank you so much for the brief explanation.
A question can't be silly if you learn something from it.
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!
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.