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;

 

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

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
  • 738 views
  • 0 likes
  • 3 in conversation