Hi ,
Could you tell me which function i cannot use for removing leading and trailing spaces from a character text?
Regards,
Pooja.
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:
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.
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 test;
string = ' AB C D ';
strip_var = strip(string);
compress_var = compress(string);
trim_var = trim(string);
run;
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:
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.
I guess this was in interview question. So now with all the information you've got, what would be your answer and why?
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>))
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.
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!
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.
Ready to level-up your skills? Choose your own adventure.