BookmarkSubscribeRSS Feed
pappusrini
Calcite | Level 5

Good Evening, Please advise what is the best way to remove certain text in a string in dataset.

 

Example: Assignment was sent to John Smith with the reason and status as Pending

Assignment was sent to Peter Smith with the reason and status as Complete.

 

Output I'm looking for is: 

Assignment was sent to John Smith

Assignment was sent to Peter Smith

I want to remove all the text that appears after the name (John smith, Peter Smith) in the above examples.

 

Thank you

2 REPLIES 2
Ksharp
Super User

You need to post more examples and rules you are taking account of.

 

data have;
input x $80.;
cards;
Assignment was sent to John Smith with the reason and status as Pending
Assignment was sent to Peter Smith with the reason and status as Complete.
;
data want;
 set have;
 pid=prxparse('/[A-Z][a-z]+\s+[A-Z][a-z]+/');
 if prxmatch(pid,x) then do;
   call prxsubstr(pid,x,p,l);
   want=substr(x,1,p+l);
 end;
 drop pid p l;
run;

 

 

ChrisNZ
Tourmaline | Level 20

Since we don't know how many words make up the name, it's easier to use with as the marker.

 

data HAVE;
  input WORDS $80.;
cards;
Assignment was sent to John Smith with the reason and status as Pending
Assignment was sent to Peter Smith with the reason and status as Complete.
run;
data WANT;
  set HAVE;
  WORDS2 = substr(WORDS, 1, index(WORDS, ' with'));
run;

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 2 replies
  • 176 views
  • 0 likes
  • 3 in conversation