Barite | Level 11

Conditional statements

Could someone please clearly explain the difference, ideally with pictures of tables and selected observations?:

A) if v1=1 and v2=2 or v3=3;

B) if v1=1 and (v2=2 or v3=3);

Many thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Super User

Re: Conditional statements

You separated three conditions by binary operators.

``V1 and V2 or V3``

There are two ways to group those three conditions.

``````(V1 and V2) or V3
V1 and (V2 or V3)``````

Let's run an experiment by testing all possible combinations of true or false for each of the three conditions and calculate all three expressions.

``````data test;
do v1=0,1;
do v2=0,1;
do v3=0,1;
a=v1 and v2 or v3;
b=v1 and (v2 or v3);
c=(v1 and v2) or v3;
output;
end;
end;
end;
run;
``````

Results

As you can see the calculate A column is the same as the C column.

So SAS grouped the operations to perform the AND before the OR.

The meaning of (V1 and V2) or V3 is that either both V1 and V2 are true or V3 is true.

The meaning of V1 and (V2 or V3) is that  V1 is true and also at least one of V2 or V3 is true.

4 REPLIES 4
Super User

Re: Conditional statements

You separated three conditions by binary operators.

``V1 and V2 or V3``

There are two ways to group those three conditions.

``````(V1 and V2) or V3
V1 and (V2 or V3)``````

Let's run an experiment by testing all possible combinations of true or false for each of the three conditions and calculate all three expressions.

``````data test;
do v1=0,1;
do v2=0,1;
do v3=0,1;
a=v1 and v2 or v3;
b=v1 and (v2 or v3);
c=(v1 and v2) or v3;
output;
end;
end;
end;
run;
``````

Results

As you can see the calculate A column is the same as the C column.

So SAS grouped the operations to perform the AND before the OR.

The meaning of (V1 and V2) or V3 is that either both V1 and V2 are true or V3 is true.

The meaning of V1 and (V2 or V3) is that  V1 is true and also at least one of V2 or V3 is true.

Super User

Re: Conditional statements

Here is a brief example of creating some observations where the comparison is different for the two statements.

```data example;
do v1=1 to 4;
do v2 = 1 to 4;
do v3 = 1 to 4;
a= v1=1 and v2=2 or v3=3;
b=  v1=1 and (v2=2 or v3=3);
if a ne b then output;
end;
end;
end;
run;```

1 for either A or B would indicate the statement is True, 0 is false. The above code only selects those that are different.

You could remove the "if a ne b then" to get the comparison values for all the values in the output set.

Remember to work from inside parentheses outward.

You may want to look up "Order of Evaluation in Compound Expressions" in the SAS documentation

Basically A and B is true only when both elements are.

A or B is true when either or both elements is true. So when you have comparisons at the same priority of evaluation you do left to right.

Barite | Level 11

Re: Conditional statements

You are right @Tom , there are hidden parenthesis around v1 and v2 because of sequential evaluation. That decouples v3 condition from v1 condition. So v3=3 can be true on its own in (A). In (B), SAS first looks inside the parenthesis to see if either condition is true. If v3=3, that is not enough because then SAS checks the v1 condition which must also be true.

Super User

Re: Conditional statements

In Boolean logic, AND takes precedence over OR. This means that your first condition is equivalent to this:

``if (v1=1 and v2=2) or v3=3;``
Discussion stats
• 4 replies
• 496 views
• 5 likes
• 4 in conversation