How do remove the last 5 characters from a char string regardless of length?
Thanks
data _null_;
string="abcdefghijklx";
newstring=substr(string, 1, length(string)-5);
put newstring;
run;
SUBSTR() + REVERSE()
SUBSTR() + LENGTH()
@dber wrote:
How do remove the last 5 characters from a char string regardless of length?
Thanks
data _null_;
string="abcdefghijklx";
newstring=substr(string, 1, length(string)-5);
put newstring;
run;
Just a note that you may want lengthn() to avoid extra spaces at the end.
Right you are 🙂
@PeterClemmensen wrote:
data _null_; string="abcdefghijklx"; newstring=substr(string, 1, length(string)-5); put newstring; run;
You may want to check that the length of the string is actual greater than or at least 5 to prevent error messages.
Just in case there are shorter than expected string values.
if length(string) ge 5 then newstring=substr(string, 1, length(string)-5);
or use substrn :
data _null_;
string="abcd";
newstring=substrn(string, 1, length(string)-5);
put newstring=;
run;
If the objective is just to remove and not create a new variable, you can use just this
data want;
string="abcdefghijklx";
substr(string, length(string)-5)=' ';
run;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—just $795!
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.