Run the code in SAS
data _null_;
if 1 or sum(.) then put 'Hi!';
run;
It reports missing value was generated which shows:
1. sum(.)
is evaluated;
2. 'or' is not a short circuit operator.
I remeber SAS is wirten by C language and short circuit operator is supported in C. Why doesn't SAS support short circuit operation?
No, I don't know why some expressions short circuit and others do not. As I say in my blog post, "the SAS documentation does not discuss the conditions for which a logical expression does or does not short circuit." My experience is that simple expressions that compare a variable will probably short circuit. More complicated expressions that involve function calls might or might not short circuit.
The safest choice for defensive programming is to use nested IF statements for situations in which the second expression should not be evaluated unless the first succeeds.
Interesting, just found https://blogs.sas.com/content/iml/2019/08/14/short-circuit-evaluation-and-logical-ligatures-in-sas.h....
But your code @whymath clearly shows that "or" is not a short circuit operator.
Maybe @Rick_SAS can enlighten us.
No, I don't know why some expressions short circuit and others do not. As I say in my blog post, "the SAS documentation does not discuss the conditions for which a logical expression does or does not short circuit." My experience is that simple expressions that compare a variable will probably short circuit. More complicated expressions that involve function calls might or might not short circuit.
The safest choice for defensive programming is to use nested IF statements for situations in which the second expression should not be evaluated unless the first succeeds.
I'm not sure that the Sum is actually evaluated to use with the IF.
I think that may be a part of the general SAS programming of some functions to make sure that you are using the data you think you have in an appropriate manner.
Consider, why in general would you attempt to SUM values? You expect something as a result. So when you provide a variable, or list that is all missing values to the function then log tells you that you had some records with missing values. That may well tell you that your program logic is incorrect for your given data, your data is not as you expect, or possibly provided the wrong arguments to the function.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.