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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.