Skip to main content

Rounding numbers

SAS ROUND() and w.d formats, written in R with sas_round() and sas_format().

Numbers
DATA step
Why round() in R gives different answers than ROUND() in SAS, and how to get the SAS results in R.

SAS ROUND(x, unit), w.d formats

Agreement with SAS 99.95% of ROUND() results, 99.40% of 32.d results

R’s round() and SAS’s ROUND() often give different answers. This is not a bug in either language. They follow different rules, and both work with numbers that the computer cannot store exactly. If you move a SAS program to R and use round(), some values in your tables can change in the last decimal place.

segue has two functions that give the SAS results in R:

Round to a whole number

SASDATA step
data halves;
  input x;
  whole = round(x);
datalines;
0.5
1.5
2.5
-2.5
;
run;
Rsegue
library(segue)

x <- c(0.5, 1.5, 2.5, -2.5)
sas_round(x)
[1]  1  2  3 -3

SAS rounds a value that is exactly halfway to the number farther from zero, so 2.5 becomes 3. R’s round() follows the IEC 60559 standard, which rounds halves to the even number, so 2.5 becomes 2:

x SAS ROUND(x) sas_round(x) round(x)
0.5 1 1 0
1.5 2 2 2
2.5 3 3 2
-2.5 -3 -3 -2

Round to two decimal places

SASDATA step
data cents;
  input x;
  cents = round(x, 0.01);
datalines;
1.005
2.675
99.995
;
run;
Rsegue
x <- c(1.005, 2.675, 99.995)
sas_round(x, digits = 2)
[1]   1.01   2.68 100.00

Here the two languages differ for a second reason. The number 2.675 cannot be stored exactly as a binary double. The computer keeps the closest value it can, which is slightly less than 2.675:

sprintf("%.20f", 2.675)
[1] "2.67499999999999982236"

R’s round(2.675, 2) rounds that stored value, so the answer is 2.67. SAS treats values that are very close to halfway as halfway. The SAS documentation calls this “fuzzing”. So ROUND(2.675, 0.01) is 2.68, which is the answer you get when you round by hand.

x SAS ROUND(x, 0.01) sas_round(x, 2) round(x, 2)
1.005 1.01 1.01 1.00
2.675 2.68 2.68 2.67
99.995 100.00 100.00 100.00

sas_round() takes either digits, as round() does, or unit, as ROUND() does. Use unit for units that are not powers of ten:

SASDATA step
quarter = round(x, 0.25);
Rsegue
sas_round(2.675, unit = 0.25)
[1] 2.75

How close are the results?

The comparison ran SAS’s ROUND() on about 23,000 test values with 59 rounding units, then ran each R option on the same doubles. The values were read into R bit for bit from the SAS data sets, so any difference comes from the rounding and not from the inputs. The test values include every value with one decimal place in [-100, 100], every value with three decimal places in [-3, 3], and values a few units in the last place away from halfway points.

Figure 1: Share of SAS ROUND(x, unit) results that each R option reproduces exactly.

sas_round() gives the same double as SAS for every value with up to three decimal places. The few differences are values of 1e15 or more, or values divided by a tiny unit such as 1e-15. Those numbers have more than 15 significant digits, and SAS is not exact there either: ROUND(1000000000000000.5, 0.5) is 1000000000000001, although that value is already a multiple of 0.5.

For formats, sas_format() matches SAS’s 32.d output for 99.40% of 100,516 results. sprintf() matches 96.98%. The differences are values within a few units in the last place of a halfway point, and values shown with 15 or more digits, where SAS’s own digits are not consistent.

Other SAS rounding functions

segue does not have functions for these yet. The closest R functions are:

SAS R Notes
ROUNDE(x, unit) none Rounds halves to even, like round(), but with SAS’s fuzzing.
ROUNDZ(x, unit) none No fuzzing. ROUNDZ(2.675, 0.01) is still 2.68 because SAS divides by the unit first.
INT(x) trunc(x) SAS treats values within 1e-12 of an integer as that integer.
FLOOR(x), CEIL(x) floor(x), ceiling(x) Same fuzzing as INT(). FLOOR(1 - 1e-12) is 1.
INTZ, FLOORZ, CEILZ trunc(), floor(), ceiling() No fuzzing. Identical except for subnormal numbers, which SAS treats as 0.
TipTyping numbers in R tests

R’s parser can turn some decimal text into a slightly different double than SAS does. For example, 1e300 in R is a few units in the last place away from SAS’s 1e300. If you compare R and SAS results in a test, read the SAS values from a .sas7bdat file with haven::read_sas() instead of typing them.