Hi,
I have a string and I want to remove last word with length one or two.
Example 1: Input: EXPERTS INTERNATIONAL RECRUITMENT S --------Output: EXPERTS INTERNATIONAL RECRUITMENT
Example 2: Input: LOOTAH BUILDING CONSTRUCTION LL -----------Output: LOOTAH BUILDING CONSTRUCTION
Please help.
In below code the RegEx for want1 just removes the last term, the RegEx for want2 only removes the last term if it has 1 or 2 characters.
data sample;
infile datalines truncover;
input have $80.;
length want1 want2 $80;
want1=prxchange('s/\b\w+$//',1,strip(have));
want2=prxchange('s/\b\w{1,2}$//',1,strip(have));
datalines;
EXPERTS INTERNATIONAL RECRUITMENT S
LOOTAH BUILDING CONSTRUCTION LL
LOOTAH BUILDING CONSTRUCTION LLS
;
proc print data=sample;
run;
If the last word is longer than two chars it should not be removed, right?
Are words separated by blanks only?
If both assumptions are true, then i would use:
In below code the RegEx for want1 just removes the last term, the RegEx for want2 only removes the last term if it has 1 or 2 characters.
data sample;
infile datalines truncover;
input have $80.;
length want1 want2 $80;
want1=prxchange('s/\b\w+$//',1,strip(have));
want2=prxchange('s/\b\w{1,2}$//',1,strip(have));
datalines;
EXPERTS INTERNATIONAL RECRUITMENT S
LOOTAH BUILDING CONSTRUCTION LL
LOOTAH BUILDING CONSTRUCTION LLS
;
proc print data=sample;
run;
You could simply scan the last word by space, and check if length is 1 or 2:
data want;
str="abc ert werty we";
if lengthn(scan(str,-1," ")) in (1,2) then check=1;
run;
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.