BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
LinyuanQin
Calcite | Level 5

Hi everyone:

I've written the code as below and I expected the result in log was '123456789101112;223456789101112' but what I get is '323;223'. Would anyone tell me why the TANSLATE did not work as is described and how to approach what I want? Appreciate any help!

The code:

data _null_;

length tran $50.;

tran=translate('yhp;qly','123456789101112','yhp','223456789101112','qly');

put tran;

run;

Qinly

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Looks like it is doing what you asked.  TRANSLATE works on characters, not strings.

You told it to take the string

'yhp;qly'


And make these letter transformations:

y -> 1

h -> 2

p -> 3

q -> 2

l -> 2

y -> 3


Note that have two different targets for the letter y so the last one won and you ended up with:

'323;223'


Perhaps you wanted to use TRANWRD() or TRANSTRN() function instead?



View solution in original post

2 REPLIES 2
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

Check out the help documentation for the function: http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000215153.htm

Firstly the details section explains why the length is not as expected - it sets the new variable to be as long as the first parameter you pass in, so yhp;qly = length 7, output length is going to be 7, not all those other characters.

Also, you may want to split that statement up:

data tmp;

length tran $50.;

tran=translate('yhp;qly','123456789101112','yhp');
tran=translate(tran,'223456789101112','qly');
put tran;

run;

Or, use the tranwrd function just to replace things.

Tom
Super User Tom
Super User

Looks like it is doing what you asked.  TRANSLATE works on characters, not strings.

You told it to take the string

'yhp;qly'


And make these letter transformations:

y -> 1

h -> 2

p -> 3

q -> 2

l -> 2

y -> 3


Note that have two different targets for the letter y so the last one won and you ended up with:

'323;223'


Perhaps you wanted to use TRANWRD() or TRANSTRN() function instead?



hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 2 replies
  • 2087 views
  • 3 likes
  • 3 in conversation