04-24-2014 01:05 AM
I have some variables, let's say 5, dummy coded. I want to create a variable that returns 1 if any dummy variable is 1 and 0 if all dummy variables are 0. Here's the array I got:
data temp;set temp;
array dummy(5) var1-var5;
do i=1 to 5;
if dummy=1 then newvar=1;
else if dummy=0 then newvar=0;
else if dummy=. then newvar=.;
The result looks wrong; well, in fact it's wrong. How can I make the right one?
04-24-2014 05:06 AM
“ if any dummy variable is 1”
You mean all of variables are 1 or any one of them is 1 ?
if range(of var1-var5)=0 then do;
if var1=1 then new=1;
else if var1=0 then new=0;
04-24-2014 05:13 AM
I mean if any of the dummy variables is 1, then the newvar will return the value of 1. Does your program apply for this case?
Plus, it also means if all of dummy variables are not 1 (i.e., equal 0, as they are dummy), then the newvar will return the value of 0.
04-24-2014 07:19 AM
newvar = MAX ( of var1 - var5) ;
Result is 1 if any are 1,
missing only if all are missing (as per OP)
assuming var1 .. var5 can only have these values.