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;
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.