Using IF-THEN/ELSE Statements Instead of Multiple IF
Statements - SAS Programming

When you are testing for mutually exclusive conditions, use IF-THEN/ELSE statements rather than a series of IF statements.

Example– INEFFICIENT

DATA ONE;
SET TWO;
IF 0 LE AGE LE 10 THEN AGEGRP=1;
IF 10 LT AGE LE 20 THEN AGEGRP=2;
IF 20 LT AGE LE 30 THEN AGEGRP=3;
IF 30 LT AGE LE 40 THEN AGEGRP=4;
IF AGE GT 40 THEN AGEGRP=5;
RUN;

This code looks straightforward and will get the job done, but there is a large amount of totally unnecessary work being done. The next method shown is definitely the preferred method hands-down — no debating.

Example – EFFICIENT

DATA ONE;
SET TWO;
IF 0 LE AGE LE 10 THEN AGEGRP=1;
ELSE IF 10 LT AGE LE 20 THEN AGEGRP=2;
ELSE IF 20 LT AGE LE 30 THEN AGEGRP=3;
ELSE IF 30 LT AGE LE 40 THEN AGEGRP=4;
ELSE IF AGE GT 40 THEN AGEGRP=5;
RUN;

In the inefficient example, all the IF statements have to be executed, even if a previous IF statement was true. In the efficient example, as soon as an IF statement is true, no subsequent ELSE statements are executed.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

SAS Programming Topics