Ora

How to Use ROUND in SAS?

Published in SAS Functions 3 mins read

The ROUND function in SAS is a powerful tool for adjusting numeric values to their closest specified unit, making your data more manageable and understandable.

To use the ROUND function in SAS, you specify a numeric value or variable that you want to round, and then provide a rounding-unit to indicate the precision required. The syntax is straightforward: ROUND(argument, rounding-unit).

How the ROUND Function Works

The ROUND function evaluates a given number and adjusts it to the nearest multiple of your chosen rounding-unit.

  • argument: This is the numeric value or variable you wish to round. It can be a direct number, a variable from your dataset, or the result of another expression.
  • rounding-unit: This parameter dictates the unit to which the argument will be rounded. It can be any positive number, such as 1 for the nearest whole number, 0.1 for the nearest tenth, 0.01 for the nearest hundredth, or even larger multiples like 5, 10, or 100.

For example, using ROUND(34.58, 0.1) tells SAS to round the number 34.58 to the nearest tenth. SAS will return the value 34.6.

Practical Examples of Using ROUND

Understanding the rounding-unit is key to effectively using the ROUND function. Here are various scenarios:

Rounding to the Nearest Whole Number

To round a number to the nearest integer, you can specify 1 as your rounding-unit. If the rounding-unit is omitted, SAS defaults to rounding to the nearest integer.

DATA round_example1;
    value_original = 15.7;
    rounded_int_1 = ROUND(value_original, 1);
    rounded_int_default = ROUND(value_original); /* Defaults to rounding to the nearest integer */
    PUT value_original= rounded_int_1= rounded_int_default=;
RUN;

Output:

value_original=15.7 rounded_int_1=16 rounded_int_default=16

Rounding to Specific Decimal Places

You can round to a specific number of decimal places by setting the rounding-unit to the desired decimal precision (e.g., 0.1 for one decimal place, 0.01 for two, etc.).

DATA round_example2;
    measurement = 34.58;
    price = 123.456;
    rounded_tenth = ROUND(measurement, 0.1);
    rounded_hundredth = ROUND(price, 0.01);
    PUT measurement= rounded_tenth= price= rounded_hundredth=;
RUN;

Output:

measurement=34.58 rounded_tenth=34.6 price=123.456 rounded_hundredth=123.46

Rounding to the Nearest Multiple

The ROUND function is also useful for rounding to the nearest multiple of any number, such as the nearest 5, 10, or 100.

DATA round_example3;
    score = 73;
    cost = 1234.56;
    rounded_score_5 = ROUND(score, 5);
    rounded_cost_100 = ROUND(cost, 100);
    PUT score= rounded_score_5= cost= rounded_cost_100=;
RUN;

Output:

score=73 rounded_score_5=75 cost=1234.56 rounded_cost_100=1200

Common Rounding Units

The table below illustrates common rounding-unit values and their effects:

Rounding Unit Description Example (ROUND(value, unit)) Result
1 Nearest whole number ROUND(15.7, 1) 16
0.1 Nearest tenth (1 decimal place) ROUND(34.58, 0.1) 34.6
0.01 Nearest hundredth (2 decimal places) ROUND(12.345, 0.01) 12.35
5 Nearest multiple of 5 ROUND(23, 5) 25
10 Nearest multiple of 10 ROUND(78, 10) 80
100 Nearest multiple of 100 ROUND(1234.56, 100) 1200

Related SAS Functions for Numeric Manipulation

While ROUND is excellent for general rounding, SAS offers other functions for specific rounding behaviors:

  • CEIL(argument): Returns the smallest integer that is greater than or equal to the argument (rounds up).
    • Example: CEIL(15.2) returns 16. CEIL(15.7) returns 16.
  • FLOOR(argument): Returns the largest integer that is less than or equal to the argument (rounds down).
    • Example: FLOOR(15.2) returns 15. FLOOR(15.7) returns 15.
  • INT(argument): Returns the integer part of the argument by truncating the decimal portion (rounds towards zero).
    • Example: INT(15.7) returns 15. INT(-15.7) returns -15.

Further Resources

For more detailed information and advanced usage of the ROUND function and other numeric functions in SAS, refer to the official SAS Documentation for the ROUND Function.