Rounding numbers in a SAS data step can be easily done with the SAS round() function.

data data_with_rounding;
	set data;
        round_to_ten = round(num,10); /* rounds to nearest ten */
        round_to_integer = round(num); /* rounds to nearest integer */
        round_to_tenth = round(num,0.1); /* rounds to nearest tenth */
run;

If you want to round up, you can use the SAS ceil() function.

data data_with_ceiling;
	set data;
	ceiling = ceil(num);
run;

If you want to round down, you can use the SAS floor() function.

data data_with_floor;
	set data;
	floor = floor(num);
run;

When working with data, rounding numbers to the nearest integer, decimal or multiple of a number can be very useful.

In SAS, we can round numbers easily. The SAS round() function returns the nearest number depending on the precision we provide.

Rounding Numbers in SAS using SAS round()

We can round numbers in a SAS data step very easily with the SAS round() function.

Let’s say we have the following code which creates a SAS dataset with some numbers.

data data;
	input num;
	datalines;
84.3187
19.23498
5.61295
-0.45324
-6.5123
-100.2382
;
run;

Let’s round the variable “num” to various different levels so you can see how the round() function works. To round to different precisions, we pass a secondary parameter to round().

So, for example, if we want to round a number to the nearest tenth, we would pass “0.1” to the round() function.

If you want to round to 2 decimal places, you would pass “0.01” to the round() function.

Below is the SAS code which rounds a number to the nearest thousand, hundred, ten, one, tenth, hundredth and thousandth.

data data_with_rounding;
	set data;
        round_to_1000 = round(num,1000); /* rounds to nearest thousand */
        round_to_100 = round(num,100); /* rounds to nearest hundred */
        round_to_10 = round(num,10); /* rounds to nearest ten */
        round_to_1 = round(num); /* rounds to nearest integer */
        round_to_0p1 = round(num,0.1); /* rounds to nearest tenth */
        round_to_0p01 = round(num,0.01); /* rounds to nearest hundredth */
        round_to_0p001 = round(num,0.001); /* rounds to nearest thousandth */
run;

This results in the following SAS dataset:


        num round_to_1000 round_to_100 round_to_10 round_to_1 round_to_0p1 round_to_0p01 round_to_0p001
1   84.3187             0          100          80         84         84.3         84.32         84.319
2  19.23498             0            0          20         19         19.2         19.23         19.235
3   5.61295             0            0          10          6          5.6          5.61          5.613
4  -0.45324             0            0           0          0         -0.5         -0.45         -0.453
5   -6.5123             0            0         -10         -7         -6.5         -6.51         -6.512
6 -100.2382             0         -100        -100       -100       -100.2       -100.24       -100.238

Rounding To Multiples of a Number in SAS using SAS round()

In the example above, I was rounding to multiples of ten. You can use the SAS round() function to round to multiples of any number.

For example, you could round to multiples of 2, 4, 13, 203, 0.123, etc. Just as above, you just need to pass the number you want into the second parameter of round()

data data_with_rounding;
	set data;
        round_to_even = round(num, 2); /* rounds to nearest even number */
        round_to_even = round(num, 5); /* rounds to nearest multiple of 5 */
run;

Other SAS Rounding Methods to Round Numbers

Rounding numbers to the nearest decimal, number, or multiple is useful, but if you want to make sure that you are going in the right direction (up or down), sometimes it is better to use a function different from round().

If you want to round a number down, you can use the SAS floor() function.

data data_with_floor;
	set data;
	floor = floor(num);
run;

If you want to round a number up, you can use the SAS ceil() function.

data data_with_ceiling;
	set data;
	ceil= ceil(num);
run;

Hopefully this article has been useful for you to understand how you can round numbers to the nearest decimal, integer, or multiple with the SAS round() function.

Categorized in:

SAS,

Last Update: March 20, 2024