You can use the ROUND function to round numeric values in SAS.
Let's illustrate this with an example.
The NUMBERS data set above contains a list of random numbers.
[Note: To see the NUMBERS data set on SAS Studio, run the code in the yellow box above]
Now, let's take a look at an example of the ROUND function with a rounding factor of 0.2 (2nd parameter).
Data Numbers2; Set Numbers; Randno2 = round(Randno, 0.2); Run;
Wanna make a guess why the number is rounded the way it is?
The first value, 7.8209 is rounded to 7.8, which makes sense.
However, the third value, 2.6857 is rounded to 2.6.
In fact, the ROUND function rounds the numbers to the nearest multiple of the rounding factor.
In our example, the rounding factor is 0.2.
Randno2 = round(Randno, 0.2);
As a result, all of the rounded values are a multiple of 0.2.
Rounding Factor: 0.3
We can also try a couple different rounding factors.
Data Numbers3; Set Numbers; Randno2 = round(Randno, 0.3); Run;
What if you just want to round it to 1 or 2 decimal places?
Set the rounding factor to 0.1 or 0.01.
Data Numbers4; Set Numbers; Randno2 = round(Randno, 0.1); Run;
Rounding to 2 decimal places
Data Numbers5; Set Numbers; Randno2 = round(Randno, 0.01); Run;
Copy and run the GROCERY data set from the yellow box below.
GROCERY contains 3 variables:
Write a SAS program to calculate the updated price based on the discount offered for each item.
The calculation should be as follow:
New = Old x (1 - Discount/100)
Round the updated price to 2 decimal places.
Create any data set or variables if necessary.
First, calculate the updated price. Round the updated price to 2 decimal places using the ROUND function.
Price2 = Price * (1 - discount/100);
Price3 = Round(Price2, 0.01);