Numeric issue in datastep

Regular Contributor
Posts: 178

Numeric issue in datastep

data test;

set test2;

diff = t1 - t2;



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';



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



Super User
Posts: 23,724

Re: Numeric issue in datastep

Welcome to computing Smiley Happy


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


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>;



Posts: 8,164

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,


Trusted Advisor
Posts: 1,147

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;
Super User
Posts: 10,239

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);

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
How to convert datasets to data steps
How to post code
Super User
Posts: 13,542

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;
   result = y-x;
   put result= best32.;

Posts: 4,241

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
  • 7 in conversation