Here is a sample macro
%macro pippo();
%let separator_s =%str( );
%let separator_c =%str(,);
%let a=ciao;
%let aa=%str(a=b);
%let a= %sysfunc(catx(&separator_s,&a,&aa));
%put &a;
%mend pippo;
%pippo;
I would like the macro variable "a" to be "ciao a=b"
However I get "ciao 0".
Any suggestion on why it is so?
Thank you very much
You can do it but it takes strong quoting. Double quoting if you will. :smileyplain: Note also that you should quote the first argument in CATX too, but I did not.
You do not need any function or operator to connect strings in macro language.
%let separator_s =%str( );
%let separator_c =%str(,);
%let a=ciao;
%let aa=a=b;
%let a=&a&separator_s&aa;
%put &a;
The only thing you need is know that you can use a period to mark the send of your macro variable so that SAS does not get confused and try to use trailing text that as part of the macro variable name. So you could assign the value of A this way and get the same result.
%let a=&a.&separator_s.&aa.;
SAS is passing the function arguments through an implicit %sysevalf() before handing it off the CATX() function. %eval( a=b ) is 0 (false) because a does not equal b. Hence CATX() sees only the text string 0 and so that is what it appends.
Thank you very much Tom.
I was kind of thinking that but I was not sure.
But is there a way to avoid the pass through %sysevalf() and still use the catx function?
Why is there in the first place?
Not that I know of. It is useful when the function argument really is a number since you can use arithmetic in the call (&i + 1) without having either add a %EVAL() or create another macro variable.
You can build you own macro version of CATX.
%macro catx(sep) / parmbuff ;
%local string i result;
%let string=%qscan(&syspbuff,1,(),q);
%let i=%eval(2-(%bquote(&sep)=,));
%let result=%qscan(&string,&i,%str(,),q);
%do i=&i+1 %to %sysfunc(countw(&string,%str(,),q));
%let result=&result.&sep.%scan(&string,&i,%str(,),q);
%end;
%unquote(&result)
%mend catx;
%let separator_s =%str( );
%let separator_c =%str(,);
%let a=ciao;
%let aa=%str(a=b);
%put space="%catx(&separator_s,&a,&aa)";
space="ciao a=b"
%put comma="%catx(&separator_c,&a,&aa)";
comma="ciao,a=b"
Message was edited by: Tom Abernathy
Added data_null_'s suggestion of using %nrstr().
%put sysfunc="%sysfunc(catx(&separator_s,%nrstr(&a),%nrstr(&aa)))";
sysfunc="ciao a=b"
You can do it but it takes strong quoting. Double quoting if you will. :smileyplain: Note also that you should quote the first argument in CATX too, but I did not.
try this..
%macro pippo();
%let separator_s =%str( );
%let separator_c =%str(,);
%let a=ciao;
%let aa=%str(a=b);
%let a= %sysfunc(catx(&separator_s,&a,%nrstr(&aa)));
%put &a;
%mend pippo;
%pippo;
Both data_null and yaswanthj answers work.
Thank you very much.
This little problem bugged me for a couple of weeks.
Good for few other scenarios:
%let m_var;
data _null_;
set dataset;
call symput('m_var',catx(' ',symget('m_var'),variable _append));
run;
%put &m_var;
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.
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.