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.
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 ;
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 ;
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'
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.
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 ;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.