ROUND Function
Var2=ROUND(Var1, 0.1);
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:
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.
Data Grocery2;
Set Grocery;
Price2 = Price * (1 - discount/100);
Price3 = Round(Price2, 0.01);
Run;
Cookie | Duration | Description |
---|---|---|
cookielawinfo-checkbox-analytics | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics". |
cookielawinfo-checkbox-functional | 11 months | The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional". |
cookielawinfo-checkbox-necessary | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary". |
cookielawinfo-checkbox-others | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other. |
cookielawinfo-checkbox-performance | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance". |
viewed_cookie_policy | 11 months | The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data. |