In this tutorial, we will show how to round numbers in SAS using various round functions, along with examples.
ROUND Functions in SAS
new_variable = round(variable);
: round to the nearest integernew_variable = round(variable, 0.1);
: round to 1 decimal placenew_variable = round(variable, 0.01);
: round to 2 decimal placesnew_variable = round(variable, 10);
: round to the nearest multiple of 10new_variable = round(variable, 100);
: round to the nearest multiple of 100new_variable = ceil(variable);
: round up to the nearest integernew_variable = floor(variable);
: round down to the nearest integernew_variable = ceil(variable*10)/10;
: round up to 1 decimal placenew_variable = floor(variable*10)/10;
: round down to 1 decimal place
Let's create a sample SAS dataset for the examples in this tutorial.
data mydata; input number; format number best20.; cards; 1.23 2.367 4.1235 105.67 7000.55 80.4 ; run;
In the SAS code below, we are rounding values of a variable to the nearest integer. Here name of the variable is number
.
data output_data; set mydata; new_value = round(number); new_value2 = round(number, 1); run;
The round function without a second argument or 1 as the second argument rounds the variable to the nearest integer. Hence, both new_value and new_value2 variables store the nearest whole number to the number variable.
The following code shows how to round values of a variable to specific decimal places in SAS.
data output_data; set mydata; new_value = round(number, 0.1); /*round to 1 decimal place*/ new_value2 = round(number, 0.01); /*round to 2 decimal places*/ run;
The following code shows how to round values of a variable to the nearest multiple of 10 and 100.
data output_data; set mydata; new_value = round(number, 10); /*round to nearest multiple of 10*/ new_value2 = round(number, 100); /*round to nearest multiple of 100*/ run;
The CEIL
function is used in SAS to round up the values of a variable to the nearest integer.
data output_data; set mydata; new_value = ceil(number); /*round a number up*/ run;
The FLOOR
function is used in SAS to round down the values of a variable to the nearest integer.
data output_data; set mydata; new_value = floor(number); /*round a number down*/ run;
In SAS, the equivalent functions to the MS Excel functions ROUNDUP and ROUNDDOWN are the CEIL and FLOOR functions. We have seen these in the previous examples. Here we are showing how to include the number of digits to which you want to round up or down.
data output_data; set mydata; new_up = ceil(number); /*round up*/ new_up1 = ceil(number*10)/10; /*round up to 1 digit*/ new_down = floor(number); /*round down*/ new_down1 = floor(number*10)/10; /*round down to 1 digit*/ run;
new_up1 = ceil(number*10)/10;
creates a new variablenew_up1
which rounds thenumber
variable up to one decimal place.new_down1 = floor(number*10)/10;
creates a new variablenew_down1
which rounds thenumber
variable down to one decimal place.
If you want to round up or down to two decimal places, you can multiply the number by 100 and then divide it by 100.
Share Share Tweet