BookmarkSubscribeRSS Feed
WendyT
Pyrite | Level 9
I’m working on a macro that uses DATA steps, PROC TRANSPOSE and PROC SQL. As a consequence of using PROC SQL, I need the same variable list both with and without commas.

My original solution was to use %str() and then COMPRESS() inside the macro to get rid of the embedded commas.

I would prefer to have the variable list without commas, and to add a second series of variables in some cases.

The code below works, but I’m sure there has to be a better method. Also, is there a way to test if &addmore is null directly?

Thanks for any help you can give me.

WendyT


%macro addcommas(somevars,addmore);
%let nullval = ;
%if &addmore ne &nullval %then
%let commavalue=%SYSFUNC(TRANSLATE(&somevars, ', ', ' ')),%SYSFUNC(TRANSLATE(&addmore, ', ', ' '));
%else %let commavalue=%SYSFUNC(TRANSLATE(&somevars, ', ', ' ')) ;
%put &commavalue;
%mend;


%addcommas(a b c, d e f) ;
a,b,c,d,e,f

%addcommas(a b c, ) ;
a,b,c
3 REPLIES 3
ChrisNZ
Tourmaline | Level 20
How do you like

%macro addcommas(somevars,addmore);
%put %sysfunc(translate(&somevars &addmore, %str(,), %str( ) ));
%mend;

>Also, is there a way to test if &addmore is null directly?

See a very interesting paper on testing for blank macro variables:
http://support.sas.com/resources/papers/proceedings09/022-2009.pdf
WendyT
Pyrite | Level 9
Chris-

Thanks so much for the elegant combination, and the reference to the paper on macro parameters was exactly what I needed. Revised code below.

Wendy


%macro addcommas(somevars,addmore);
%if %sysevalf(%superq(addmore)=,boolean) %then %let commavalue=%sysfunc(translate(&somevars, %str(,), %str( ) )) ;
%else %let commavalue=%sysfunc(translate(&somevars &addmore, %str(,), %str( ) ));
%put &commavalue;
%mend;



%addcommas(a b c, d e f) ;
a,b,c,d,e,f


%addcommas(a b c, ) ;
a,b,c
ChrisNZ
Tourmaline | Level 20
I don't think you need the %if test Wendy.

%sysfunc(translate(&somevars &addmore ...

should be enough in all cases. If addmore is empty, translate will not see it.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 3 replies
  • 1167 views
  • 0 likes
  • 2 in conversation