## Machine Precision errors

Frequent Contributor
Posts: 146

# Machine Precision errors

Hi everyone,

Today I was testing some calculations, I want to add a flag variable to indicate when a p-value is <=0.05 or <=0.01 and i found a machine precision error. An example to duplicate the error could be

data test;

set sashelp.class;

if _N_<5 then new_var=age;

else if _N_<10 then new_var=0.1-_N_/100;

else if _N_<=15 then new_var=0.05-_N_/300;

else new_var=0.001-_N_/100000;

if new_var>0 then do;

if new_var <= 0.001 then flag = '***';

else if new_var <= 0.01  then flag = '**';

else if new_var <= 0.05  then flag = '*';

end;

run;

There are two lines with value "equal" to 0.01 but if I make a IF condition keeping rows with value equal to 0.01 and I don't get nothing.

I'm wondering if there are any usual procedure to avoid this or a good practice to avoid this kind of problems, rather than round to each operation.

Super User
Posts: 9,599

## Re: Machine Precision errors

Hi,

What I presume you are hitting here, and I am just leaving so don't have SAS open, is where a number looks on screen like it is correct, however there is a really, really tiny change some 10 decimal places down.  So you may see in your table: 0.0001345, but the data stored is 0.000134500000012.

My solution is to use a rounding function to ensure the value is actually as you expect it.  So in your example new_var=round(0.05 - _N_ / 300,0.01).  This will ensure the tiny fraction doesn't hang on to annoy your calculations further on.

Posts: 2,125

## Re: Machine Precision errors

Not an error, a feature.  This is a feature of IEEE floating point operations and the fact that some number in base10 cannot be represented identically in base2.

SAS stores all numbers a floating point.  base10 and base2 can represent base10 integers identically, but not all base10 decimal numbers.  This reference will tell you more than you want to know about how SAS handles precision

SAS(R) 9.4 Language Reference: Concepts, Fourth Edition

Frequent Contributor
Posts: 146

## Re: Machine Precision errors

I know that the problem is produced by IEEE floating point, my question is if exist any way to avoid it without putting in each operation a ROUND function.

Super User
Posts: 5,884