turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- SAS Procedures
- /
- Machine Precision errors

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-30-2015 10:19 AM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to arodriguez

01-30-2015 10:28 AM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to arodriguez

01-30-2015 10:37 AM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to arodriguez

02-02-2015 03:01 AM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to arodriguez

02-02-2015 06:50 AM

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

Data never sleeps