Numeric issue in datastep

Reply
Frequent Contributor
Posts: 131

Numeric issue in datastep

data test;

set test2;

diff = t1 - t2;

run;

 

sample data

t1               t2                                     diff

 

12               6                                       6

14191.65   14191.65                        1.818989E-12

 

The issue is whenever I encounter a large number the difference is reflected as scientific notation where as it should be a 0

I used

proc format;

picture mynum

low - high = '000000000009';

run;

 

The number displays as a 0 however when I apply a test such as

if diff = 0 then delete;  The test fails and the row still appears

any ideas

 

 

Grand Advisor
Posts: 17,464

Re: Numeric issue in datastep

Welcome to computing Smiley Happy

 

This is a known 'issue'. Please see the full explanation below. 

http://support.sas.com/documentation/cdl/en/lrcon/69852/HTML/default/viewer.htm#p0ji1unv6thm0dn1gp4t...

 

You can resolve this by formatting your data, using something like 8.1 should be enough. 

And you can change your test to:

 

if round(value, 0.0001) = 0 then <action>;

 

 

Esteemed Advisor
Posts: 7,301

Re: Numeric issue in datastep

Just responded to a similar question in a different thread. The topic you want to Google is numeric precision. It's not just with SAS, but any computer.

 

The numbers are likely to be off somewhere off to the right decimal place. The solution is to round the numbers to the number of decimal points that are practically significant to whatever you're doing.

 

Art, CEO, AnalystFinder.com

 

Super User
Posts: 1,117

Re: Numeric issue in datastep

Could you please try the below approach

 

data test;
set test2;
diff = t1 - t2;
format diff mynum.;
if compress(put(diff,best.))='0' then delete;
run; 
Thanks,
Jag
Esteemed Advisor
Posts: 6,733

Re: Numeric issue in datastep

As the others have already noted, this is an artifact resulting from the way SAS stores numeric values.

Adapt your data step (and other steps that do similar calculations):

data test;
set test2;
diff = round(t1 - t2,.00001);
run;

And you'll get rid of problems caused by precision overflows or numbers that are periodic in binary representation.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Grand Advisor
Posts: 10,251

Re: Numeric issue in datastep

BTW your example data is wrong because you let a very likely default format of best8. display the values of t1 and t2.

 

I know this because when I copy your pasted example data and use that in a data step the result is 0.

data example;
   x=14191.65;
   y=14191.65;
   result = y-x;
   put result= best32.;
run;

SAS Super FREQ
Posts: 3,420

Re: Numeric issue in datastep

In addition to what others have said, you should avoid testing floating-point values for equality. Instead, test whether the value is within a small amount of the target value:

 

if fabs(diff) < 1e-6 then delete;

Ask a Question
Discussion stats
  • 6 replies
  • 135 views
  • 0 likes
  • 7 in conversation