SAS Programming

DATA Step, Macro, Functions and more
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?



sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1913 views
  • 3 likes
  • 3 in conversation