Arranging the Order of Your IF Statements - SAS Programming

This hint is a bit subtle and is important only when you want to squeeze the last ounce of efficiency out of your system. This example uses the logic of the previous efficient example. As a matter of fact, the following inefficient code is exactly the same as the previous efficient code. Read on.

Example– INEFFICIENT

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;

If you know something about the contents of your data set, place the IF statements in decreasing order of probability of being true. Thus, if most of your subjects are in the 30-to-40 age group,followed by the greater-than-40 group, you should rearrange the IF-THEN/ELSE statements as follows:

Example– EFFICIENT

DATA ONE;
SET TWO;
IF 30 LT AGE LE 40 THEN AGEGRP=4;
ELSE IF AGE GT 40 THEN AGEGRP=5;
ELSE 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;
RUN;

The result here is that for most of the observations read in, the DATA step would stop processing after the first IF statement.The next largest group of observations would only need to process two IF statements, and so on. Only the smallest group of observations would have to process the entire set of IF statements.


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

SAS Programming Topics