DATA Step, Macro, Functions and more

Strange result using lt comparison operator

Reply
Occasional Contributor
Posts: 12

Strange result using lt comparison operator

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;

Respected Advisor
Posts: 4,641

Re: Strange result using lt comparison operator

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

PG
Super User
Super User
Posts: 6,498

Re: Strange result using lt comparison operator

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;

PROC Star
Posts: 1,558

Re: Strange result using lt comparison operator

Occasional Contributor
Posts: 12

Re: Strange result using lt comparison operator

Thanks to you all for your quick help.

Santosh

Ask a Question
Discussion stats
  • 4 replies
  • 243 views
  • 4 likes
  • 4 in conversation