BookmarkSubscribeRSS Feed
arodriguez
Lapis Lazuli | Level 10

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.

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Doc_Duke
Rhodochrosite | Level 12

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

arodriguez
Lapis Lazuli | Level 10

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.

LinusH
Tourmaline | Level 20

Since SAS just has floating point data type, yes, to be 100% sure, you need to round it.

Data never sleeps

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

What is Bayesian Analysis?

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1586 views
  • 3 likes
  • 4 in conversation