DATA Step, Macro, Functions and more

Problem in using tranwrd function

Reply
New Contributor
Posts: 2

Problem in using tranwrd function

/*my problem is that tranwrd function is not working while using in macro with looping and passing macro var..*/
/*Problem to resolve*/

%let mvar1= rahim1 ramu1 sham1;
%let mvar= rahim ramu sham;
%let val=3;

data testing22;
rahim="good";
ramu="cool";
sham="fine";
rahim1="rahim";
ramu1="ramu";
sham1="akram";
a="from rahim";output;
a="from ramu";output;
a="from akram";output;
a="from aram";output;
a="from &sham";output;
a="from &sham;";output;
rahim="good";output;
ramu="cool";output;
sham="fine";output;
rahim1="rahim";output;
ramu1="ramu";output;
sham1="akram";output;
run;


options mprint mlogic symbolgen;
%MACRO Q4;
%do i=1 %to &val;
%LET m=%SCAN("&mvar",&i," ") ;
%LET k=%SCAN("&mvar1",&i," ") ;
data testing222;
set testing22;
F8=TRANWRD(a,trim(&k),trim(&m));* here this function is not working but htere is no error in log as well tried compress and other vars as well but nothing is working is there any other function also to replace the word in a string;
run;
%end;
%MEND;

%q4;

/*this is working fine without macro or do loop if we pass the var names manual.*/

data testing222;
set testing22;
F8=TRANWRD(a,rahim1,rahim);
run;
Respected Advisor
Posts: 3,894

Re: Problem in using tranwrd function

Hi

The tranwrd() function works o.k. but the macro might do something different than you think.

Run below code and have a look at the log. May be this will give you the explanation what's going on.

%LET SHAM=akram;
%let mvar1= rahim1 ramu1 sham1;
%let mvar= rahim ramu sham;
%let val=3;

data testing22;

rahim="good";
ramu="cool";
sham="fine";

rahim1="rahim";
ramu1="ramu";
sham1="akram";

a="from rahim";output;
a="from ramu";output;
a="from akram";output;
a="from aram";output;
a="from &sham";output;
a="from &sham;";output;

run;


options nomprint nomlogic nosymbolgen;
%MACRO Q4;
data testing222;
set testing22;
put / "_n_ is" _n_;
%do i=1 %to &val;
%LET m=%SCAN(&mvar,&i," ") ;
%LET k=%SCAN(&mvar1,&i," ") ;
put '&i is ' "&i";
F8=TRANWRD(a,strip(&k),strip(&m));
PUT F8=;
%end;

run;
%MEND;

%q4;


HTH
Patrick
New Contributor
Posts: 2

Re: Problem in using tranwrd function

Thanks for the reply Patrick,

I already found the solution, I was using different variable but I should use the same variable in tranward function so that it should not be overwritten each time with the loop.If we use the same variable it will it will be updated everytime with the loop.

Regards,
Saurabh Diwaker
Super User
Posts: 9,681

Re: Problem in using tranwrd function

Hi.Patrick is right.
To avoid to overwrite F8,you need to change the code a little bit to retain the changed value of F8.

[pre]
%let mvar1= rahim1 ramu1 sham1;
%let mvar= rahim ramu sham;
%let val=3;

data testing22;
rahim="good";
ramu="cool";
sham="fine";
rahim1="rahim";
ramu1="ramu";
sham1="akram";
a="from rahim";output;
a="from ramu";output;
a="from akram";output;
a="from aram";output;
a="from &sham";output;
a="from &sham;";output;
rahim="good";output;
ramu="cool";output;
sham="fine";output;
rahim1="rahim";output;
ramu1="ramu";output;
sham1="akram";output;
run;

options mprint;
%MACRO Q4;

data testing222;
set testing22;
F8=a;
%do i=1 %to &val;
%LET m=%SCAN("&mvar",&i," ") ;
%LET k=%SCAN("&mvar1",&i," ") ;
F8=TRANWRD(F8,&k,&m);
%end;

run;

%MEND;

%q4

[/pre]


Ksharp
Ask a Question
Discussion stats
  • 3 replies
  • 196 views
  • 0 likes
  • 3 in conversation