Solved
Contributor
Posts: 20

# How is it stored this boolean expression?

Which way is faster?

Suppose I want to write the expression (k=1)*e+(k=2)*f and suppose that e,f need to be calculated. Does SAS calculate e,f , then (k=1)*e+(k=2)*f  or does it evaluate first the boolean expressions (k=1) and (k=2) and then the corresponding e if k=1 is true or f if k=2 is true.

If SAS calculates both e and f  then it is more efficient to do a conditional

if k=1 then do e;

else do f;

Accepted Solutions
Solution
‎04-01-2013 02:58 PM
Super User
Posts: 6,785

## Re: How is it stored this boolean expression?

Yes, all the math will get performed.  You might get some savings by switching as you suggest:

if k=1 then ...;

else if k=2 then ...;

In addition, this code works faster if k=1 most of the time.  If k=2 most of the time, you could switch and test for k=2 first.  But truthfully, the savings will be tiny, perhaps not even measurable.  If you are interested in efficiency, you might post more of your code and get suggestions about how to speed it up.

Good luck.

All Replies
Posts: 3,852

## Re: How is it stored this boolean expression?

Why don't you write some code to test your assumption?  Do each a million times and observe the time difference.  You will need options FULLSTIMER.

Regular Contributor
Posts: 227

## Re: How is it stored this boolean expression?

Here are references to help you understand the order of evaluation of an expression:

Logic: and, not, or

SAS(R) 9.3 Language Reference: Concepts, Second Edition

arithmetic:

SAS Operators in Expressions

SAS(R) 9.3 Language Reference: Concepts, Second Edition

Ron Fehd  knot logically lazy maven

Solution
‎04-01-2013 02:58 PM
Super User
Posts: 6,785

## Re: How is it stored this boolean expression?

Yes, all the math will get performed.  You might get some savings by switching as you suggest:

if k=1 then ...;

else if k=2 then ...;

In addition, this code works faster if k=1 most of the time.  If k=2 most of the time, you could switch and test for k=2 first.  But truthfully, the savings will be tiny, perhaps not even measurable.  If you are interested in efficiency, you might post more of your code and get suggestions about how to speed it up.

Good luck.

🔒 This topic is solved and locked.

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

Discussion stats
• 3 replies
• 240 views
• 6 likes
• 4 in conversation