Writing Efficient Testing Conditions
When you code a complex test condition, think about the order in which you specify the subconditions. For example:
if age ge 80 or sex=’M’;
is going to take longer on average to execute than:
if sex=’M’ or age ge 80;
Why? Well if our data was a random sample from the population, we would expect about half of the subjects in our dataset to be male, but only a small fraction of them to be 80 or over. If we test the sex first, half the time we won’t need to test the age at all as the observation will have already been included, as a result this technique has become known as Boolean short circuiting.
if hometown in (“Whitby”, “Leeds”, “Bradford”);
could be made more efficient by re-ordering the towns. Whitby is a much smaller town than the other two, so it makes sense to test for Leeds or Bradford first as SAS will stop testing once a true condition has been found.
When conditions are ANDed rather than ORed together, similar but opposite considerations apply.
if age ge 80 and sex=’M’;
is the correct way round, since a high proportion of candidates will fail the first part of the test, so that we won’t need to check the second.