Sample SAS Training 2
ROUND Function
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).
Example
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.
Example
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.
Example
Data Numbers4;
Set Numbers;
Randno2 = round(Randno, 0.1);
Run;
Rounding to 2 decimal places
Example
Data Numbers5;
Set Numbers;
Randno2 = round(Randno, 0.01);
Run;
Exercise
Copy and run the GROCERY data set from the yellow box below.
GROCERY contains 3 variables:
- ITEM: Item no.
- PRICE: Price of the items
- DISCOUNT: Percentage of discount for each item.
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.
Need some help?
Fill out my online form.