What is wrong with this array (and what is the correct one)?

Reply
Frequent Contributor
Posts: 75

What is wrong with this array (and what is the correct one)?

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=.;

end;run;

The result looks wrong; well, in fact it's wrong. How can I make the right one?

Super User
Posts: 5,424

Re: What is wrong with this array (and what is the correct one)?

Posted in reply to NonSleeper

You don't need a do loop, use this instead:

newvar=max(of var1-var5);

Data never sleeps
Super User
Posts: 10,018

Re: What is wrong with this array (and what is the correct one)?

Posted in reply to NonSleeper

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;

end;



Xia Keshan

Frequent Contributor
Posts: 75

Re: What is wrong with this array (and what is the correct one)?

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.

Super User
Posts: 10,018

Re: What is wrong with this array (and what is the correct one)?

Posted in reply to NonSleeper

My code is for all of variables are 1  or  all of variables are 0 .

Super Contributor
Posts: 644

Re: What is wrong with this array (and what is the correct one)?

Posted in reply to NonSleeper

Why not

newvar = MAX ( of var1 - var5) ;

?

Result is 1 if any are 1,

missing only if all are missing (as per OP)

otherwise 0

assuming var1 .. var5 can only have these values.

Richard

Ask a Question
Discussion stats
  • 5 replies
  • 239 views
  • 1 like
  • 4 in conversation