BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Bogdan
Calcite | Level 5

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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.

View solution in original post

3 REPLIES 3
data_null__
Jade | Level 19

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.

Ron_MacroMaven
Lapis Lazuli | Level 10

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

Astounding
PROC Star

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.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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