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 (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info at https://www.basug.org/events.
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 2025: Call for Content

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!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 661 views
  • 1 like
  • 3 in conversation