Examples please
data have;
string='I love dog, i have a lovely dog';
data want;
set have;
s=tranwrd(string,' love ',' like ');
proc print;run;
obs string s
1 I love dog, i have a lovely dog I like dog, i have a lovely dog
Check with the following example it is repacing substrings not only words. I want only word replacement.
1 data _null_;
2
3 x= 'For the Format';
4
5 y= tranwrd(x, 'For', 'A');
6
7 Put y=;
8
9 run;
y=A the Amat
NOTE: DATA statement used (Total process time):
real time 0.25 seconds
cpu time 0.04 seconds
s
94 data _null_;
95
96
97
98 x= 'For the Format';
99
100
101
102 y= tranwrd(x, ' For ', ' A ');
103 y= tranwrd(x, 'For ', 'A ');
104
105
106
107 Put y=;
108
109 run;
y=A the Format
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds
Your words may be at the beginning/end of a sentence, preceeded/followed by periods/commas/exclamation marks, etc.
\b: word boubdary
ABC: Search string
xyz: Replace string
data have;
string = 'ABC abcde dabc abc abc, abc.';
new_string = PRXCHANGE("s/\bABC\b/xyz/i",-1,string);
put (_all_)(=);
run;
string=ABC abcde dabc abc abc, abc. new_string=xyz abcde dabc xyz xyz, xyz.
Yes, regular expressions do the job! You may improve efficiency by adding the suffix 'o' at the end of the pattern so that it is compiled only once. - PG
Hi PG,
Would you please recommend some materials for me to learn about regular expressions?
Thanks - Linlin
Hi Linlin. I'm no expert in regular expressions, I have to get back to the documentation every time I use them! They are omnipresent in the Unix world. Since regular expressions are now present in so many programming environments, there are tons of material out there, Try searching
"regular expressions" tutorial
in Google. Pick something that matches (no pun) your learning style.
Good luck.
PG
Thank you! I will do as you suggested. - Linlin
Hi PG,
Thanks for the tip. I will keep that in mind from now on.
Alpay
Thanks, PG , Ksharp and Linlin. I am in the same boat with Linlin.
Haikuo
I am honored to be in the same boat with you. Happy July 4th! - Linlin
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Use this tutorial as a handy guide to weigh the pros and cons of these commonly used machine learning algorithms.
Find more tutorials on the SAS Users YouTube channel.