I´m unable to remove all the blanks (especially trailing blanks) from character variables.
I have the following macrovariables:
&Period1
&Period2
&Period3
&Period4
and want another macrovariable that´s a concatenation of these.
So if the values of these macro variables is 1, 2, 3 and 4 respectively I want a macrovariable called &Period1_4 that has the value 1234, and without any blanks.
I´ve tried to achive that using this method
data _NULL_;
period1_4=compress("&period1.&period2.&period3.&period4.", " ");
format period1_4 $4.;
call symput('period1_4', period1_4);
but I just can´t get rid of those blanks.....
If I execute
data _NULL_;
x="&period1_4.testing1234";
put x;
run;
I get the following result:
1234 testing1234
I´ve tried the trim and trimn functions to but nothing helps so I need some advice from you out there.
call symputx and catt, though I think call symputx is possibly overkill
%let period1=1;
%let period2=2;
%let period3=3;
%let period4=4;
data _NULL_;
period1_4=catt("&period1.","&period2.","&period3.","&period4.");
format period1_4 $4.;
call symputx('period1_4', period1_4);
run;
%put &period1_4.;
call symputx and catt, though I think call symputx is possibly overkill
%let period1=1;
%let period2=2;
%let period3=3;
%let period4=4;
data _NULL_;
period1_4=catt("&period1.","&period2.","&period3.","&period4.");
format period1_4 $4.;
call symputx('period1_4', period1_4);
run;
%put &period1_4.;
Thank you! In fact I used only call symputx, as I already had a lot of call symput statement I just had to add a few xs :smileygrin:
Have you tried
x= cats("&period1_4","testing1234");
?
It looks like you are trying to control the actual storage length of variable period1_4 with a format. Doesn't work, format only affects the way the value is displayed.
To control the physical stored length of a variable use the Length statement BEFORE the first use/creation of the variable:
Length period1_4 $ 4.;
period1_4=compress("&period1.&period2.&period3.&period4.", " ");
Compress was returning a much longer value.
You may get the string you want into a macro variable using
%let period1_4 = &period1.&period2.&period3.&period4;
If you are starting with 4 macro variables, you don't need to use any DATA step tools. This should do it:
%let period1 = &period1;
%let period2 = &period2;
%let period3 = &period3;
%let period4 = &period4;
%let period1_4 = &period.&period2.&period3.&period4.;
The %LET statement ignores leading and trailing blanks to the right of the equal sign.
Of course, you may still want to understand the tools, and why those blanks got in there in the first place. But that's a separate question, since you're not showing the code that created the 4 variables.
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 25. 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.