Square root transformations: How to handle negative data values?


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 ith element is xi when xi > 0, and is 0 otherwise, as shown in the following SAS/IML program:

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:

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!


About Author

Rick Wicklin

Distinguished Researcher in Computational Statistics

Rick Wicklin, PhD, is a distinguished researcher in computational statistics at SAS and is a principal developer of SAS/IML software. His areas of expertise include computational statistics, simulation, statistical graphics, and modern methods in statistical data analysis. Rick is author of the books Statistical Programming with SAS/IML Software and Simulating Data with SAS.


  1. 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! :)

Leave A Reply

Back to Top