Date: Tue, 23 Nov 2004 09:11:51 -0500
Reply-To: Ed Heaton <EdHeaton@WESTAT.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Ed Heaton <EdHeaton@WESTAT.COM>
Subject: Re: DATA step q: variable has name of variable to use for value
Content-Type: text/plain
Bruce;
Here is a two-step process. I don't know if you can live with that,
but it avoids much of the hard-coding of variable names.
/* 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
;
/* Now, let's use SQL to create two lists of WHEN statements. */
Proc sql noPrint ;
Select unique
catS( 'When ("' , indicate1 , '") new1 = ' , indicate1 )
, catS( 'When ("' , indicate2 , '") new2 = ' , indicate2 )
into
:new1WhenStatements separated by " ; "
, :new2WhenStatements separated by " ; "
from one
;
Quit ;
/* Finally, we will use the WHEN statements in a DATA step. */
Data two ;
Set one ;
Select (indicate1) ;
&new1WhenStatements ;
End ;
Select (indicate2) ;
&new2WhenStatements ;
End ;
Run ;
Proc print data=two ;
Run ;
Ed
Edward Heaton, SAS Senior Systems Analyst,
Westat (An Employee-Owned Research Corporation),
1600 Research Boulevard, RW-3541, Rockville, MD 20850-3195
Voice: (301) 610-4818 Fax: (301) 610-5128
mailto:EdHeaton@Westat.com http://www.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)