Sample 26045: Truncate a decimal value without rounding
The sample code on the Full Code tab illustrates how to truncate a numeric value to a specified number of decimal places without rounding the value.
The number of desired decimal places is specified using a macro variable called Precision.
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 sample truncates X to to digits past the decimal without rounding. The CEIL function is applied to negative values while the FLOOR function is applied to positive values, and both can be conditionally applied with the IFN function.
%let precision=10**-2; /* can also be expressed as .01 */
data ds1;
input x;
y=ifn(x<0,ceil(x/&precision),floor(x/&precision)) * &precision;
datalines;
16.58849
-5.12945
-.135
100.12
;
run;
proc print data=ds1;
format x 10.5 y;
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 y
1 16.58849 16.58
2 -5.12945 -5.12
3 -0.13500 -0.13
4 100.12000 100.12
Type: | Sample |
Topic: | SAS Reference ==> DATA Step SAS Reference ==> Functions ==> Truncation
|
Date Modified: | 2012-02-21 15:05:21 |
Date Created: | 2006-03-17 10:55:12 |
Operating System and Release Information
SAS System | Base SAS | All | n/a | n/a |