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

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 674 views
  • 0 likes
  • 2 in conversation