BookmarkSubscribeRSS Feed
keen_sas
Quartz | Level 8

Hi All,

I have a dataset with variable HAVE and another variable Replace consisting of some text to be replaced from HAVE to new value present in the same variable,
Old represent the value present in the HAVE variable and New is the value to be replaced in the same variabe HAVE. This has to be done dynamically by selecting the text from replace variable without manual checking.
I am trying to find the text in Old using FIND,SCAN,SUBSTRs function and then replacing the value with TRANWRD fucntion. Is there any direct approach/function/short cut to do this in much simpler way. The output variable is WANT for reference.

 

Have Replace Want
Benchmark indices registered recovery from the day's low point. (old="low", new="high") Benchmark indices registered strong recovery from the day's high point.
Completes inspection at Factory  (old="at", new="in") Completes inspection in Factory 
Patient; status; cardiac arrest (old=";",new=',') Patient, status, cardiac arrest
2 REPLIES 2
RW9
Diamond | Level 26 RW9
Diamond | Level 26

First I would suggest fixing the replace column, it will just make your code longer if you have it all in one.  Have two columns:

FIND     REPLACE

low        high

 

Will make things easier.

Second, should it replace all occurrences, what about casing?  What about low in a word, e.g. below?

Tranwrd is probably the simplest method, e.g:

want=tranwrd(have," low "," high ");

I put spaces in so as to get only th word, not be"low" for instance.

Amir
PROC Star

The points raised by @RW9 will need to be addressed, but as the question is presented the following might help.

 

/* set up data */
data have;
   infile datalines truncover;
   input have    $char70.;
   input replace $char30.;

   datalines4;
Benchmark indices registered recovery from the day's low point.
(old="low", new="high")
Completes inspection at Factory
(old="at", new="in")
Patient; status; cardiac arrest
(old=";",new=',')
That"s ok
(old='"',new="'")
This 'dataline' uses quotes
(old="'",new='"')
;;;;

data want;
   set have;

   length find_str repl_str $ 8;

   /* get 'old' string and remove quotes */
   find_str = strip(tranwrd(scanq(replace,1,','),'(old=',''));
   find_str = dequote(find_str);

   /* get 'new' string and remove quotes */
   repl_str = strip(tranwrd(scanq(replace,2,','),'new=',''));
   repl_str = substr(repl_str,1,length(repl_str) - 1);
   repl_str = dequote(repl_str);

   /* perform replace */
   want = tranwrd(have,strip(find_str),strip(repl_str));
run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1078 views
  • 0 likes
  • 3 in conversation