Dear experts, I would ask a clarification. Over a set of %if statement, a final %else would be applied to the latest %if condition, or to any of the previous %if conditions ? I believe it's the first one, so question is, how to skip the final %else if any of the previous %if is satisfied?
The net result is just the way @Quentin described it. However, you may see documentation that says that %else is only executed under two conditions:
This is also correct! How can that also be true?
Consider applying those conditions to Quentin's example. On the second line, looking at &B, clearly the logic to execute that line depends on:
When both of those bullet points are true, the software executes %if &B %then ....;
When &A was true, the software skips evaluating %if &B %then ....;
So what happens when we get to &C? The software checks:
So when &B was false, the software skips evaluating &C, and never executes %if &C %then ....;
In essence, with a set of %else statements, each one always means was the previous %if condiition evaluated and found to be true. However, the result is the one you hope for: %else requires all the previous %if conditions to be true. The logic behind that, however, is that once a true condition is found, the next %else statement is skipped. Once one is skipped, all the rest are skipped.
Your questions isn't very clear. Can you show an example of what you are trying to do?
I think the answer might be to use %ELSE %IF.
So you can code:
%if &A %then %put A is true;
%else %if &B %then %put B is true;
%else %if &C %then %put C is true;
%else %put A is false and B is false and C is false;
With that structure, the code on the %ELSE statement is only executed if all of the prior %IF and %ELSE %IF statements are false.
The net result is just the way @Quentin described it. However, you may see documentation that says that %else is only executed under two conditions:
This is also correct! How can that also be true?
Consider applying those conditions to Quentin's example. On the second line, looking at &B, clearly the logic to execute that line depends on:
When both of those bullet points are true, the software executes %if &B %then ....;
When &A was true, the software skips evaluating %if &B %then ....;
So what happens when we get to &C? The software checks:
So when &B was false, the software skips evaluating &C, and never executes %if &C %then ....;
In essence, with a set of %else statements, each one always means was the previous %if condiition evaluated and found to be true. However, the result is the one you hope for: %else requires all the previous %if conditions to be true. The logic behind that, however, is that once a true condition is found, the next %else statement is skipped. Once one is skipped, all the rest are skipped.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.