BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
pink_poodle
Barite | Level 11

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
Tom
Super User Tom
Super User

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

Tom_0-1652904986733.png

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.

View solution in original post

4 REPLIES 4
Tom
Super User Tom
Super User

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

Tom_0-1652904986733.png

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.

ballardw
Super User

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.

pink_poodle
Barite | Level 11

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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 404 views
  • 5 likes
  • 4 in conversation