BookmarkSubscribeRSS Feed
blueskyxyz
Lapis Lazuli | Level 10

chg=aval- base;

 

....

run;

 

why? get the value -0.2999999999? how to modify it?

捕获.JPG

3 REPLIES 3
PaigeMiller
Diamond | Level 26

SAS (and all computer software) stores numbers in a binary form, and so some decimal values cannot be represented exactly, this is not possible and not avoidable.

 

However, you can change the way the numbers are displayed, so if you assign a format to CHG, then it displays with one decimal place. For example:

 

format chg 8.1;
--
Paige Miller
blueskyxyz
Lapis Lazuli | Level 10
Miller, thanks for your answer, there are different parameters with different digitals, it is not good to use format to change all param.
Patrick
Opal | Level 21

The root cause is how computers store numbers. Details here.

 

And here some coding options for dealing with it.

data test;
  aval=51.6;
  base=51.9;
  chg=aval-base;
  chg1=round(aval-base,.0000000001);
  chg2=aval-base;
  chg3=sum(aval,-base);
  format chg2 best32.1;
run;
proc print data=test;
run;

Capture.JPG

 

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

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!

Register now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1211 views
  • 0 likes
  • 3 in conversation