Search code examples
sasrounding

SAS Rounding by thousandths place


I have been asked to write some code in SAS that rounds a number up but only if the digit in the thousandth place is greater than one. For example, 78.858 would obviously round up to 78.86 but would also want to take 78.852 and round up to 78.86.


Solution

  • I would just do it in two operations. Use the normal ROUND() function. Then check how much it changed. And then based on that difference decide whether or not to add an extra hundredth.

    Example:

    data have;
      input x ;
    cards;
    78.858
    78.86
    78.852
    78.8515
    ;
    
    data want;
      set have;
      round=round(x,0.01);
      diff = x-round;
      if diff > 0.001 then round=round+0.01 ;
    run;
    

    Results

    OBS       x       round     diff
    
     1     78.8580    78.86    -.0020
     2     78.8600    78.86    0.0000
     3     78.8520    78.86    0.0020
     4     78.8515    78.86    0.0015