Using Multiple OR Operators instead of the IN Operator - SAS Programming

This is an interesting example that demonstrates a point that was made in the introduction to this, namely that the SAS System is continually changing and improving. When this example was first written,the title was reversed, with the IN operator being the recommended efficiency method! The way the system processes Boolean expressions was then improved to the point where the use of multiple OR operators is now very efficient, often more so than using an IN operator.

When an IF statement contains a series of comparisons separated by OR operators, as soon as a hit is made, no further tests are done.This example also demonstrates that you may very well want to use an inefficient coding method when you are not in a limited resource production environment and where the relative importance of ease of coding (the IN operator) outweighs efficiency considerations.

Example – INEFFICIENT

DATA NEW;
SET OLD;
IF GROUP IN ('A','C','E','Z')];
RUN;

Just as you arranged the order of IF-THEN/ELSE statements in the previous example, you can also arrange the order of the IN list in decreasing probability of inclusion.So, if groups Z and C are the most common, you would put them at the front of the list:

IF GROUP IN ( ' Z ' , ' C ' , ' A ' , ' E ' ) ;

The IN operator can also be used with numeric variables. For example,to print only observations corresponding to ID's 1, 5, and 13, you write:

PROC PRINT DATA=OLD;
WHERE ID IN (1,5,13);
RUN;

Even though the IN operator is a very useful SAS feature, the following code may be more efficient than the preceding.

Example – EFFICIENT

DATA NEW;
SET OLD;
IF GROUP-'A' OR GROUP='C' OR GROUP='E' OR GROUP='Z';
RUN;

Although this program requires more effort to write, it runs faster than the less efficient example.Once again, you should order the OR list in order of decreasing probability of occurrence for absolute maximum efficiency.


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

SAS Programming Topics