Manage Division by Zero
A common problem when processing data is the generation of missing values due to a division by zero. SAS will report the occurrence in the log but there is no indication of the cause in the data itself.
NOTE: Division by zero detected at line 18 column 26. numerator=150 denominator=0 calc_result=. func_result=I _ERROR_=1 _N_=2
NOTE: Missing values were generated as a result of performing an operation on missing values.
Each place is given by: (Number of times) at (Line):(Column). 1 at 18:26
A report listing the data will simply show that the values are missing.
This situation can be far better managed by using the divide function to perform the calculation.
data create_data; input numerator denominator; cards; 150 1 150 0 150 . run;
data divide_function; set create_data; calc_result=numerator/denominator; func_result=divide(numerator,denominator); run;
Now when using the divide function in place of a calculation a division by zero results in the special missing value of 'I' which represents a result of infinity. A simple reporting procedure will display this value in the output.
title 'Using the data for a listing'; proc print data=divide_function; run;
Proc Print produces the following listing, note the 'I' in the output
Using the data for a listing
calc_ func_ Obs numerator denominator result result
1 150 1 150 150 2 150 0 . I 3 150 . . .
However when we use the same data to generate a calculated value it is treated as any other missing value would be, for example Proc Means.
title 'Using the data to calculate statistics'; proc means data=divide_function mean n; var calc_result func_result; run;
The Proc Means procedure ignores missing values. Below we can see that only one record has been used in the calculation and the 'I' value has been ignored along with the other missing values.
Using the data to calculate statistics
The MEANS Procedure
Variable Mean N calc_result 150.0000000 1 func_result 150.0000000 1