BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
EG_user
Calcite | Level 5

Hi,

 

I have a dataset which contains text fields.

In the text field,  I would like to replace a part of the sentence like " ABC123" with "XXX".

The string always starts with ABC, but the numbers behind the characters varies. 

 

So:

ABC123 -> XXX

ABC4567 -> XXX

ABC45B12-> XXX

 

I tried TRANWRD, but it only changes the ABC123 -> XXX123.

1 ACCEPTED SOLUTION

Accepted Solutions
AMSAS
SAS Super FREQ

Different example for sentances

data have ;
	infile cards ;
	input string $30.;
cards ;
blar abc
blar abc123
blar abc234
blar abc4567
blar bad123
;

data want ;
	retain 
		patternID ;
	length newString $20 ;
	set have ;
	if _n_=1 then do ;
		/* Create a Subsitute Regular Expression */
		/* (abc)(\d.) Searches for abc followed by 1 or more (.) digits (\d) */
	    /* xxx$2 what the found term is replaced with, the $2 is the contents 2nd search term (\d.) */
		patternID=prxparse('s/abc/xxx/') ;
	end ;
	/* This actually does the substitution  */
	newString=prxchange(patternID,-1,string) ;
	put string= newString= ;
run ;

View solution in original post

7 REPLIES 7
AMSAS
SAS Super FREQ

Look at the PRXCHANGE Function 

Here's an example with your data, note it only replaces "abc" when followed by numbers:

data have ;
	infile cards ;
	input string $;
cards ;
abc
abc123
abc234
abc4567
bad123
;

data want ;
	retain 
		patternID ;
	set have ;
	if _n_=1 then do ;
		/* Create a Subsitute Regular Expression */
		/* (abc)(\d.) Searches for abc followed by 1 or more (.) digits (\d) */
	    /* xxx$2 what the found term is replaced with, the $2 is the contents 2nd search term (\d.) */
		patternID=prxparse('s/(abc)(\d.)/xxx$2/') ;
	end ;
	/* This actually does the substitution  */
	newString=prxchange(patternID,1,string) ;
	put string= newString= ;
run ;
PaigeMiller
Diamond | Level 26
if string=:'ABC' then string='XXX';

 

Please note the colon after the equal sign, that indicates we are testing to see if the variable STRING begins with 'ABC'

--
Paige Miller
EG_user
Calcite | Level 5
Hi, thanks for reply.
Does it work in a sentence? So, my variable does not only contains 1 string like "ABC123", it is a whole text field
For example "Restaurant ABC123 has great hamburgers" , I would like te result to be "Restaurant XXX has great hamburgers".
Or "Yesterday I walked by restaurant ABC345, and it was closed" to be "Yesterday I walked by restaurant XXX, and it was closed".

PaigeMiller
Diamond | Level 26

Always best to describe the actual problem rather than a simplified problem where the answer someone gives doesn't apply.

 

In a sentence, you would have to test each word in sequence and then do the replacement if it finds 'ABC', so yes, it does work if you do it that way.

--
Paige Miller
AMSAS
SAS Super FREQ

Different example for sentances

data have ;
	infile cards ;
	input string $30.;
cards ;
blar abc
blar abc123
blar abc234
blar abc4567
blar bad123
;

data want ;
	retain 
		patternID ;
	length newString $20 ;
	set have ;
	if _n_=1 then do ;
		/* Create a Subsitute Regular Expression */
		/* (abc)(\d.) Searches for abc followed by 1 or more (.) digits (\d) */
	    /* xxx$2 what the found term is replaced with, the $2 is the contents 2nd search term (\d.) */
		patternID=prxparse('s/abc/xxx/') ;
	end ;
	/* This actually does the substitution  */
	newString=prxchange(patternID,-1,string) ;
	put string= newString= ;
run ;
EG_user
Calcite | Level 5
Thanks! I run the code, "blar abc" is replaced by "blar xxx", but "blar abc123" is replaced by "blar xxx123".
The output I would like is that "blar abc123" also being replaced by "blar xxx". Any string that starts with "abc" should be replaced by "xxx" while left the rest of the sentence in place.
EG_user
Calcite | Level 5
Thank you very much! I understand it now, so for more digits I can use "prxparse('s/(abc)(\d.)/xxx/')

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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
  • 7 replies
  • 2101 views
  • 3 likes
  • 3 in conversation