BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
buechler66
Barite | Level 11
%let RULE=A.ROWID IN (SELECT TABLE_ROWID FROM IV_RPT.COMPARE_ERROR);

 

 

I have a macro variable defined as above.  I need to search this value sometimes and replace ' IV_RPT.' with

' IVPRL.' before the macro variable is referenced later in my program.  Does SAS offer a Replace function of some type?  Any suggestions would be greatly appreciated.  Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User
%let rule=A.ROWID IN (SELECT TABLE_ROWID FROM IV_RPT.COMPAE_ERROR);

data _null_;
rule = tranwrd("&rule","IV_RPT.","IVPRL.");
call symput("rule",trim(rule));
run;

%put rule=&rule;

If you want it to be more parameterized, wrap it into a macro:

%let rule=A.ROWID IN (SELECT TABLE_ROWID FROM IV_RPT.COMPARE_ERROR);

%macro tran_mac(macvar,previous,replace);
%global &macvar;
data _null_;
length string $1000; * make big enough for all use cases;
string = tranwrd("&&&macvar.","&previous","&replace");
call symput("&macvar",trim(string));
run;
%mend;

%tran_mac(rule,IV_RPT.,IVPRL.);

%put rule=&rule;

View solution in original post

11 REPLIES 11
ptimusk
Obsidian | Level 7

I would think to use PERL. PRXChange could work here.  Here is a paper on this http://analytics.ncsu.edu/sesug/2012/CT-03.pdf and here is some documentation on it http://support.sas.com/documentation/cdl/en/lefunctionsref/63354/HTML/default/viewer.htm#n0frf578x6v...

 

ptimusk
Obsidian | Level 7

If you do use PERL can you makr my reply as a solution. thanks.

data_null__
Jade | Level 19

Would it be easier to make another macro variable to hold that part of the string and set that value?

 

You can TRANSTRN using SYSFUNC:  %SYSFUNC(TRANSTRN(source,from,to)) 

 

%let rule = %sysfunc(transtrn(%superq(rule),IV_PRT,IVPRL));

buechler66
Barite | Level 11
Hmm...doesn't seem to be working.
data_null__
Jade | Level 19

Hmm I left the testing for you.

 


@buechler66 wrote:
Hmm...doesn't seem to be working.

 

 

AhmedAl_Attar
Ammonite | Level 13

Here is a simpler solution

 

%let RULE=A.ROWID IN (SELECT TABLE_ROWID FROM IV_RPT.COMPARE_ERROR);
%let RULE=%sysfunc(tranwrd(%superq(RULE),IV_RPT,IVRPT));
%put RULE=%superq(RULE);

buechler66
Barite | Level 11
It has to be done on the fly because the macro variable value actually comes from an Oracle table that stores the string. So I'm going to have to figure out a way to convert it. 😞
ptimusk
Obsidian | Level 7

The PERL solution can help you be dynamic or 'on the fly'

AhmedAl_Attar
Ammonite | Level 13

IF you are extracting the string value from a table, then you can do the string replacement within the sql using the TRANWRD/TRANSTRN functions!!

 

Depending on what kinda of SQL statement you are using, Implecit vs. Explicit, the function call location need to change to ensure optimal performance.

 

 

 

ronan
Lapis Lazuli | Level 10

I agree : unknowingly, I came up with the same solution. 😉
%superq is not necessary, though. %bquote is sufficient.
The requester is always right, they say.

Kurt_Bremser
Super User
%let rule=A.ROWID IN (SELECT TABLE_ROWID FROM IV_RPT.COMPAE_ERROR);

data _null_;
rule = tranwrd("&rule","IV_RPT.","IVPRL.");
call symput("rule",trim(rule));
run;

%put rule=&rule;

If you want it to be more parameterized, wrap it into a macro:

%let rule=A.ROWID IN (SELECT TABLE_ROWID FROM IV_RPT.COMPARE_ERROR);

%macro tran_mac(macvar,previous,replace);
%global &macvar;
data _null_;
length string $1000; * make big enough for all use cases;
string = tranwrd("&&&macvar.","&previous","&replace");
call symput("&macvar",trim(string));
run;
%mend;

%tran_mac(rule,IV_RPT.,IVPRL.);

%put rule=&rule;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 11 replies
  • 17310 views
  • 2 likes
  • 6 in conversation