DATA Step, Macro, Functions and more

trim, trimn, compress problem

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 16
Accepted Solution

trim, trimn, compress problem

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.


Accepted Solutions
Solution
‎02-25-2014 04:05 PM
Super User
Posts: 19,771

Re: trim, trimn, compress problem

Posted in reply to gusfryk01

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


All Replies
Solution
‎02-25-2014 04:05 PM
Super User
Posts: 19,771

Re: trim, trimn, compress problem

Posted in reply to gusfryk01

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

Occasional Contributor
Posts: 16

Re: trim, trimn, compress problem

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:

Super User
Posts: 11,343

Re: trim, trimn, compress problem

Posted in reply to gusfryk01

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;

Super User
Posts: 5,498

Re: trim, trimn, compress problem

Posted in reply to gusfryk01

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.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 488 views
  • 1 like
  • 4 in conversation