BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
himself
Pyrite | Level 9

Hi, all

   Am having the following code, could someone kindly explain to me why the values of d1 and d2 are missing. Though we have the value of c=1.8.

data mult;
  a=1.2;b=1.5;
  c=a*b;
  if c="1.8" then d1=1;
   if c=1.8 then d2=1;
run;

thanks

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20

You need to read about numeric precision. Lots of resources on the web and here.

Summary: the binary system used by computers is perfect for storing powers-of-2 numbers, not so good for storing decimals.

Run this to see the horrid details:

data _null_;
  A=1.5*1.2-1.8;   
  putlog A= 32.16;
run;

A=-0.0000000000000002

View solution in original post

4 REPLIES 4
ChrisNZ
Tourmaline | Level 20

You need to read about numeric precision. Lots of resources on the web and here.

Summary: the binary system used by computers is perfect for storing powers-of-2 numbers, not so good for storing decimals.

Run this to see the horrid details:

data _null_;
  A=1.5*1.2-1.8;   
  putlog A= 32.16;
run;

A=-0.0000000000000002

himself
Pyrite | Level 9
Hi, @ChrisNZ, thanks for sharing , actually I thought SaS performs the multiplication of decimals as the normal way one can do multiplication, plus the way it stores the values, I have realized its very different
Amir
PROC Star

Hi @himself ,

 

The round() function can be useful in situations like this.

 

It is documented here and has a discussion and demonstration of the type @ChrisNZ was referring to.

 

 

Kind regards,

Amir.

gamotte
Rhodochrosite | Level 12

Hello,

 

Column c is numeric, why do you compare it to a string ?

 

To compare floating points numbers, you can do as follows :

%let precision=1.0e-06;

data mult;
  a=1.2;b=1.5;
  c=a*b;
   if abs(sum(c,-1.8))<&precision. then d2=1;
run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1997 views
  • 7 likes
  • 4 in conversation