I was looking at someone else's SAS/IML program when I saw this line of code:

y = sqrt(x<>0); |

The statement uses the element maximum operator (<>) in the SAS/IML language to make sure that negative value are never passed to the square root function.

This little trick is a real treat, which makes it perfect for my annual Halloween trick-and-treat post. The vector (or matrix) `x` might contain negative values, which would generate errors if passed to the SQRT function. The expression `x<>0` truncates the data at zero. That is, it creates a new vector whose *i*th element is *x _{i}* when

*x*> 0, and is 0 otherwise, as shown in the following SAS/IML program:

_{i}proc iml; x = {. -1 0 1 2 4}; y = sqrt(x<>0); print (x//y)[r={"x" "sqrt(x<>0)"} format=best4.]; |

Of course, that is not the only way to prevent negative numbers from reaching the square root function. Some people prefer to obtain a missing value for the square root of a negative number. As I showed in my article on how to handle negative values in a log function, you can use the SAS/IML CHOOSE function to return missing values:

y2 = sqrt( choose(x>=0, x, .) ); print (x//y2)[r={"x" "sqrt(x) or ."}]; |

Whichever method you prefer, it is important for statistical programmers to practice *defensive programming*.
That means that a program should be able to handle bad input values. In my blog posts, I often skip the error handling to make the examples as simple as possible. However, in my day job as a SAS developer, I make sure that bad input values are properly handled. One of the jobs of SAS testers is to ensure that all SAS procedures and functions gracefully handle bad inputs.

Did you miss my trick-and-treat posts from previous years? Here are links to previous posts:

- 2012: Compute the log-determinant of a matrix
- 2011: The UNIQUE-LOC trick: A real treat!
- 2010: Tricks and Treats

And if you plan on eating a lot of Halloween candy this year, remember to use SAS to analyze the frequency distribution of colors for your favorite candy!

## 3 Comments

A definite treat for SAS programmers. And thanks for sharing your previous trick-and-treat posts too... (FYI, the 2011 post isn't hyperlinked correctly)

Happy Halloween!

Thanks. Fixed.

Fortunately, this trick isn't complex at all. It's quite rational, though for me perhaps not entirely natural. On the whole, the integration of tricks into my daily routine still seems imaginary, though perhaps this trick will differentiate itself from the rest and pay real dividends in the long-term limit as I accumulate more of them at each point.

Thanks for sharing! :)