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.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 767 views
  • 0 likes
  • 2 in conversation