BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
LRogers
Obsidian | Level 7

Hello!

I have numeric values within my data where I simply would like to just drop the digits after the decimal point so I have a whole number.

Example:

Have 460.2866667

Want 460

Have 318.9816667

Want 318

So I am using the FLOOR function which is returning the correct results.

The problem i'm running into though is when I have a whole number....this is what is happening with the FLOOR function:

Have 824

Getting 823

Technically since the number is 824.00000000 and I want to just drop the numbers after the decimal point, I would hope to get a value of 824.  

I tried FLOORZ and INT but it still gives me the 823 result.

Anyone have any ideas how to solve this?

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

ROUND() the number first.

 2  data x;
 3    a=824;
 4    b=824 - 1E-10 ;
 5    floora=floor(a);
 6    floorb=floor(b);
 7    floorb2=floor(round(b,1E-5));
 8    put (_all_) (=/) ;
 9  run;


a=824
b=824
floora=824
floorb=823
floorb2=824

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

Almost definitely, your 824 is not an integer but actually 823.999999...

 

So floor is giving the correct answer. If you change the format of your variable to something like BEST32.16, do you see 824 or do you see 823.9999999...

 

 

--
Paige Miller
LRogers
Obsidian | Level 7

yes, when I do change to that format I see 823.99999999999

FreelanceReinh
Jade | Level 19

@LRogers wrote:

yes, when I do change to that format I see 823.99999999999


Hi @LRogers

 

Please note that you were just lucky that one of the common numeric formats (such as w.d, BESTw. or Ew.) revealed the small difference (most likely a rounding error) between the stored number and 824 directly. There are several different numbers which these formats would display as if they were exactly 824, because these formats do some rounding. (Needless to say, this is no special feature of the number 824.)

 

Here are examples (assuming Windows or Unix operating systems):

data test;
input x hex16.;
cards;
4089BFFFFFFFFFFE
4089BFFFFFFFFFFF
4089C00000000000
4089C00000000001
4089C00000000002
;

Among the above five numbers (entered as their internal hexadecimal representations) only the third is equal to 824, but you would need either

  • a special format, ideally one that shows the internal representation (such as BINARY64. or HEX16.)
  • or the (non-default) system option setting DECIMALCONV=STDIEEE (in conjunction with a sufficient w.d format, e.g. 17.13)

in order to distinguish it from the other four values by just looking at a formatted value.

 

data _null_;
set test;
put x best32. +1 x 32.28 +1 x hex16.;
run;

Result (under Windows SAS 9.4 using the default DECIMALCONV=COMPATIBLE):

                             824 824.0000000000000000000000000000 4089BFFFFFFFFFFE
                             824 824.0000000000000000000000000000 4089BFFFFFFFFFFF
                             824 824.0000000000000000000000000000 4089C00000000000
                             824 824.0000000000000000000000000000 4089C00000000001
                             824 824.0000000000000000000000000000 4089C00000000002

 

Numbers like the four "deviating" cases above can easily result from calculations (as a consequence of rounding errors in floating-point arithmetic and numeric representation error):

data have;
x1=906.4/1.1;
x2=0.00824*100000;
run; /* Mathematically, both calculations result in 824, but ... */

proc print data=have noobs;
format x: hex16.;
run; /* ... not using the computer's floating-point arithmetic. */

Result (Windows SAS 9.4):

              x1                  x2

4089BFFFFFFFFFFF    4089C00000000001

 

So, it's always good to have the ROUND function at hand.

Tom
Super User Tom
Super User

ROUND() the number first.

 2  data x;
 3    a=824;
 4    b=824 - 1E-10 ;
 5    floora=floor(a);
 6    floorb=floor(b);
 7    floorb2=floor(round(b,1E-5));
 8    put (_all_) (=/) ;
 9  run;


a=824
b=824
floora=824
floorb=823
floorb2=824
LRogers
Obsidian | Level 7

This is great!  The floor(round...)) worked perfectly!  Thank you!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 732 views
  • 4 likes
  • 4 in conversation