How to Round Numbers in SAS

Deepanshu Bhalla Add Comment

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 integer
  • new_variable = round(variable, 0.1); : round to 1 decimal place
  • new_variable = round(variable, 0.01); : round to 2 decimal places
  • new_variable = round(variable, 10); : round to the nearest multiple of 10
  • new_variable = round(variable, 100); : round to the nearest multiple of 100
  • new_variable = ceil(variable); : round up to the nearest integer
  • new_variable = floor(variable); : round down to the nearest integer
  • new_variable = ceil(variable*10)/10; : round up to 1 decimal place
  • new_variable = floor(variable*10)/10; : round down to 1 decimal place
Sample Dataset

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;
Example 1: Round to Nearest Integer

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;
ROUND Function in SAS

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.

Example 2: Round to N Decimal Places

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;
SAS: Round to N Decimal Places
Example 3: Round to Nearest Multiple

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;
Round to Nearest Multiple
Example 4: ROUND UP in SAS

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;
SAS: ROUND UP
Example 5: ROUND DOWN in SAS

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;
ROUND DOWN in SAS
Example 6: Equivalent of ROUND UP, ROUND DOWN Excel Function in SAS

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;
ROUND UP, ROUND DOWN Excel Function in SAS
  • new_up1 = ceil(number*10)/10; creates a new variable new_up1 which rounds the number variable up to one decimal place.
  • new_down1 = floor(number*10)/10; creates a new variable new_down1 which rounds the number 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.

Related Posts
Spread the Word!
Share
About Author:
Deepanshu Bhalla

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and HR.

Post Comment 0 Response to "How to Round Numbers in SAS"
Next → ← Prev