N/A
Posts: 1

how to deal very small or large numbers when doing computation using SAS?

Hi,

I'm doing some computations using SAS. In my formula, I have to divide a variable which I know it is not ZERO. But because it is very very small, SAS recognizes it as a ZERO, and gives me errors when processing the calculation.

What can I do to make SAS to increase the degree of accuracy when dealing with numbers? Thanks!

Grace.

Posts: 5,391

Re: how to deal very small or large numbers when doing computation using SAS?

As far as I know, SAS does all it's computations using double precision floating point numbers and never goes beyond that precision. Within those limits, it tries to avoid rounding error and numerical instability. The most common approach when dealing with very large or very small numbers involves rescaling your problem.

For example, in the following datastep, an error occurs only when z is calculated :

data _null_;

x = 1e-200;

y = 1e200;

t = 1 / x;

z = y / x;

run;

PG

PG
Super User
Posts: 3,761

Re: how to deal very small or large numbers when doing computation using SAS?

Can you post an example of the calculation causing the problem? Dividing by a very small number may result in a very large number that could be meaningless in the context of how you want to use it.

SAS Employee
Posts: 37

Re: how to deal very small or large numbers when doing computation using SAS?

Another thing you might try is changing the order of your computations. It may be that your very small numbers are intermediary results.

Also, it never hurts to go back to the assumptions of the model. Is it really meaningful in the real world problem you are trying to solve to divide by such a small number? Maybe there is a special condition in practice when measurements are very close to zero?

PROC Star
Posts: 1,258

Re: how to deal very small or large numbers when doing computation using SAS?

Here's another example. If you run this code, it quits after just over 1000 iterations, even though theoretically smallnum should never become zero. It's quite possible that the result that is causing you problems is actually zero.

All of the preceding advice is correct; you should consider ways to restructure your code.

Tom

data nums;
smallnum = 1;
stopflag = 0;

do i = 1 to 1000000 while(stopflag = 0);
smallnum = smallnum / 2;
testnum = 1 / smallnum;
output;

if smallnum = 0 then
stopflag = 1;
end;
run;

Discussion stats
• 4 replies
• 773 views
• 0 likes
• 5 in conversation