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