Date: Tue, 23 Nov 2004 08:52:10 -0500
Reply-To: Mike Rhoads <RHOADSM1@WESTAT.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Mike Rhoads <RHOADSM1@WESTAT.COM>
Subject: Re: DATA step q: variable has name of variable to use for value
Content-Type: text/plain
Bruce,
This is a perfect job for the new (in SAS 9) VVALUEX function:
data two;
set one;
new1 = input(vvaluex(indicate1),best.);
new2 = input(vvaluex(indicate2),best.);
run;
Note that VVALUEX returns the formatted value of the variable pointed to by
its argument, so INPUT is used to convert this character string back to
numeric.
Mike Rhoads
Westat
RhoadsM1@Westat.com
-----Original Message-----
From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of Bruce
F. Gilsen
Sent: Monday, November 22, 2004 6:23 PM
To: SAS-L@LISTSERV.UGA.EDU
Subject: DATA step q: variable has name of variable to use for value
Here's a stripped down version of a problem one of my users had.
Data set one has the following.
var1 var2 var3 var4 indicate1 indicate2
1 2 3 4 var3 var4
5 6 7 8 var1 var2
9 10 11 12 var3 var1
He wanted to create 2 new variables, new1 and new2, whose values
are the values of the variables named in indicate1 and indicate2,
respectively. That is,
in observation 1,
indicate1="var3" and the value of var3 is 3, so new1= 3.
indicate2="var4" and the value of var4 is 4, so new2= 4.
in observation 2,
indicate1="var1" and the value of var1 is 5, so new1= 5.
indicate2="var2" and the value of var2 is 6, so new2= 6.
etc., with more obs and more variables.
There's kind of a level of indirection here, what you want in
effect is value(value(indicate1)) and value(value(indicate2).
I don't know how to do this directly, I suggested arrays.
This code worked.
/* input sample data to illustrate */
data one;
length indicate1 indicate2 $32.;
input var1 var2 var3 var4 indicate1 $ indicate2 $;
datalines;
1 2 3 4 var3 var4
5 6 7 8 var1 var2
9 10 11 12 var3 var4
;
run;
data two;
set one;
drop i; * index variable;
/* make character array with names of variables, numeric
array with their values, must be in same order */
array varall (4) $ _temporary_ ("var1" "var2" "var3" "var4");
array dataall (4) var1 var2 var3 var4;
/* loop until found both variables or check all possibles,
could both be same so do not do ELSE IF */
do i = 1 to dim(dataall) while (new1=. or new2=.);
if varall(i) = indicate1 then new1=dataall(i);
if varall(i) = indicate2 then new2=dataall(i);
end;
run;
But, I keep thinking there might be a way to directly
get the value. Any ideas?
Bruce Gilsen bruce.gilsen@frb.gov
speaking only for myself (and my fantasy baseball team, which finished
in 3rd place of 12 in 2004)