Sample 24728: Round a numeric variable to three significant digits
Use the ABS, LOG10, and INT functions to determine the appropriate power to use in the ROUND function in order
to round a numeric variable to three significant digits.
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
This code produces the desired result for many values, but it does not work for all. For example, the values 1 and 1.0000 produce a result of 1, whereas the result should be 1.00. Use it at your discretion.
data one;
input x;
if int(x) ne 0 then do;
_3sigdigit=round(x,10**(int(log10(abs(x)))2));
end;
else do;
_3sigdigit=round(x,10**(1*(abs(int(log10(abs(x))))+3)));
end;
datalines;
0.00001616
0.0051368
35.479
3149.0865
;
proc print;
format x _3sigdigit 15.8;
run;
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
Obs x _3sigdigit
1 0.00001616 0.00001620
2 0.00513680 0.00514000
3 35.47900000 35.50000000
4 3149.08650000 3150.00000000
Use the ABS, LOG10, and INT functions to determine the appropriate power to use in the ROUND function in order to round a numeric variable to three significant digits.
Type:  Sample 
Topic:  SAS Reference ==> DATA Step SAS Reference ==> Functions ==> Mathematical

Date Modified:  20051223 03:02:57 
Date Created:  20040930 14:09:08 
Operating System and Release Information
SAS System  Base SAS  All  n/a  n/a 