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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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