BookmarkSubscribeRSS Feed
karenyang
Calcite | Level 5
data three;
x=36; y=3.6;
z=(y-x)/x*100;
if z<=-90 then a=1; 
                else a=0;
 
if a=-90 then flag=1;
run;
proc print;
run;
 
I got the following result:
Obs x y z a flag1
363.6-900 

I expect a=1, flag=1. But I got zero, null value instead. Why?

5 REPLIES 5
karenyang
Calcite | Level 5

How  can I make SAS think it is -90?

sbxkoenk
SAS Super FREQ

It has probably something to do with numeric values in the SAS System being represented as IEEE double-precision floating-point numbers.

SAS Help Center: Numeric Precision

 

data _NULL_;
file print;
x=36; y=3.6;
z=(y-x)/x*100; put z= 25.15;
if z=-90        then do; put '1. hurray ; z equals -90'; end;
if int(z)=-90   then do; put '2. hurray ; z equals -90'; end;
if floor(z)=-90 then do; put '3. hurray ; z equals -90'; end;
if ceil(z)=-90  then do; put '4. hurray ; z equals -90'; end;
if round(z)=-90 then do; put '5. hurray ; z equals -90'; end;
run;
/* end of program */

Ciao,

Koen

karenyang
Calcite | Level 5
Thank you!
OlafKratzsch
SAS Employee
It's a datatype thing.

Try:

if int(z)<=-90 then a=1;

And your flag is always missing as you check for a being <=90. A can only be 1 or 0
PaigeMiller
Diamond | Level 26

@sbxkoenk is correct, this is a numerical precision issue. Here is more reading: Solved: comparing two values - SAS Support Communities and even more at https://communities.sas.com/t5/SAS-Programming/Formula-evaluates-totals-the-same-set-of-variables-di...

 

Your value of Z is not -90 because SAS cannot represent some decimals exactly. Decimal values that are not an integer power of 2 cannot be represented exactly. So y=3.6 produces a number that isn't quite 3.6 (can't represent it exactly) and so the value of Z (which uses Y) isn't exactly -90. 

 

If you display the numbers using the HEX format so we can see the exact value, like this

 

data three;
    x=36; y=3.6;
    z=(y-x)/x*100;
    minusninety=-90;
    if z<=-90 then a=1; else a=0;
    if a=-90 then flag=1;
run;

proc print data=three;
    var x y z minusninety;
    format x y z minusninety hex16.;
run;

 

this is the result, note that variable Z is not exactly equal to the value of variable MINUSNINETY. So then variable A=0 because it is not exactly equal. 

 

Obs                   x                   y                   z         minusninety

 1     4042000000000000    400CCCCCCCCCCCCD    C0567FFFFFFFFFFF    C056800000000000

 

Then flag is missing because it only is assigned a value when A=1, but A=0 so no value is assigned.

 

I think the best solution is to use the ROUND function like this: if round(z)=-90 as suggest by @Kurt_Bremser here is a better solution which will work in more cases than the INT or FLOOR or CEIL function. In this case INT works but there may be other cases where you are not comparing to an integer, and so INT won't work in those cases but ROUND will.

--
Paige Miller

G2 Grid Leader Spring 2025.png

 

 

 

 

Want to review SAS Customer Intelligence 360? Gartner and G2 are offering a gift card or charitable donation for each accepted review. Use this link for G2 to opt out of receiving anything of value for your review.

 

Gartner.jpg

 

SAS Customer Intelligence 360

Get started with CI 360

Review CI 360 Release Notes

Open a Technical Support case

Suggest software enhancements

Assess your marketing efforts with a free tool

 

Training Resources

SAS Customer Intelligence Learning Subscription (login required)

Access free tutorials

Refer to documentation

Latest hot fixes

Compatibility notice re: SAS 9.4M8 (TS1M8) or later

How to improve email deliverability

SAS' Peter Ansbacher shows you how to use the dashboard in SAS Customer Intelligence 360 for better results.

Find more tutorials on the SAS Users YouTube channel.

G2 Grid Leader Spring 2025.png

 

 

 

 

Want to review SAS Customer Intelligence 360? Gartner and G2 are offering a gift card or charitable donation for each accepted review. Use this link for G2 to opt out of receiving anything of value for your review.

 

Gartner.jpg

 

SAS Customer Intelligence 360

Get started with CI 360

Review CI 360 Release Notes

Open a Technical Support case

Suggest software enhancements

Assess your marketing efforts with a free tool

 

Training Resources

SAS Customer Intelligence Learning Subscription (login required)

Access free tutorials

Refer to documentation

Latest hot fixes

Compatibility notice re: SAS 9.4M8 (TS1M8) or later

Discussion stats
  • 5 replies
  • 206 views
  • 3 likes
  • 4 in conversation