BookmarkSubscribeRSS Feed
khomer
Calcite | Level 5
The following is a little code to help explain my problem:

DATA T;
LENGTH S $10 A B C $2;

A='01';
B='02';
C='A';

S=???(C);
RUN;

When C takes on the value 'A' then I want S to take on the value of '01' (The value of the variable A). When C takes on the value 'B' then I want S to take on the value of '02' (The value of the variable B).

Any ideas on how I can do this?

Thanks,

khomer
4 REPLIES 4
Dave_SAS
SAS Employee
Here's one approach. You need to use formats to provide the decodes for A and B, and then apply that format to whatever value C takes on. In the attached sample code, provided with no guarantees, I've been very loose with the issues of case, variable types and variable lengths, but hopefully this will be enough to point you in the right direction.

proc format;
value $ myfmts
"a"="01"
"b"="02";
run;

data t;
length s $10;

c="a";
s=put(c,$myfmts.);
put "First example: " s=;

c="b";
s=put(c,$myfmts.);
put "Second example: " s=;
run;
ScottBass
Rhodochrosite | Level 12
Hi,

I don't take credit for this, I'm just passing on a tip I saw on SAS-L. Credit goes to Toby Dunn:

DATA T;
LENGTH S $10 A B C $2;

A='01';
B='02';

C='A';
S=vvaluex(C);
put S=;

C='B';
S=vvaluex(C);
put S=;

RUN;

So, look up the function vvaluex. Oh, and you didn't say what version of SAS you're using. Hope it's 9.x.

HTH,
Scott

Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.
deleted_user
Not applicable
Hello Scott,

How does vvaluex bring a difference with the example quoted??

even if we equate S=C; this means the same, pls correct me if am wrong?? btw i am not challenging the functionality of vvaluex...

Best Cheers
Rao
Cynthia_sas
SAS Super FREQ
Hi!
If you have a statement such as S=C, then you will not get the desired results (I don't think so, anyway.) Maybe this alternate program will help explain it. Consider this scenario. I have some data with home emails, work emails and and a variable that contains the person's actual email that they said they wanted to be the one used.

I want a new variable (SendTo) created in the data that contains either WORK or HOME e-mail as the value of the SendTo column.

So, first, let's say that the data looks like this. And the variables are Name, Home, Work, and Use
[pre]
"Anne", "anne@aa.com", "anne@qq.com", "Home"
"Bob", "Bob@bb.com", "Bob@rr.com", "Work"
"Chet", "Chet@cc.com", "Chet@tt.com", "Work"
"Diane", "Diane@dd.com", "Diane@tt.com", "Home"
"Eudora", "Eudora@ee.com", "Eudora@rr.com", "Home"
"Fiona", "Fiona@ff.com", "Fiona@xx.com", "Other"
[/pre]
and that the data lives in a file called email_info.txt in my c:\temp directory. Next, I need to read the data and make the SendTo column:
[pre]
DATA T;
LENGTH SendTo Home Work $20 Use $5;
infile 'c:\temp\email_info.txt' dsd;
input name $ Home $ Work $ Use $ ;
SendTo=vvaluex(Use);
run;
[/pre]
So the key statement is where the SendTo variable will take on the value of whereever the Use variable points to (either Work or Home).

Then if you do a proc print on the final data set (after running the above program), you will see:
[pre]
Obs name Home Work Use SendTo
1 Anne anne@aa.com anne@qq.com Home anne@aa.com
2 Bob Bob@bb.com Bob@rr.com Work Bob@rr.com
3 Chet Chet@cc.com Chet@tt.com Work Chet@tt.com
4 Diane Diane@dd.com Diane@tt.com Home Diane@dd.com
5 Eudora Eudora@ee.com Eudora@rr.com Home Eudora@ee.com
6 Fiona Fiona@ff.com Fiona@xx.com Other
[/pre]

In this example, vvaluex acted as a lookup within one row. For Anne's row, the value of Use was Home.
So when SAS was on Anne's row of data, imagine that this is what happened:[pre]
SendTo=vvaluex(Use) --> SendTo=vvaluex(Home) --> SendTo=anne@aa.com[/pre]

On the other hand, for Chet's row, the resolution for the function would be:[pre]
SendTo=vvaluex(Use) --> SendTo=vvaluex(Work) --> SendTo=Chet@tt.com[/pre]

It's hard to figure out exactly what the original person needed to do...since the data was made up data. But, if I had used SendTo = Use as the assignment statement in my code, then I would NOT have gotten the actual email address for the value of SendTo, I would just have gotten either Home or Work (or whatever the value of Use was).

There are a lot of ways to do lookups with SAS, including, but not limited to, Proc Format, IF statements, Merge, Join -- most of these involve a fixed list. On the other hand, the VVALUEX function allows you to do a lookup on the same row. Tech Support may be your best bet for solving these kinds of problems because each method has pros and cons that can change depending on the kind of data you have, the kind of lookup you need to do and the size of the tables involved or the the kind of variables involved.
Good luck!
cynthia

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!

New Learning Events in April

 

Join us for two new fee-based courses: Administrative Healthcare Data and SAS via Live Web Monday-Thursday, April 24-27 from 1:00 to 4:30 PM ET each day. And Administrative Healthcare Data and SAS: Hands-On Programming Workshop via Live Web on Friday, April 28 from 9:00 AM to 5:00 PM ET.

LEARN MORE

Discussion stats
  • 4 replies
  • 1178 views
  • 0 likes
  • 5 in conversation