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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 611 views
  • 0 likes
  • 2 in conversation