BookmarkSubscribeRSS Feed
SDIWAKER
Calcite | Level 5
/*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;
3 REPLIES 3
Patrick
Opal | Level 21
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
SDIWAKER
Calcite | Level 5
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
Ksharp
Super User
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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 3 replies
  • 1559 views
  • 0 likes
  • 3 in conversation