Difference between strip, compress and trim

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

Difference between strip, compress and trim

Hi ,

 

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

 

Regards,

Pooja.


Accepted Solutions
Solution
a month ago
Super User
Posts: 1,243

Re: Difference between strip, compress and trim

[ Edited ]

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


All Replies
Esteemed Advisor
Posts: 5,198

Re: Difference between strip, compress and trim

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
New Contributor
Posts: 4

Re: Difference between strip, compress and trim

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.
Valued Guide
Posts: 518

Re: Difference between strip, compress and trim

data test;
   string = '    AB C D  ';

   strip_var    = strip(string);
   compress_var = compress(string);
   trim_var     = trim(string);
run;
Solution
a month ago
Super User
Posts: 1,243

Re: Difference between strip, compress and trim

[ Edited ]

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.

New Contributor
Posts: 4

Re: Difference between strip, compress and trim

Thanks it really helped!!
Respected Advisor
Posts: 3,837

Re: Difference between strip, compress and trim

@pooja215

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

New Contributor
Posts: 4

Re: Difference between strip, compress and trim

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.
Respected Advisor
Posts: 3,837

Re: Difference between strip, compress and trim

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

 

Esteemed Advisor
Esteemed Advisor
Posts: 7,249

Re: Difference between strip, compress and trim

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.

☑ This topic is SOLVED.

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

Discussion stats
  • 9 replies
  • 4042 views
  • 4 likes
  • 6 in conversation