BookmarkSubscribeRSS Feed
Shone
Calcite | Level 5

Hi I have a  macro variable called list when I used tranwrd it matches everything
%let list=apple ball cats bapplee;
%let i=1 ;

 

 

%let dependent=%scan(&list,&i," ");/* list of dependent variable*/
%let independent = %sysfunc(tranwrd(&list, &dependent.,%str()));


%put &dependent;
%put &independent;

 

Result I am getting is ball cats b e 

 

Result I want is ball cats bapplee

 

How can I get the expected result? 


Thank you, 

Shone

1 REPLY 1
AMSAS
SAS Super FREQ

Hi,

 

From the documentation:

The Basics

The TRANWRD function copies the value in source to the result string while searching for all non-overlapping substrings in source that are equal to the value in target. Each of these substrings is omitted from the result and the value of replacement is copied in its place. The TRANWRD function does not remove trailing blanks in the target string or the replacement string.
A good place to start is to first remove complexity by removing all the macro references e.g. 

data _null_ ;
	x=tranwrd("apple ball cats bapplee","apple","xxx") ;
	put x= ;
run ;
When you run this then you can understand what TRANWRD is doing:
149  data _null_ ;
150      x=tranwrd("apple ball cats bapplee","apple","xxx") ;
151      put x= ;
152  run ;

x=xxx ball cats bxxxe
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
As you can see every occurrence of "apple" is replaced by "xxx" which is why "bapplee" is transformed to bxxxe

So how do you get the result you are looking for, well that's where it gets a little tricky as I can't be sure of your data, but one option could be to add a space after the target:
218  data _null_ ;
219      x=tranwrd("apple ball cats bapplee","apple "," ") ;
220      put x= ;
221  run ;

x=ball cats bapplee
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
But that would depend on your data and what you want to achieve 

I hope this helps

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!

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
  • 1 reply
  • 808 views
  • 1 like
  • 2 in conversation