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
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?
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.
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?
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.