BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
dcortell
Pyrite | Level 9

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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:

  • The previous %if condition was evaluated, and
  • The previous %if condition was found to be false.

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:

  • SAS examined &A from the previous %if condition, and
  • SAS found that &A was false.

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:

  • Did we evaluate the previous %if condiition for &B?
  • Was it false?

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.

View solution in original post

2 REPLIES 2
Quentin
Super User

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 Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
Astounding
PROC Star

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:

  • The previous %if condition was evaluated, and
  • The previous %if condition was found to be false.

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:

  • SAS examined &A from the previous %if condition, and
  • SAS found that &A was false.

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:

  • Did we evaluate the previous %if condiition for &B?
  • Was it false?

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.

sas-innovate-wordmark-2025-midnight.png

Register Today!

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.


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
  • 2 replies
  • 811 views
  • 1 like
  • 3 in conversation