DATA Step, Macro, Functions and more

multiple or comparison

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

multiple or comparison


 

I have a group of variables , say b1-b30 and i need to test same condition on same variables among themselves like, if b1='a' or b2='a' or b3='a'.. and so on. is there any solution in SAS macros or using of sas arrays to do this task to avoid writing this explicitly. sample code is like this:

data test;
    input d1 d2 d3 d4 d5 ;
cards;
1 2 1 1 0
2 3 1 0 0 
0 0 0 1 0
0 2 1 0 2
0 4 0 2 2
0 0 0 0 3
;
run;

data want;
    set test;
    if d1=1 or d2=1 or d3=1 or d4=1 then flag=1;
    else flag=0;
run;

so i have around 50 variable , to test same condition and flag it .


Accepted Solutions
Solution
‎04-12-2016 01:26 PM
Trusted Advisor
Posts: 1,204

Re: multiple or comparison

Yes, this is doing OR comparison. This says if any of the d variable equals to 1 then flag will be assigned a value of 1 and 0 otherwise.

View solution in original post


All Replies
Trusted Advisor
Posts: 1,204

Re: multiple or comparison

data want(drop=i);

set test;
flag=0;
array d(*) d:;
do i=1 to dim(d);
if d(i)=1 then flag=1;
end;
run;

New Contributor
Posts: 4

Re: multiple or comparison

[ Edited ]

Hi , thanks for quick reply, is this doing OR comparison among the variables? kindly explain and also in case we want to use "AND" operator, then how will it work

Solution
‎04-12-2016 01:26 PM
Trusted Advisor
Posts: 1,204

Re: multiple or comparison

Yes, this is doing OR comparison. This says if any of the d variable equals to 1 then flag will be assigned a value of 1 and 0 otherwise.

Super User
Posts: 10,483

Re: multiple or comparison

The code provided by @stat_sas assigns a default value of 0 to the flag (the Else assignment in the original post). Then it tests each variable in the array for the test value. If any of them match then the flag is set to 1.

So it will provide the equivalent of  var1=1 or var2=1 or ..;

 

New Contributor
Posts: 4

Re: multiple or comparison

thanks @ballarw . just curious to know, if in case we have "and " operator

Trusted Advisor
Posts: 1,115

Re: multiple or comparison

[ Edited ]

Hi @ved_sas2014,

 

If I may step in here: Sure, a slight modification of @stat_sas's code could be used to set FLAG=1 if (and only if) the condition di=1 is true for all i=1, ..., 5 (edit: and FLAG=0 otherwise):

 

data want2(drop=i);
set test;
flag=1;
array d(*) d:;
do i=1 to dim(d);
  if d(i)~=1 then flag=0;
end;
run;

This makes use of the fact that

(d1=1 and d2=1 and ... d5=1)

is logically equivalent to

not (d1~=1 or d2~=1 or ... d5~=1)

 

 

Trusted Advisor
Posts: 1,115

Re: multiple or comparison

Here is an alternative solution to your original question: You can apply the IN operator to an array.

data want;
set test;
array d d:;  /* Your IF condition rather suggests: array d d1-d4; */
flag = 1 in d;
run;

The condition "1 in d" (meaning that 1 is contained in the set {d(1), d(2), ...}, where the last element in the set depends on the array definition, see above) evaluates to 1 (true) or 0 (false).

 

Super User
Posts: 17,784

Re: multiple or comparison

Look at the WHICHN function. It doesn't return a 1/0 but the index of where the 1 is found. 

For character variables there's a WHICHC function.

 

found=whichn(1, of d1-d100);
Trusted Advisor
Posts: 1,115

Re: multiple or comparison


Reeza wrote:

Look at the WHICHN function. It doesn't return a 1/0 but the index of where the 1 is found. 

For character variables there's a WHICHC function.

 

found=whichn(1, of d1-d100);

... and if you need a 1/0 flag, you can simply write:

flag = whichn(1, of d1-d100)>0;

(or, still shorter: ... of d: ... if the variables whose names start with "d" are just d1, ..., d100).

So, you can even save the ARRAY statement.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 9 replies
  • 603 views
  • 1 like
  • 5 in conversation