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

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!

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

%LET text1 = a;

%LET text2 = %STR(b,c,d);

%let text3=%bquote(&text1,&text2);

%put &text3;


%loop(&text3)

View solution in original post

10 REPLIES 10
novinosrin
Tourmaline | Level 20

%LET text1 = a;

%LET text2 = %STR(b,c,d);

%let text3=%bquote(&text1,&text2);

%put &text3;


%loop(&text3)
ballardw
Super User

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
Fluorite | Level 6
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!
ballardw
Super User

@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.;
clarkchong1
Fluorite | Level 6

Ah. That's obvious, wasn't it? Thank you!

Tom
Super User Tom
Super User

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;
clarkchong1
Fluorite | Level 6

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.

 

ScottBass
Rhodochrosite | Level 12

@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=|);

 

 

 


Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.
LeonidBatkhan
Lapis Lazuli | Level 10

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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 10 replies
  • 8220 views
  • 8 likes
  • 6 in conversation