I have the following macro variable definitions:
%LET text1 = a;
%LET text2 = %STR(b,c,d);
What do I need to do to create text3 from &text1 and &text2, which is equivalent to assigning:
%LET text3 = %STR(a,b,c,d);
?
I need the resulting text3 to be able to be used as &value in the following sample code:
http://support.sas.com/kb/26/155.html
Thank you!
%LET text1 = a;
%LET text2 = %STR(b,c,d);
%let text3=%bquote(&text1,&text2);
%put &text3;
%loop(&text3)
%LET text1 = a;
%LET text2 = %STR(b,c,d);
%let text3=%bquote(&text1,&text2);
%put &text3;
%loop(&text3)
Are you sure you need that particular code?
You can get the same result without any commas:
%macro loopalt(values); /* Count the number of values in the string */ %let count=%sysfunc(countw(&values)); /* Loop through the total number of values */ %do i = 1 %to &count; %let value=%scan(&values, &i); %put &value; %end; %mend; %loopalt(2 3 5 7 11 13 17 )
In general introducing commas and/or quotes into macro variables seems to cause more headaches than needed for most cases.
@clarkchong1 wrote:
That's a great idea! How do one then combine text1 and text2 below to get text3?
%LET text1 = a b;
%LET text2 = c d;
%LET text3 = a b c d;
Thanks!
%LET text1 = a b; %LET text2 = c d; %let text3 = &text1. &text2.; %put text3= &text3.;
Ah. That's obvious, wasn't it? Thank you!
Why are you adding the macro quotes?
%LET text2 =1,5,9;
Then you can use the value a DO statement.
data test;
do x=&text2;
output;
end;
run;
Thanks for the suggestion!
I need to execute a macro functions with various input, with input defined by items in a comma delimited list supplied by the user. So I can't just do it in a data step.
@clarkchong1 wrote:
Thanks for the suggestion!
I need to execute a macro functions with various input, with input defined by items in a comma delimited list supplied by the user. So I can't just do it in a data step.
I hate commas in macro lists, especially if that list is then a parameter to another macro or macro function. It's nothing but headaches.
You said "comma delimited list supplied by the user":
1) Is that a macro variable supplied by the user, or some other "list"?
2) Can the user supply it as just a delimited (space or some other character) list?
I prefer "injecting" commas at "run time" via the %seplist macro:
https://github.com/scottbass/SAS/blob/master/Macro/seplist.sas
Here are some sample invocations:
%put %seplist(a b c);
%put %seplist(a b c,nest=q);
%put %seplist(a b c,nest=qq);
%put %seplist(a b c,prefix=a.),%seplist(d e f,prefix=b.); * say for a SQL SELECT column list ;
%put %seplist(%bquote(a,b,c),indlm=%str(,)); * if you insist on embedded commas then you have to quote the argument ;
%put %seplist(hello scott^how are you^i am well thanks,indlm=^); * if items contain spaces ;
%put %seplist(hello scott^how are you^i am well thanks,indlm=^,dlm=|);
It's even simpler than that. You can have
%let text1 = 2;
%let text2 = 3,5,7,11,13,17;
%let text3 = &text1,&text2;
To check it out just run
%put &text3;
and you will get in SAS log:
2,3,5,7,11,13,17
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.