BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ss59
Obsidian | Level 7

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!

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

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

8 REPLIES 8
lethcons
Obsidian | Level 7

How about:

 

 

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

 

ss59
Obsidian | Level 7

@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.

PeterClemmensen
Tourmaline | Level 20

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;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

ss59
Obsidian | Level 7

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!

Ksharp
Super User
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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 8580 views
  • 1 like
  • 5 in conversation