BookmarkSubscribeRSS Feed
marcuswong
Fluorite | Level 6

Hi Members,

 

In the SAS documentation, it says " The ELSE statement, if used, must immediately follow the IF-THEN statement."

 

I am trying to understand the above meaning but is confused when the below codes are valid and at line 6, the else statement does not immediately follow the if-then statement.

 

Anyone can enlighten me?

 

data storm_cat;
    set pg1.storm_summary;
    keep Name Basin MinPressure StartDate PressureGroup;
    if MinPressure=. then PressureGroup=.;
    else if MinPressure<=920 then PressureGroup=1;
    else PressureGroup=0;
run; 

 

8 REPLIES 8
SASKiwi
PROC Star

That's true when you have only one ELSE statement, but if you have more than one ELSE, then they can follow one after the other as long as the first ELSE in the sequence is preceded by an IF statement.

marcuswong
Fluorite | Level 6

@Tom@Kurt_BremserThank you for the guidance!

 

ChrisNZ
Tourmaline | Level 20

> the else statement does not immediately follow the if-then statement.

It does.

data storm_cat;
    set pg1.storm_summary;
    keep Name Basin MinPressure StartDate PressureGroup;
    if MinPressure=. then PressureGroup=.;
    else if MinPressure<=920 then PressureGroup=1;
    else PressureGroup=0;
run; 

The red ELSE follows the red IF, and the blue ELSE follows the blue IF.

The catch here is that the blue IF has been made part of the red ELSE statement, hence the confusion. If DO blocks were used, the sequence would be (slightly) clearer.

 

The bottom line is this: you cannot use a ELSE statement when there is not a corresponding IF-THEN just before.

marcuswong
Fluorite | Level 6

@SASKiwiand @ChrisNZ  Thank you for the guidance!

Tom
Super User Tom
Super User

Not sure why you are confused. You have two ELSE and two THEN.  Perhaps if you changed your indentation style it would be clearer?

if MinPressure=. then PressureGroup=.;
                 else if MinPressure<=920 then PressureGroup=1;
                                          else PressureGroup=0;
marcuswong
Fluorite | Level 6

@Tom I misinterpret the sentence in the SAS documentation that after "else" must follow by "if-then".

instead, I should learn it as ''else" cannot be used alone and "else" must always follow behind a "if-then".

 

Please let me know if I am learning correctly?

Tom
Super User Tom
Super User

Sounds like an issue with understanding the complexities of English language. The phrase "x follows y" and "x is followed by y" imply opposite orders for X and Y.  The first means the sequence is Y -> X and the second means X -> Y.

Kurt_Bremser
Super User

To help you understand the code, let's expand it a little, and slightly change the indentation:

data storm_cat;
set pg1.storm_summary;
keep Name Basin MinPressure StartDate PressureGroup;
if MinPressure = .
then PressureGroup = .;
else do;
  if MinPressure <= 920
  then PressureGroup = 1;
  else PressureGroup = 0;
end;
run; 

So you can see that the second if-then-else follows the exact same pattern as the first if-then-else.

Since, logically, a if-then-else can be used as a single statement, the do/end can be omitted, leading to this:

if MinPressure = .
then PressureGroup = .;
else if MinPressure <= 920
then PressureGroup = 1;
else PressureGroup = 0;

which is what you have originally.

SAS Innovate 2025: 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
  • 8 replies
  • 1623 views
  • 1 like
  • 5 in conversation