Hi guys:
I am looking for a smart solution to remove the last character (last two characters) from a string that is preceded by a space(blank). I know how to do it for the & character but I am not sure about the others. I have more than 23,000 company names and I would not like to strip these company names from letters accidentally.
data test;
input cname $40.;
string=prxchange('s/\&&$//',-1,strip(ur_string));
cards;
A G SIMPSON AUTOMOTIVE &
A G SIMPSON AUTOMOTIVE A
A G SIMPSON AUTOMOTIVE I
A G SIMPSON AUTOMOTIVE C
A G SIMPSON AUTOMOTIVE U
A G SIMPSON AUTOMOTIVE H
A G SIMPSON AUTOMOTIVE H
A G SIMPSON AUTOMOTIVE PA
; run;
To remove a space followed by 1 or 2 non spaces, possibly followed by spaces until the end of the string:
data test;
input cname $60.;
string=prxchange('s/\s\S{1,2}\s*$//o', 1, cname);
cards;
A G SIMPSON AUTOMOTIVE &
A G SIMPSON AUTOMOTIVE A
A G SIMPSON AUTOMOTIVE I
A G SIMPSON AUTOMOTIVE C
A G SIMPSON AUTOMOTIVE U
A G SIMPSON AUTOMOTIVE H
A G SIMPSON AUTOMOTIVE H
A G SIMPSON AUTOMOTIVE PA
A G SIMPSON AUTOMOTIVE INC
;
proc print; run;
To remove a space followed by 1 or 2 non spaces, possibly followed by spaces until the end of the string:
data test;
input cname $60.;
string=prxchange('s/\s\S{1,2}\s*$//o', 1, cname);
cards;
A G SIMPSON AUTOMOTIVE &
A G SIMPSON AUTOMOTIVE A
A G SIMPSON AUTOMOTIVE I
A G SIMPSON AUTOMOTIVE C
A G SIMPSON AUTOMOTIVE U
A G SIMPSON AUTOMOTIVE H
A G SIMPSON AUTOMOTIVE H
A G SIMPSON AUTOMOTIVE PA
A G SIMPSON AUTOMOTIVE INC
;
proc print; run;
Another solution without regular expressions:
data test;
length cname $ 40 string $ 40 ;
input cname $40.;
string = substr(cname, 1, findc(trim(cname), ' ', 'b'));
cards;
A G SIMPSON AUTOMOTIVE &A G SIMPSON AUTOMOTIVE A
A G SIMPSON AUTOMOTIVE I
A G SIMPSON AUTOMOTIVE C
A G SIMPSON AUTOMOTIVE U
A G SIMPSON AUTOMOTIVE H
A G SIMPSON AUTOMOTIVE HA G SIMPSON AUTOMOTIVE PA
;
run;
Thank you.
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 lock in 2025 pricing—just $495!
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.