Building models with SAS Enterprise Miner, SAS Factory Miner, SAS Visual Data Mining and Machine Learning or just with programming

What are the functions to repalce words but not substrings in a character string?

Reply
Contributor
Posts: 49

What are the functions to repalce words but not substrings in a character string?

Examples please

Super Contributor
Posts: 1,636

Re: What are the functions to repalce words but not substrings in a character string?

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


Contributor
Posts: 49

Re: What are the functions to repalce words but not substrings in a character string?

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

Super Contributor
Posts: 1,636

Re: What are the functions to repalce words but not substrings in a character string?

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

Frequent Contributor
Posts: 95

Re: What are the functions to repalce words but not substrings in a character string?

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.

Respected Advisor
Posts: 4,649

Re: What are the functions to repalce words but not substrings in a character string?

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
Super Contributor
Posts: 1,636

Re: What are the functions to repalce words but not substrings in a character string?

Hi PG,

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

Thanks - Linlin

Respected Advisor
Posts: 4,649

Re: What are the functions to repalce words but not substrings in a character string?

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
Super Contributor
Posts: 1,636

Re: What are the functions to repalce words but not substrings in a character string?

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

Frequent Contributor
Posts: 95

Re: What are the functions to repalce words but not substrings in a character string?

Hi PG,

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

Alpay

Respected Advisor
Posts: 4,649

Re: What are the functions to repalce words but not substrings in a character string?

I got it from , pass it on! - PG

PG
Respected Advisor
Posts: 3,124

Re: What are the functions to repalce words but not substrings in a character string?

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

Haikuo

Super Contributor
Posts: 1,636

Re: What are the functions to repalce words but not substrings in a character string?

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

Ask a Question
Discussion stats
  • 12 replies
  • 472 views
  • 0 likes
  • 5 in conversation