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;

 

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

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 556 views
  • 0 likes
  • 3 in conversation