LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (October 1996, week 2)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Mon, 14 Oct 1996 15:09:03 -0700
Reply-To:     Chris B Long <Chris_B_Long@SANDWICH.PFIZER.COM>
Sender:       "SAS(r) Discussion" <SAS-L@UGA.CC.UGA.EDU>
From:         Chris B Long <Chris_B_Long@SANDWICH.PFIZER.COM>
Organization: Pfizer Central Research, Sandwich, Kent, UK
Subject:      Re: 'Proper' use of IF-THEN-ELSE (was IF-THEN-ELSEs in a DATA
              step)

AMICELI wrote: Chris writes: >>This question raises a question of programming style, as well as problems with nested IF statements. In much of the user-written code that I see, there's actually no need to use nested IF statements at all. If the conditions of your various IF statements are mutually exclusive, why not just code them as sequential IF statements? For example, I often see things like:

if name='Fred' then do; ... end; else if name='Mary' then do; ... end; else if name='Ann' then do; ... end;

This is logically equivalent to sequential IF statements:

if name='Fred' then do; ... end; if name='Mary' then do; ... end; if name='Ann' then do; ... end;

but the latter doesn't involve any problems with potential stack overflows etc.

<CUT> >>>

Just a note from a compiler perspective. The following code: If a=1 then do; %blah; end; else do; %blahblah; end;

is NOT NESTED. This is merely a set of mutually exclusive conditions.

----------------------

Agreed - but I was referring to the common construct:

if a=1 then do; <something> end; else if a=2 then do; <another thing> end;

which is different from your example, and *is* nested - the second IF statement is nested within the ELSE clause of the first one. The nesting is less obvious than it should be because the indentation which is commonly used with this sort of construct is misleading - it should strictly be:

if a=1 then do; <something> end; else if a=2 then do; <another thing> end;

to show that the second END is paired with the DO of the second IF statement. (What I regard as the aesthetic unpleasantness of this construct is revealed when indented like this!)

Chris.


Back to: Top of message | Previous page | Main SAS-L page