BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Moksha
Pyrite | Level 9

Hi All, I have a macro which takes a list of variables say var1, var2, var3, var4, var5 and var6 as a parameter as shown the code below . In need to create a new variable say varlist containing these input variables separated by commas, without any quotation marks. I have tried it as follows, but it is not working : 

%macro(vars= var1 var2 var3 var4 var5 var6);
%let varcount = %sysfunc(countw(&vars));

%let varlist=;
%do i = 1 %to &varcount;
       %let varname = %qscan(&vars,&i);
       %let var&i = &varname;
       %let varlist = %unquote(%sysfunc(catx(',', &varlist, &&var&i)));
        %put var list is &varlist;

%mend;

In the following NOTE is there in the log:
NOTE: Line generated by the macro variable "VARLIST"
1                   var1','var2','var3','var4','var5','var6

Please, advise how achieve the result as var1,var2,var3,var4,var5,var6

 

Thanks

1 ACCEPTED SOLUTION
9 REPLIES 9
PaigeMiller
Diamond | Level 26
%let varlist=var1 var2 var3 var4 var5 var6;
%let varlist_wanted=%sysfunc(translate(&varlist,%str(,),%str( )));
%put &=varlist_wanted;

 

 

However, I should point out that if you don't want spaces and you do want commas, you probably should NOT create the macro variable with spaces in the first place; create it with commas and then you don't need the above code. 

--
Paige Miller
Moksha
Pyrite | Level 9
Thank you very much. It's working. Can you please help me how to get the out as varlist_wanted = 'var1', 'var2', 'var3', 'var4', 'var5', 'var6'
Moksha
Pyrite | Level 9

Thank you very much. That helped me a lot and it's working for my requirement. 

Tom
Super User Tom
Super User

@Moksha wrote:
Thank you very much. It's working. Can you please help me how to get the out as varlist_wanted = 'var1', 'var2', 'var3', 'var4', 'var5', 'var6'

Do you actually need single quotes (are you using the macro to generate SQL code in some external database) or are double quotes ok (are you using the macro to generate SAS code)?

%macro qlist(vars);
%local i sep word;
%do i = 1 %to %sysfunc(countw(&vars,%str( )));
  %let word=%scan(&vars,&i,%str( ));
&sep."&word"
  %let sep=,;
%end;
%mend;

Example usage:

21   %let varlist=%qlist(vars= var1 var2 var3 var4 var5 var6);
22   %put &=varlist;
VARLIST='var1' ,'var2' ,'var3' ,'var4' ,'var5' ,'var6'

 

If you need the single quotes you could use the QUOTE() function to add them.

&sep.%sysfunc(quote(&word,%str(%')))

There is a more robust version of %QLIST() available on-line.

Moksha
Pyrite | Level 9

Thank you very much Tom. This is working and I will use this as the code is small. As mentioned by PaigeMiller and you, the Qlist() macro available at github is more sophisticated and it's also working for my requirement. I am really learning new concepts and different ways to solve a problem, in this forum. It's really very helpful. I thank each and everyone who responded.

 

I didn't understand 

&sep."&word"

 

why it is without semicolon ; and why it is before 

 %let sep=,;

Can you please clarify?

Quentin
Super User

This kind of need comes up a lot in the macro language.  I have a list of <variables|values|dataset names> and I need to <add|remove|change>  a <delimiter|quotes|brackets>.  You might want to look into using a utility macro for this sort of manipulation.

 

I'm a fan of Richard Devenezia's %seplist:  https://www.devenezia.com/downloads/sas/macros/index.php?m=seplist 

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
Moksha
Pyrite | Level 9

Thank you very much for providing such a valuable information. I will use this wherever applicable.

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!

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
  • 9 replies
  • 1541 views
  • 0 likes
  • 5 in conversation