While working with numeric data, you may need to round them. There are a few numerical functions in SAS which can help you to round numeric values. In this article, you will learn how to round numbers in SAS.
[Recomended Reading: How to truncate Decimals in SAS?]
To round numbers, you can use the ROUND function to round numeric values in SAS.
The ROUND function rounds the numbers to the nearest multiple of the rounding factor.
Data Numbers2; input numbers; datalines; 3.141592653589793238 5.6857884419 34.234 35.234 36.234 34 ; run; data ex1; set numbers2; Randno1 = round(numbers,1); Randno2 = round(numbers,2); Run;
How to Round to Nearest Whole numbers
To round to the nearest whole number, set the rounding unit to the multiple.
The following example shows how we can round up 1326 to the nearest multiple of 500.
Randno1 = round(1326,500);
The following steps are involved in the rounding process
- Work out how many times 500 goes into 1326, i.e. 1326 / 500 = 2.652
- Round this value up to the nearest integer, i.e. CEIL(2.652) = 3
- Multiply this value by the rounding unit, i.e. 3 * 500 = 1500
How to Round Decimals in SAS?
While rounding decimals, SAS finds an exact multiple of the rounding unit closest to the value to be rounded.
To truncate decimals in SAS without rounding, see our guide on How to truncate Decimals in SAS?
For example, 12.342 rounded to the nearest tenth equals 1 decimal place equals 12.3
Randno1 = round(12.342,0.1);
What if you want to round it to 2 decimal places?
you need to set the rounding factor to 0.01.
Randno1 = round(12.342,0.01);
Round UP or Down in SAS – CEIL and FLOOR Function
The Ceil() Function in SAS rounds up the number.
The CEIL and FLOOR functions round up or down, but they are limited to the nearest integer.
For example, it is impossible to round up to the nearest multiple of 10.
floor(1.95); /*Output = 1 */ ceil(1.95); /*Output = 2*/
The INT function in SAS returns only the integer portion of the argument.
data ex1; var1=1.94; x=int(var1); var2= -1.94; x2=int(var2); Run;
We hoped this article helped you to round numbers in SAS
You may also want to see our article on How to check if a string is numeric in SAS? and Convert all character variables to numeric automatically.