BookmarkSubscribeRSS Feed
SASPhile
Quartz | Level 8

 

I tried below code to convert the words to space if the string ends in limited,ltd,plc,sa,inc. But the third record(limited abc) , the abc is converted to a space. Any help is appreciated!

 

 

data test;
input string $1-12;
cards;
abc limited
pqr ltd
limited abc
abc sa
abc plc 
plc abc
abc inc
;
run;
data want;
 set Test;
 if prxmatch("/(^.*\binc\b|^.*\blimited\b|^.*\bltd\b|^.*\bsa\b|^.*\bplc\b)/i",string) > 0 then do;
 want=prxchange('s/\w+$//',-1,strip(string));
 End;
run;
1 REPLY 1
PGStats
Opal | Level 21

Keep it simple:

 

data have;
input string $12.;
cards;
abc limited
pqr ltd
limited abc
abc sa
abc plc 
plc abc
abc inc
;

data want;
 set have;
 want = prxchange("s/\s*\b(inc|limited|ltd|sa|plc)\s*$//io", 1, string);
run;
PG

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 1 reply
  • 655 views
  • 0 likes
  • 2 in conversation