# SAS Round? Breakdown of SAS Round Function

## sas round, sas round function, sas round down, sas roundup, sas roundup function, sas ceil, sas ceilz, sas floor,  floor in sas, sas fuzz, sas int

```You might be interested in this course:
Complete & Practical SAS```

### Introduction

SAS Round or SAS round function helps us decrease numerical errors in precision. This is a function that lets you round to the nearest integer, or round up or down. One of the uses of this function is to prevent the accumulation of imprecise values or avoid computational errors. We will also briefly discuss SAS Floor and SAS ceil. A full course on Base SAS is available here: SAS Online Tutor for Base Programming: Learn SAS

#### Avoiding Computational Errors With SAS Round

Before we start discussing the specifics, let’s look at the syntax carefully.

ROUND (argument <,rounding-unit>)

In SAS programming, the function, in this case ROUND, is followed by parentheses that enclose the arguments or parameters of the function. Each argument or parameter is separated by a comma. For the Round function specifically, the second argument is optional. The reason that the second argument (rounding unit) is optional is because there is a default in place. The default is to round up or down to the nearest unit. For example, 40.397 would be rounded down to 40. In other words, it rounds to the nearest integer. If you require a different level of precision you need to include that second argument. If you include that second argument, it will round the first argument to the nearest multiple of the second argument. Let me show you a specific example when you include a second argument.

data myexample;
value1=0.6;
value2=3*0.2;
differencev=value1-value2;
if round(value1, 0.1)=round(value2, 0.1)
then equal=’Y’;
else equal=’N’;
run;

First of all, before we look at the example more deeply, let’s look at the code in case you are new to SAS programming. value1 and value2 are variables, and these variables equal whatever is after the equal sign. differencev is a new variable that will store the difference/subtraction between value1 and value2 variables. Then we have an if then statement that says if value1 is the same as value2 (based on rounding to the nearest tenth, 0.1) then return a ‘Y’. If they are not equal then return ‘N’ or No. You will notice that if you run this code, you will get a Y in the ouput, and this is because the round function has returned the correct mathematical result.

So, looking back at our example, you can see that we asked for the nearest tenth in terms of rounding. What would the code be if we are interested in the nearest hundredth?

round(value1, 0.01)

What if you wanted to round down from 297 to 200? To round DOWN, you’ll need to subtract off half of the rounding unit. Half of the rounding unit (0.01) is 0.005.

round(value1-0.005, 0.01)

##### Round Function And Comparison

Especially relevant, whenever you are dealing with non-integers (ie decimals), you have to be really careful. The technical answer as to why SAS has issues computing non-integers is unimportant but being aware that it does is important.

To be precise, if you were to compare 0.8 to (4.0 – 3.2), it would not see these values as being equal. This is because the values involved in the computation cannot be precisely represented in binary and hexadecimal.

Hence, one way to prevent these comparison errors is by rounding the values before doing any comparison. Another strategy is to always use integers or whole numbers when possible. Understanding your data also helps as you can figure out the level of significance you might be dealing with going in.

###### Utility of SAS Floor and SAS Ceil functions

Briefly, I want to mention the sas floor and sas ceil functions. It will be easier to understand their role now that you understood the explanation above about sas round function.

The floor function returns the largest integer that is less or equal to the argument. In the case below, it will return 2.

value1=2.1;

a=floor(value1);

put a;

The ceil function returns the smallest integer that is greater than or equal to the argument. In the case below, it will return 3.

value1=2.1

a=ceil(value1)

put a;

In conclusion, let’s finish with an example of how you would utilize a lot of what you have learned here.

Let’s assume for a second that you want to round all of the values of a variable UP to the nearest hundred, as an example 142 to 200. First, consider, would we use floor or ceil? Since ceil returns the smallest integer that is GREATER than or equal to the argument, we want to use ceil in this case.

If you want to apply ceil or floor functions first multiply by the rounding factor, apply floor/ceil, and then divide by the rounding factor.

Our rounding factor? 100 (round to the nearest hundred)

Do we want to apply floor or ceil? Ceil makes sense for us

Then simply divide

R= RF*ceil(x/RF)

This concludes this lesson. If you have any questions, feel free to ask in the comments.

Written by Ermin

You might be interested in these articles: