BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sas_null_
Calcite | Level 5

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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;

View solution in original post

3 REPLIES 3
Reeza
Super User

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;

PGStats
Opal | Level 21

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
sas_null_
Calcite | Level 5

@ PGStat Thanks. Your answer works as well

SAS Innovate 2025: Register Now

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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 3595 views
  • 6 likes
  • 3 in conversation