BookmarkSubscribeRSS Feed
Q1983
Lapis Lazuli | Level 10

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

 

 

6 REPLIES 6
Reeza
Super User

Welcome to computing 🙂

 

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

 

 

art297
Opal | Level 21

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

 

Jagadishkatam
Amethyst | Level 16

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
Kurt_Bremser
Super User

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.

ballardw
Super User

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;

Rick_SAS
SAS Super FREQ

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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 560 views
  • 0 likes
  • 7 in conversation