BookmarkSubscribeRSS Feed
Ronein
Meteorite | Level 14

Hello

What is the way to concatenate two macro variables without space between them  in %let statement?

%let a_b=&a.&b.;  is not good because there is space between them?

Why space is created here?

%let a=783;
%let b=9828888223;
%let a_b=&a.&b.;  /**No good due to space**/

/**Good way**/
Data _null_;
a_b=CATS(&a,&b.);
call symputx("a_b",a_b);
Run;
%put &a_b;
8 REPLIES 8
andreas_lds
Jade | Level 19

Good (code taken from the log):

28         %let a = 783;
29         %let b = 9828888223;
30         %let a_b = &a.&b.;
31         
32         %put &a_b;
7839828888223

Note: there is no space inserted.

Astounding
PROC Star
If you are getting a space, it means that &a and &b were not created using the simple %let statements that you posted. To remove the space (or spaces), run these statements:

%let a = &a;
%let b = &b;

Then add:

%let a_b = &a.&b.;
Kurt_Bremser
Super User

There is no space.

 

%let a=783;
%let b=9828888223;
%let a_b=&a.&b.;

data _null_;
a = "&a.";
b = "&b.";
a_b = "&a_b.";
l_a = length(a);
l_b = length(b);
l_ab = length(a_b);
put l_a= l_b= l_ab=;;
put a_b $hex26.;
run;

Log:

 69         %let a=783;
 70         %let b=9828888223;
 71         %let a_b=&a.&b.;
 72         
 73         data _null_;
 74         a = "&a.";
 75         b = "&b.";
 76         a_b = "&a_b.";
 77         l_a = length(a);
 78         l_b = length(b);
 79         l_ab = length(a_b);
 80         put l_a= l_b= l_ab=;;
 81         put a_b $hex26.;
 82         run;
 
 l_a=3 l_b=10 l_ab=13
 37383339383238383838323233

As you can see, the lengths add up correctly, and there is no space (hex 20) in the variable. In fact, you can see that there's only digits (hex codes 30 to 39).

s_lassen
Meteorite | Level 14

As @Astounding remarked, there are spaces in you macro variables, which get inserted into the result, for instance if they were created like this:

data _null_;
  a=783;
  b=9828888223;
  call symput ('a',put(a,12.0));
  call symput ('b',put(b,12.0));
run;

So, if you just join them in a %LET statement, the spaces will be conserved:

 76         %put "&a.&b.";
 "         783  9828888223"

And even if you do not use the quotes, there will still be spaces between the two numbers, because &B is right-aligned.

 

I would suggest using the CATS function to get rid of the blanks, e.g.:

 77         %put "%sysfunc(cats(&a,&b))";
 "7839828888223"

The solution proposed by @Astounding will also work, but explicitly calling the function as shown makes the code easier to read: Strip blanks from the inputs, and concatenate.

 

If somebody else takes over the code as suggested by @Astounding, it is not obvious what the two %LET statements are doing. So my suggestion is you use

%let a_b=%sysfunc(cats(&a,&b));

 

Tom
Super User Tom
Super User

Except %SYSFUNC() does not work well with functions. like CATS(), that can except either a numeric or a character value for the same argument position.  Poor %SYSFUNC() has to try to figure out what type of values it is giving CATS().  This can result it warnings and even incorrect results.

 

To not add the spaces into the macro variables to begin with do not use the ancient CALL SYMPUT() function.

Instead use CALL SYMPUTX() which will remove the leading/trailing spaces.

 

When using PROC SQL to create macro variables include the TRIMMED keyword to remove the spaces.

s_lassen
Meteorite | Level 14

Good point, but I assumed that the inputs to %SYSFUNC(CATS()) would always be numeric, as in the example. In which case, "poor sysfunc" will not get confused, but just deliver the results as shown in my example.

s_lassen
Meteorite | Level 14

BTW, %sysfunc does not get confused that easily:

 69         %let a=%str(aaa  );
 70         %let b=1234;
 71         %put %sysfunc(cats(&a,&b));
 aaa1234
ballardw
Super User

A possible detail of that "space" that is in your value is that it is not actually a space but another non-printable character.

The macro processor will remove spaces by default but not so much other characters. Consider this code:

%let a=783 ;
%let a2=783 ;
%let b= 9828888223 ;
%let a_b=&a.&b.;
%let a2_b=&a2.&b;

%put a_b is:&a_b.;
%put a2_b is:&a2_b;

Result is :

138  %put a_b is:&a_b.;
a_b is:7839828888223
139  %put a2_b is:&a2_b;
a2_b is:783 9828888223

Why the difference? The A2 has a trailing null, entered in Windows with Alt-255 on the numeric keypad. So you may think it is a space but it may be a considerably different character and you need to address it. Since you did not include an actual example that duplicates your stated case then it is time to go back to your actual code that generates your two variables with any data used and show that.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 6176 views
  • 4 likes
  • 7 in conversation