Hello
I want to concatenate macro varaibles into a new macro varaible with plus sign between values.
Please note that there is no calculation here and just concatenate of values with + sign.
My question is how to tell SAS that If values contain null (dot) then no need to include it in the new macro variable.
Instead of getting 2005+.+1912 I nant to get 2005+1912
%let YYMM1=2005;
%let YYMM2=.;
%let YYMM3=1912;
%let vector=%sysfunc(catx(+,&YYMM1,&YYMM2.,&YYMM3.));
%put &vector3.;/*2005+.+1912*/
@Ronein wrote:
Hello
I want to concatenate macro varaibles into a new macro varaible with plus sign between values.
Please note that there is no calculation here and just concatenate of values with + sign.
My question is how to tell SAS that If values contain null (dot) then no need to include it in the new macro variable.
Instead of getting
2005+.+1912 I nant to get 2005+1912%let YYMM1=2005; %let YYMM2=.; %let YYMM3=1912; %let vector=%sysfunc(catx(+,&YYMM1,&YYMM2.,&YYMM3.)); %put &vector3.;/*2005+.+1912*/
Use a data step:
%let YYMM1=2005;
%let YYMM2=.;
%let YYMM3=1912;
data _null_;
length vector $32767;
do i = 1 to 3;
vec = symget(cats('yymm',i));
if vec ne '.' then vector = catx('+',vector,vec);
end;
call symputx('vector',vector);
run;
%put &=vector.;
Log excerpt:
85 86 %put &=vector.; VECTOR=2005+1912
Hello,
A slightly different version using an array and the $charw informat to read missing numeric values as spaces.
%let YYMM1=2005;
%let YYMM2=.;
%let YYMM3=1912;
data _NULL_;
informat YYMM: $char4.;
array vector YYMM1-YYMM3;
do over vector;
vector=symget(vname(vector));
end;
call symputx("vector", catx('+', of vector(*)));
run;
%put &vector;
Macro variables are strings. The string . is a valid string. You are better off removing the dot when the macro variable is populated.
Otherwise you need to process the string. Something like :
%let vector=%sysfunc(catx(+
,%sysfunc(ifc(&yymm1=.,,&yymm1))
,%sysfunc(ifc(&yymm1=.,,&yymm1))
,%sysfunc(ifc(&yymm1=.,,&yymm1))
));
It can be done with PRXCHANGE, e.g.:
%let vector=%sysfunc(prxchange(s/(\+\.|\.\+)//,-1,%sysfunc(catx(+,&YYMM1,&YYMM2.,&YYMM3.))));
The string deletes ".+" or "+." - the backslashes are there to escape the special characters "+" and ".".
Hi,
you could use optinos:
%let YYMM1=2005;
%let YYMM2=.;
%let YYMM3=1912;
options missing=" ";
%let vector3=%sysfunc(catx(+,&YYMM1.,&YYMM2.,&YYMM3.));
%put &vector3.;/*2005+1912*/
options missing=.;
%let vector4=%sysfunc(catx(+,&YYMM1.,&YYMM2.,&YYMM3.));
%put &vector4.;/*2005+.+1912*/
All the best
Bart
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.