When working with numerical data in SAS, you may need to round values. Fortunately, SAS provides several functions for rounding. This article explains how to round numbers in SAS.
To round numbers, you can use the ROUND function to round numeric values in SAS.
“Syntax”:
ROUND(argument <, rounding-unit>)
An argument is a number that is to be rounded. The rounding unitis an optional argument and is a positive number that specifies the rounding unit.
The ROUND function rounds the numbers to the nearest multiple of the rounding factor.
Data Numbers2;input numbers;datalines;3.1415926535897932385.685788441934.23435.23436.23434;run;data ex1;set numbers2;Randno1 = round(numbers,1);Randno2 = round(numbers,2);Run;
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
$1
$1
$1
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);
The floor() Function in SAS takes up the column name as an argument and rounds down the column.
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 */<br/>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.