BookmarkSubscribeRSS Feed
Ronein
Meteorite | Level 14

Hello

How can I remove words from string if the word length is lower than 4?

For example:

For string "The copmany LLTX inc"  I will get "company LLTX"

 

 

5 REPLIES 5
ballardw
Super User

What have you tried so far?

Countw, loop over the string, Scan each word, Length function for each word, build new string with CATX function.

PeterClemmensen
Tourmaline | Level 20
data test;
   s = "The copmany LLTX inc";
   n = prxchange("s/\b\w{1,3}\b//", -1, s);
run;
andreas_lds
Jade | Level 19

I am almost sure that this problem can be solved by using a regular expression and that is you will find code if you would search for it.

s_lassen
Meteorite | Level 14

The answer supplied by @PeterClemmensen is almost there. Except that you may get leading blanks and double blanks in your output, they can be removed by LEFT and COMPBL:

data _null_;
   s = "The copmany LLTX inc";
   n = left(compbl(prxchange("s/\b\w{1,3}\b//", -1, s)));
   put n $quote.;
run;

I changed the demonstration example to a DATA _NULL_, as this makes it easier to see the result in the log, with quotes so that you can easily see the leading blanks.

 

The PRX expression looks for a word boundary(\b), followed by one to three "word" characters (letter, numbers and underscores, that's \w) and then another word boundary, and that expression is changed to nothing.

 

If you also want to get rid of other sets of non-blank characters, such as "f.3" or "1,5", you can change the \w to \S, which means any non-blank character.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 665 views
  • 5 likes
  • 5 in conversation