BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
gusfryk01
Calcite | Level 5

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

run;

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

call symputx and catt, though I think call symputx is possibly overkill Smiley Happy

%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.;

View solution in original post

4 REPLIES 4
Reeza
Super User

call symputx and catt, though I think call symputx is possibly overkill Smiley Happy

%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.;

gusfryk01
Calcite | Level 5

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:

ballardw
Super User

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;

Astounding
PROC Star

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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 4 replies
  • 1678 views
  • 1 like
  • 4 in conversation