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

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.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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