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.
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.
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
Thanks for your answers.
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.
Since SAS just has floating point data type, yes, to be 100% sure, you need to round it.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.