To round a number to 2 decimal places in SAS, you can use the SAS round() function and pass ‘0.01’ for the second argument.

data data_with_rounding;
	set data;
        round_to_two_decimals = round(num, 0.01);
run;

When working with numbers in SAS, the ability to round and keep a certain number of decimal places is valuable.

To round to 2 decimal places in a SAS data step, you can use the sas round() function.

By default, the SAS round() function rounds to the nearest integer. To round to 2 decimal places, pass ‘0.01’ for the second argument.

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;

To round these numbers to the nearest hundredth, or to 2 decimal places, we can use the following SAS code.

data data_rounded_to_2_decimals;
	set data;
        round_to_two_decimals = round(num, 0.01);
run;

This results in the following dataset.


        num   round_to_two_decimals
1   84.3187                   84.32
2  19.23498                   19.24
3   5.61295                    5.61
4  -0.45324                   -0.45
5   -6.5123                   -6.51
6 -100.2382                 -100.24

Using SAS round() Function to Round to Other Levels of Precision

We can use the SAS round() function to round to other levels of precision. The SAS round() function returns the nearest number depending on the precision we provide.

Let’s say we have the following code which creates the same SAS dataset as above.

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 second 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.

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

Hopefully this article has been useful for you to learn how to round numbers to 2 decimal places in SAS.

Categorized in:

SAS,

Last Update: February 26, 2024