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.
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;
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;
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
@ PGStat Thanks. Your answer works as well
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.