DATA Step, Macro, Functions and more

macro - separating values with commas

Reply
Frequent Contributor
Posts: 91

macro - separating values with commas

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
PROC Star
Posts: 1,760

Re: macro - separating values with commas

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
Frequent Contributor
Posts: 91

Re: macro - separating values with commas

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
PROC Star
Posts: 1,760

Re: macro - separating values with commas

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.
Ask a Question
Discussion stats
  • 3 replies
  • 121 views
  • 0 likes
  • 2 in conversation