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
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.