BookmarkSubscribeRSS Feed
Ronein
Onyx | Level 15

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*/
5 REPLIES 5
Kurt_Bremser
Super User

@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
 
gamotte
Rhodochrosite | Level 12

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;
ChrisNZ
Tourmaline | Level 20

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))
                   ));

 

s_lassen
Meteorite | Level 14

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 ".".

yabwon
Amethyst | Level 16

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 2115 views
  • 1 like
  • 6 in conversation