Rounding in SAS

There are two rounding functions in SAS.

The round() function in SAS will round to the nearest whole number and ‘away from zero’ or ‘rounding up’ when equidistant meaning that exactly 12.5 rounds to the integer 13.

The rounde() function in SAS will round to the nearest whole number and ‘rounding to the even number’ when equidistant, meaning that exactly 12.5 rounds to the integer 12.

Both functions allow you to specify the number of decimal places you want to round to.

For example (See references for source of the example)

    #Example code
    data XXX;
      my_number=2.2;   decimal_paces=1; output;
      my_number=2.2;   decimal_paces=2;  output;
      my_number=2.2;;  decimal_paces=3 output;
      my_number=3.99;  decimal_paces=1;  output;
      my_number=3.99;  decimal_paces=2; output;
      my_number=3.99;  decimal_paces=3; output;
      my_number=1.2345; decimal_paces=1;  output;
      my_number=1.2345; decimal_paces=2; output;
      my_number=1.2345; decimal_paces=3; output;
      my_number=7.876; decimal_paces=1;  output;
      my_number=7.876; decimal_paces=2; output;
      my_number=7.876; decimal_paces=3; output;
      my_number=13.8739; decimal_paces=1;   output;
      my_number=13.8739; decimal_paces=2;  output;
      my_number=13.8739; decimal_paces=3;  output;
    run;

    data xxx2;
      set xxx;
        round = round(my_number, decimal_places);
      rounde = rounde(my_number, decimal_places);
    run;
my_number decimal_places round rounde
2.2 0.1 2.2 2.2
2.2 0.01 2.2 2.2
2.2 0.001 2.2 2.2
3.99 0.1 4 4
3.99 0.01 3.99 3.99
3.99 0.001 3.99 3.99
1.2345 0.1 1.2 1.2
1.2345 0.01 1.23 1.23
1.2345 0.001 1.235 1.234
7.876 0.1 7.9 7.9
7.876 0.01 7.88 7.89
7.876 0.001 7.876 7.876
13.8739 0.1 13.9 13.9
13.8739 0.01 13.87 13.87
13.8739 0.001 13.874 13.874

In some rare cases, round() does not return result as expected. For example below.

data incorrect_round;
  *rounded=32768.015625, but it should be 32768.015626;
  rounded=round(32768.0156255,1e-6); output;
  *rounded=0.137, but it should be 0.138;
  rounded=round(2048.1375-2048,1e-3); output;
run;

You can find a little more by the code below. It creates dummy numbers with different numbers of decimal digits, and filter incorrect results. Note, the incorrect results are expected when the input number is near or beyond the precision level, i.e. the last decimal of the input number is near or less than the number multiplied by constant('maceps').

data dum1;
  int1=0; output;
  do i=1 to 25;
    int1=2**i; output;
  end;
  keep int1;
run;

data dum2;
  do round_digits=1 to 7;
    *x.xxx5 should be rounded up, or replace 5 to 4.99 which should be rounded down;
    dec1=2**(-round_digits)+10**(-round_digits-1)*5;
    output;
  end;
  keep dec1 round_digits;
run;

proc sql;
  create table incorrect_round2(where=(rounded<num1)) as
  select dum1.*,dum2.*,int1+dec1 as num1,round(calculated num1,10**(-round_digits)) as rounded
  from dum1, dum2;
quit;

Or more by the code below and comparing with results from another language, e.g. R.

data dum1;
  dec1=0; int1=0; output;
  do i=0 to 12;
    dec1=2**(-i);
    dec1=dec1*1.1;
    int1=2**i;
    output;
  end;
run;

proc sql;
  create table dum3 as select dec1+int1 as num1 from dum1(keep=dec1) a, dum1(keep=int1) b
  ;
  create table dat1 as select a.num1,b.num1 as num2 from dum3 a, dum3 b
;quit;

data dat2;
  set dat1;
  operator='+'; num3=num1+num2; output;
  operator='-'; num3=num1-num2; output;
  if num1^=0 and num2^=0 then do;
    operator='*'; num3=num1*num2; output;
    operator='/'; num3=num1/num2; output;
  end;
run;

data dat3;
  set dat2;
  rounded=round(num3,1e-3);
run;

As the incorrect rounding all occur on large number, round() is still reliable in most of cases.

References

How to Round Numbers in SAS - SAS Example Code