DATA Step, Macro, Functions and more

Use tranwrd to replace string - ONLY FROM STARTING OF A WORD

Accepted Solution Solved
Reply
Contributor
Posts: 42
Accepted Solution

Use tranwrd to replace string - ONLY FROM STARTING OF A WORD

Hi all,

 

I want to replace the string "he_is_nice" with "cool" whenever it appears on a column. I can easily do it using tranwrd function.

 

The catch is, I only want to replace it if it's the starting of a word, not when it's in the middle. For example let's say if I have following var column, the result would be:

 

varresult_var
she_is_niceshe_is_nice
oh_he_is_niceoh_cool
he_is_coolhe_is_cool
well_he_is_nice_to_be_withwell_cool_to_be_with
ohhe_is_niceohhe_is_nice

 

 

So, I don't want to replace when it's starting from the middle of a word (my delimiter is '_'). Is it easy to do with tranwrd? Thanks!


Accepted Solutions
Solution
‎12-21-2017 06:38 AM
PROC Star
Posts: 1,209

Re: Use tranwrd to replace string - ONLY FROM STARTING OF A WORD

One way..

 

data have;
input var:$50.;
datalines;
she_is_nice
oh_he_is_nice
he_is_cool
well_he_is_nice_to_be_with
ohhe_is_nice
;

data want;
	set have;
	resultvar=var;
	if substr(var, find(var, "he_is_nice")-1, 1) in ("_", "") 
		then resultvar=tranwrd(var, "he_is_nice", "cool");
run;

View solution in original post


All Replies
Occasional Contributor
Posts: 19

Re: Use tranwrd to replace string - ONLY FROM STARTING OF A WORD

How about:

 

 

if var =: 'he_is_nice' then
  result_var = tranwrd(var, 'he_is_nice', 'cool');
else
  result_var = var;

 

Contributor
Posts: 42

Re: Use tranwrd to replace string - ONLY FROM STARTING OF A WORD

@lethcons doesn't work. ':'  is there to tell it it's the beginning of the phrase? That's not the case though. Also the delimiter is different.

Solution
‎12-21-2017 06:38 AM
PROC Star
Posts: 1,209

Re: Use tranwrd to replace string - ONLY FROM STARTING OF A WORD

One way..

 

data have;
input var:$50.;
datalines;
she_is_nice
oh_he_is_nice
he_is_cool
well_he_is_nice_to_be_with
ohhe_is_nice
;

data want;
	set have;
	resultvar=var;
	if substr(var, find(var, "he_is_nice")-1, 1) in ("_", "") 
		then resultvar=tranwrd(var, "he_is_nice", "cool");
run;
Contributor
Posts: 42

Re: Use tranwrd to replace string - ONLY FROM STARTING OF A WORD

Perfect, thanks!

PROC Star
Posts: 1,209

Re: Use tranwrd to replace string - ONLY FROM STARTING OF A WORD

Anytime, glad to help Smiley Happy

Super User
Super User
Posts: 9,376

Re: Use tranwrd to replace string - ONLY FROM STARTING OF A WORD

Perhaps you could provide the full question you want to ask.  You have several posts asking more or less exactly the same thing, for example:

https://communities.sas.com/t5/Base-SAS-Programming/Extract-a-portion-of-a-string-variable-from-colu...

 

You have yet to tell me what is wrong with the code I provided you which loops over each word delimited - in this way you could check word{i-2}+word{i-1}+word{i} = your given string, however I am not going to further waste my time trying to guess what you want.  Provide a clear question, with test data in the form of datastep (i.e. so we don't have to type it all in and guess what structure), and a clear example of what you want out.

Contributor
Posts: 42

Re: Use tranwrd to replace string - ONLY FROM STARTING OF A WORD

Sorry @RW9, I am new to sas (having mostly used 'R' previously) and trying to learn things through example as I find documentations a bit difficult to understand in some cases.

 

And your code was perfectly fine, it did work. This question was a simpler one, but since I'm dealing with lot of strings, I'm trying to learn about trim, substr, scan, compress, find, tranwrd, prxchange, prxparse, prxnext etc etc. as much as possible through examples. And I'll provide test data in the form of datastep for future questions. Thanks!

Super User
Posts: 10,679

Re: Use tranwrd to replace string - ONLY FROM STARTING OF A WORD

data have;
input var:$50.;
datalines;
he_is_nice
she_is_nice
oh_he_is_nice
he_is_cool
well_he_is_nice_to_be_with
ohhe_is_nice
he_is_nice_xx
;

data want;
	set have;
	resultvar=prxchange("s/\bhe_is_nice\b/cool/",-1,var);
	resultvar=prxchange("s/_he_is_nice_/_cool_/",-1,resultvar);
	resultvar=prxchange("s/_he_is_nice\b/_cool/",-1,resultvar);
	resultvar=prxchange("s/\bhe_is_nice_/cool_/",-1,resultvar);
run;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 405 views
  • 0 likes
  • 5 in conversation