## TRANSLATE function on multiple characters of a variable

Solved
Occasional Contributor
Posts: 14

# TRANSLATE function on multiple characters of a variable

data Have;

length name \$10;

Shuffle= “BCNJIWXYZ”;

input name \$ ;

datalines;

DAVID

DON

ABEL

KANE

;

New variable called NEWNAME

Translate all occurrences of “D” in name to “B”

Translate all occurrences of “A” in name to “C”

Translate all occurrences of “V” in name to “N”

Translate all occurrences of “I” in name to “J”

Translate all occurrences of “O” in name to “I”

Translate all occurrences of “N” in name to “W”

Translate all occurrences of “B” in name to “X”

Translate all occurrences of “E” in name to “Y”

Translate all occurrences of “L” in name to “Z”

Note: Translation is based on the variable Shuffle= “BCNJIWXYZ

NEWNAME for name=’DAVID’ will be ‘BCNJB’

NEWNAME for name=’DON’ will be ‘BIW’

NEWNAME for name=ABEL will be ‘CXYZ’

NEWNAME for name=KANE will be ‘KCWY’

Any help with regards to this will be helpful.

Accepted Solutions
Solution
‎11-20-2014 09:20 PM
Super User
Posts: 23,784

## Re: TRANSLATE function on multiple characters of a variable

Where does the from variables come from? i.e. Shuffle provides the to part.

A loop like the following would work assuming the TO is in a variable similar to shuffle.

data Have;

length name \$10;

TO_Shuffle= "BCNJIWXYZ";

FROM_SHUFFLE="DAVIONBEL";

input name \$ ;

datalines;

DAVID

DON

ABEL

KANE

;

DATA WANT;

SET HAVE;

i=1;

do while (scan(TO_shuffle, i) ne "");

to=scan(TO_shuffle, i);

from=scan(FROM_shuffle, i);

new_word=translate(NAME, to, from);

i+1;

end;

RUN;

All Replies
Solution
‎11-20-2014 09:20 PM
Super User
Posts: 23,784

## Re: TRANSLATE function on multiple characters of a variable

Where does the from variables come from? i.e. Shuffle provides the to part.

A loop like the following would work assuming the TO is in a variable similar to shuffle.

data Have;

length name \$10;

TO_Shuffle= "BCNJIWXYZ";

FROM_SHUFFLE="DAVIONBEL";

input name \$ ;

datalines;

DAVID

DON

ABEL

KANE

;

DATA WANT;

SET HAVE;

i=1;

do while (scan(TO_shuffle, i) ne "");

to=scan(TO_shuffle, i);

from=scan(FROM_shuffle, i);

new_word=translate(NAME, to, from);

i+1;

end;

RUN;

Posts: 5,543

## Re: TRANSLATE function on multiple characters of a variable

data Have;

length name \$10;

to= "BCNJIWXYZ";

from="DAVIONBEL";

input name \$ ;

newname=translate(name, to, from);

put name newname;

datalines;

DAVID

DON

ABEL

KANE

;

DAVID BCNJB

DON BIW

ABEL CXYZ

KANE KCWY

Note: be careful with the quote characters in your code. The right and left quotes used in your question are not understood by SAS.

PG

PG
Occasional Contributor
Posts: 14