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.

BASUG is hosting free webinars Next up: Don Henderson presenting on using hash functions (not hash tables!) to segment data on June 12. Register now at the Boston Area SAS Users Group event page: 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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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
  • 392 views
  • 1 like
  • 3 in conversation