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
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
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
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;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.