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;
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.
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.
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
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.