BookmarkSubscribeRSS Feed
Dhana4ru
Calcite | Level 5

Hello,

 

Can anyone help me to replace the exact matching text in a text string please. I have used tranwrd in the below code and it is  replacing all occurrences of the text.

 

%let s= "system_id, identifier, a_amount, account_identifier, b_identifier";

data _null_;

k=tranwrd(&s,'identifier','DHANA');

run;

 

Current Output :        "system_id, DHANA, a_amount, account_DHANA, b_DHANA"

 

However, I need the output something similar below :

 

"system_id, DHANA, a_amount, account_identifier, b_identifier"

 

Note : It is not necessary that it always second please.

 

Appreciate your help on this.

 

Thanks

Dhana

 

5 REPLIES 5
RW9
Diamond | Level 26 RW9
Diamond | Level 26

First, its really not a good idea putting lists of variables in macro variables, you will run into problems and your code will get all messy with macro code dealing with it.

Second you need double quotes to dereference macro variables:

%let s= "system_id, identifier, a_amount, account_identifier, b_identifier";

data _null_;
  k=tranwrd("&s.",'identifier','DHANA');
run;
Dhana4ru
Calcite | Level 5

Thanks for your reply. I am just a beginner and going through different SAS functions.

Coming back to the below suggested code, I am afraid, some how it is not working

RW9
Diamond | Level 26 RW9
Diamond | Level 26

I would then highly advise you to learn Base SAS first and use that all the time.  Macro is an additional component for generating text, it is not the programming language.

To answer your question:

%let s= "system_id, identifier, a_amount, account_identifier, b_identifier";
%let x=%sysfunc(tranwrd(&s.,identifier,DHANA));
%put s: &s.;
%put x: &x.;
Astounding
PROC Star

It's not working because you have an extra set of double-quotes.  They are part of the value of &S, and so you should not add a second set by coding "&s." as the first parameter.  Just use &s. instead.

 

And I agree ...  you should not be using macro language at all until you know more SAS language.  The purpose of macro language is to construct a SAS program.  You need more experience to be able to visualize what an accurate SAS program looks like.

ballardw
Super User

TRANWRD as you have discovered will not work as it processes the entire string and replaces duplicates.

On way is to parse the string to find "words" that are exactly equal to your target. This is one way:

 

data example;
s= "system_id, identifier, a_amount, account_identifier, b_identifier";
length word k $ 70;
do i= 1 to countw(s);
   if upcase(scan(s,i,' ,'))='IDENTIFIER' then word='DHANA';
   else word=scan(s,i,' ,');
   k=catx(',',k,word);
end;
drop i word;
   

run;

Note that setting an appropriate length for the word and K variables is up to you if you insist on using macro variables. The example expects that your "words" are separated by spaces and commas only. If you introduce other delimiters then the SCAN code will need to be changed. The upcase is insure a case insensitive replacement. If you don't want to replace Identifier, or IDENTIFIER but only the exact case of "identifier" then remove the upcase and change the value compared to the desired form.

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 2026 views
  • 0 likes
  • 4 in conversation