BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
AIO
Calcite | Level 5 AIO
Calcite | Level 5

For the numeric variables, the default format in SAS is best12. When a variable is derived VAR1-VAR2, format best12. and best32. will show different value.

Sample code:

data test;
x=9.87;
y=7.53;
z1=x-y;
z2=x-y;
if z1=z2 then flag="Y";
format z1 best12.;
format z2 best32.;
run;

------------------------------------------

Variable z1 and z2 should be same, why it display differnt in SAS? Which is the true value?

AIO_1-1706147484221.png

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Two factors are combining to produce these results.

 

First, SAS performs arithmetic in a binary system.  It turns out there is no way to store some numbers exactly in a binary system, just like there is no way to store 1/3 exactly in a base 10 system.  Sometimes the arithmetic generates numbers that are very nearly accurate, but could be off in the 15th decimal place (approximately).  In this case, that is what you are seeing when you see all that detail in the printing of z2.

 

Secondly, applying a format will round the numeric values to fit into the width of the format.  z1 and z2 are not different.  But by printing in a format they appear to be different.  When rounded to 12 positions (as limited by the best12 format), they both would round to 2.34.  You can demonstrate this rounding yourself without the math involved (which adds a second factor to complicate the issue).  For example,  you could code:

 

z1 = 2.3999999999999;

z2=z1;

 

Then continue as you are doing now:  print one variable in the best12 format and one in the best32 format.

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

That is the whole purpose of a FORMAT.  You use a different format so that the values will SHOW differently.

 

For example if you set a date variable to the first of January of 2024 you can use the DATE9. format to have it display as 01JAN2024 and the YYMMDD10. format to have it display as 2024-01-01, but the actual value stored is still just the number 23,376.

 

1    data _null_;
2      x='01jan2024'd;
3      put x= date9. x= yymmdd10. x= comma. ;
4    run;

x=01JAN2024 x=2024-01-01 x=23,376

If you want to see the TRUE value stored in the variable use the HEX16. format as that will show the actual bits used by the 64 but floating point format that the value is stored as.

Astounding
PROC Star

Two factors are combining to produce these results.

 

First, SAS performs arithmetic in a binary system.  It turns out there is no way to store some numbers exactly in a binary system, just like there is no way to store 1/3 exactly in a base 10 system.  Sometimes the arithmetic generates numbers that are very nearly accurate, but could be off in the 15th decimal place (approximately).  In this case, that is what you are seeing when you see all that detail in the printing of z2.

 

Secondly, applying a format will round the numeric values to fit into the width of the format.  z1 and z2 are not different.  But by printing in a format they appear to be different.  When rounded to 12 positions (as limited by the best12 format), they both would round to 2.34.  You can demonstrate this rounding yourself without the math involved (which adds a second factor to complicate the issue).  For example,  you could code:

 

z1 = 2.3999999999999;

z2=z1;

 

Then continue as you are doing now:  print one variable in the best12 format and one in the best32 format.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 1150 views
  • 0 likes
  • 3 in conversation