How to Round Numbers in SAS (4 Examples)


You can use the following methods to round numbers in SAS:

Method 1: Round to Nearest Integer

data new_data;
    set original_data;
    new_value = round(value);
run;

Method 2: Round to Specific Decimal Places

data new_data;
    set original_data;
    new_value1 = round(value, .1); /*round to 1 decimal place*/
    new_value2 = round(value, .01); /*round to 2 decimal places*/
    new_value3 = round(value, .001); /*round to 3 decimal places*/
run;

Method 3: Round All Values Down (Or Up) to Next Integer

data new_data;
    set original_data;
    new_value1 = floor(value); /*round down to next integer*/
    new_value2 = ceil(value); /*round up to next integer*/
run;

Method 4: Round to Nearest Multiple

data new_data;
    set original_data;
    new_value1 = round(value, 10); /*round to nearest multiple of 10*/
    new_value2 = round(value, 100); /*round to nearest multiple of 100*/
run;

The following examples show how to use each method with the following dataset in SAS:

/*create dataset*/
data original_data;
    input value;
    datalines;
0.33
0.9
1.2593
1.61
2.89
4.3
8.8
14.4286
18.2
51.4
;
run;

/*view dataset*/
proc print data=original_data;

Example 1: Round to Nearest Integer

The following code shows how to round each value to the nearest integer:

/*round to nearest integer*/
data new_data;
    set original_data;
    new_value = round(value);
run;

/*view new dataset*/
proc print data=new_data;

Example 2: Round to Specific Decimal Places

The following code shows how to round the values to a specific number of decimal places:

data new_data;
    set original_data;
    new_value1 = round(value, .1); /*round to 1 decimal place*/
    new_value2 = round(value, .01); /*round to 2 decimal places*/
    new_value3 = round(value, .001); /*round to 3 decimal places*/
run;

/*view new dataset*/
proc print data=new_data;

Example 3: Round All Values Down (Or Up) to Next Integer

The following code shows how to round all values down (or up) to the next integer using the floor() and ceil() functions:

data new_data;
    set original_data;
    new_value1 = floor(value); /*round down to next integer*/
    new_value2 = ceil(value); /*round up to next integer*/
run;

/*view new dataset*/
proc print data=new_data;

Method 4: Round to Nearest Multiple

The following code shows how to round all values to the nearest multiple of some value:

data new_data;
    set original_data;
    nearest10 = round(value, 10); /*round to nearest multiple of 10*/
    nearest100 = round(value, 100); /*round to nearest multiple of 100*/
run;

/*view new dataset*/
proc print data=new_data;

Additional Resources

The following tutorials explain how to perform other common tasks in SAS:

How to Normalize Data in SAS
How to Replace Missing Values with Zero in SAS
How to Remove Duplicates in SAS

Leave a Reply

Your email address will not be published.