Hi There,
If I run the below data step I see SAS behaving in strange manner. Variable y resolves to 1.2. SAS thinks x(1.2) is less than y. Any help will be appreciated.
Thanks,
Santosh
data aa;
x=1.2;
y=(1.1+1*0.1);
if x<y then Confused="Yes";
else Confused="No";
run;
SAS numbers are represented internally by floating point values. 1.2 doesn't have an exact representation as a floating point number, so its internal value is an approximation. That approximation will take different values depending on how it was arrived at. Add the following statements and see what happens
if round(x, 0.1) < round(y, 0.1) then RoundConfused="Yes";
else RoundConfused="No";
PG
Use the HEX16. format to look at how these two different numbers end up getting stored into the 8 byte IEEE floating point format.
data aa;
x=1.2;
y=(1.1+1*0.1);
put (x y) (= hex16. /) ;
x=round(x,0.1);
y=round(y,0.1);
put / (x y) (= hex16. /) ;
run;
x=3FF3333333333333
y=3FF3333333333334
x=3FF3333333333333
y=3FF3333333333333
run;
Much reading available on this topic.
For example:
Numerical Accuracy in SAS Software: SAS(R) 9.4 Language Reference: Concepts, Fifth Edition
Thanks to you all for your quick help.
Santosh
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.
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.