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 Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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