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

Hi ,

 

Could you tell me which function i cannot use for removing leading and trailing spaces from a character text?

 

Regards,

Pooja.

1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

Run next code and learn the result of each function:

 

data _null_;
    length text $15;
    format text $char15.;
    text = '  ab   cde  f   ';
    trim = '*'||trim(text)||'*';
    compress = '*'||compress(text)||'*';
    strip = '*'||strip(text)||'*';
    put trim= ;
    put compress= ;
    put strip=;
run;

 

Here's the output:

 

Spoiler
trim=*  ab   cde  f*
compress=*abcdef*
strip=*ab   cde  f*

 See also the doc:

 

Editor's note: modified this reply to include helpful info from @RW9 and others.

View solution in original post

9 REPLIES 9
LinusH
Tourmaline | Level 20

The documentation is quite clear IMO. What is your doubt?

This can be quite easily tested by using hard coded string in a data step, with comparisons/putting using $HEX. format.

Data never sleeps
pooja215
Fluorite | Level 6
Thanks for such an early response!
Which of the following cannot be used to "just" remove the leading and trailing spaces from character data?

* Strip

* Compress

* Trim
Regards,
Pooja.
PeterClemmensen
Tourmaline | Level 20
data test;
   string = '    AB C D  ';

   strip_var    = strip(string);
   compress_var = compress(string);
   trim_var     = trim(string);
run;
Shmuel
Garnet | Level 18

Run next code and learn the result of each function:

 

data _null_;
    length text $15;
    format text $char15.;
    text = '  ab   cde  f   ';
    trim = '*'||trim(text)||'*';
    compress = '*'||compress(text)||'*';
    strip = '*'||strip(text)||'*';
    put trim= ;
    put compress= ;
    put strip=;
run;

 

Here's the output:

 

Spoiler
trim=*  ab   cde  f*
compress=*abcdef*
strip=*ab   cde  f*

 See also the doc:

 

Editor's note: modified this reply to include helpful info from @RW9 and others.

pooja215
Fluorite | Level 6
Thanks it really helped!!
Patrick
Opal | Level 21

@pooja215

I guess this was in interview question. So now with all the information you've got, what would be your answer and why?

pooja215
Fluorite | Level 6
It wasn't an interview question. I got the information about trim, strip and compress in more detail.
The answer to my question was trim, as it did not remove the leading space from my text.
Regards,
Pooja.
Patrick
Opal | Level 21

If you're using the question as you've formulated it the 2nd time "Which of the following cannot be used to "just" remove the leading and trailing spaces from character data?" then also compress() couldn't get used as it removes ALL blanks and not only the leading and trailing ones.

 

The two ways that's normally done:

strip(<string>)

trim(left(<string>))

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

And what does the manual say?  This is not a "question" as such, but asking someone to repeat what is clearly written in the manual.  I will paste tehm below for you:

Strip:

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a002295689.htm

 

Trim:

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000212226.htm

 

Compress:

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000212246.htm

 

Each page actually gives you examples of the use of each of them.  And links to other string compress/stripping functions which may be of use.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 9 replies
  • 240809 views
  • 26 likes
  • 6 in conversation