Pages

Tuesday, 26 January 2010

NOTE: IFC and IFN, the New IF

Have you discovered SAS V9.1's new IF functions yet: IFC and IFN? They don't provide any functionality that you couldn't achieve in V8 (nor V6 nor V5 nor...) but they could make a big change to your SAS coding style.

Consider the following piece of DATA step code (please excuse the sexual stereotyping!):

if gender eq 'M' then hobby='football'; /* Male */
else hobby='knitting';
/* Female */

You will be aware that missing input data (or an earlier code/logic error) will result in the code erroneously following the female route. You might resolve this issue by coding the following:

select (gender);
  when ('M') hobby='football';
  when ('F') hobby='knitting';
  otherwise hobby='data err';
end;

The SELECT statement offers a more robust coding solution, but our code is beginning to get a bit verbose. The IFC and IFN functions resolve this issue by offering three potential outcomes, viz:

hobby = ifc(gender eq 'M','football','knitting','data err');

Do you see how your coding style might be changed significantly? I think the IFC and IFN functions might easily be dismissed as insignificant additions to the SAS language, but when we look a little deeper we see that they are really rather significant.

Check-out the V9.2 documentation for the low-down, such as default lengths for the return values.