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;
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.
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).
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));
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.
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.
BTW, %sysfunc does not get confused that easily:
69 %let a=%str(aaa ); 70 %let b=1234; 71 %put %sysfunc(cats(&a,&b)); aaa1234
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: