BookmarkSubscribeRSS Feed
12 REPLIES 12
Linlin
Lapis Lazuli | Level 10

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


devarayalu
Fluorite | Level 6

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

Linlin
Lapis Lazuli | Level 10

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

Alpay
Fluorite | Level 6

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.

PGStats
Opal | Level 21

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

PG
Linlin
Lapis Lazuli | Level 10

Hi PG,

Would you please recommend some materials for me to learn  about regular expressions?

Thanks - Linlin

PGStats
Opal | Level 21

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

PG
Linlin
Lapis Lazuli | Level 10

Thank you! I will do as you suggested.  - Linlin

Alpay
Fluorite | Level 6

Hi PG,

Thanks for the tip. I will keep that in mind from now on.

Alpay

PGStats
Opal | Level 21

I got it from , pass it on! - PG

PG
Haikuo
Onyx | Level 15

Thanks, PG , Ksharp and Linlin. I am in the same boat with Linlin.Smiley Happy

Haikuo

Linlin
Lapis Lazuli | Level 10

I am honored to be in the same boat with you. Happy July 4th! - Linlin

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to choose a machine learning algorithm

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.

Discussion stats
  • 12 replies
  • 1939 views
  • 0 likes
  • 5 in conversation