- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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:
var | result_var |
she_is_nice | she_is_nice |
oh_he_is_nice | oh_cool |
he_is_cool | he_is_cool |
well_he_is_nice_to_be_with | well_cool_to_be_with |
ohhe_is_nice | ohhe_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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
How about:
if var =: 'he_is_nice' then
result_var = tranwrd(var, 'he_is_nice', 'cool');
else
result_var = var;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Perfect, thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Anytime, glad to help 🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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:
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;