• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / SAS / Round Number to 2 Decimal Places in SAS

Round Number to 2 Decimal Places in SAS

April 8, 2022 Leave a Comment

To round a number to 2 decimal places in SAS, you can use the SAS round() function and pass ‘0.01’ for the second argument.

data data_with_rounding;
	set data;
        round_to_two_decimals = round(num, 0.01);
run;

When working with numbers in SAS, the ability to round and keep a certain number of decimal places is valuable.

To round to 2 decimal places in a SAS data step, you can use the sas round() function.

By default, the SAS round() function rounds to the nearest integer. To round to 2 decimal places, pass ‘0.01’ for the second argument.

Let’s say we have the following code which creates a SAS dataset with some numbers.

data data;
	input num;
	datalines;
84.3187
19.23498
5.61295
-0.45324
-6.5123
-100.2382
;
run;

To round these numbers to the nearest hundredth, or to 2 decimal places, we can use the following SAS code.

data data_rounded_to_2_decimals;
	set data;
        round_to_two_decimals = round(num, 0.01);
run;

This results in the following dataset.


        num   round_to_two_decimals
1   84.3187                   84.32
2  19.23498                   19.24
3   5.61295                    5.61
4  -0.45324                   -0.45
5   -6.5123                   -6.51
6 -100.2382                 -100.24

Using SAS round() Function to Round to Other Levels of Precision

We can use the SAS round() function to round to other levels of precision. The SAS round() function returns the nearest number depending on the precision we provide.

Let’s say we have the following code which creates the same SAS dataset as above.

data data;
	input num;
	datalines;
84.3187
19.23498
5.61295
-0.45324
-6.5123
-100.2382
;
run;

Let’s round the variable “num” to various different levels so you can see how the round() function works. To round to different precisions, we pass a second parameter to round().

So, for example, if we want to round a number to the nearest tenth, we would pass “0.1” to the round() function.

Below is the SAS code which rounds a number to the nearest thousand, hundred, ten, one, tenth, hundredth and thousandth.

data data_with_rounding;
	set data;
        round_to_1000 = round(num,1000); /* rounds to nearest thousand */
        round_to_100 = round(num,100); /* rounds to nearest hundred */
        round_to_10 = round(num,10); /* rounds to nearest ten */
        round_to_1 = round(num); /* rounds to nearest integer */
        round_to_0p1 = round(num,0.1); /* rounds to nearest tenth */
        round_to_0p01 = round(num,0.01); /* rounds to nearest hundredth */
        round_to_0p001 = round(num,0.001); /* rounds to nearest thousandth */
run;

This results in the following SAS dataset:


        num round_to_1000 round_to_100 round_to_10 round_to_1 round_to_0p1 round_to_0p01 round_to_0p001
1   84.3187             0          100          80         84         84.3         84.32         84.319
2  19.23498             0            0          20         19         19.2         19.23         19.235
3   5.61295             0            0          10          6          5.6          5.61          5.613
4  -0.45324             0            0           0          0         -0.5         -0.45         -0.453
5   -6.5123             0            0         -10         -7         -6.5         -6.51         -6.512
6 -100.2382             0         -100        -100       -100       -100.2       -100.24       -100.238

Hopefully this article has been useful for you to learn how to round numbers to 2 decimal places in SAS.

Other Articles You'll Also Like:

  • 1.  SAS mean() Function – Find Average Across Columns in Data Step
  • 2.  SAS Power Function – Exponentiate Numbers with ** in a Data Step
  • 3.  Identifying Duplicates in SAS with PROC SORT dupout Option
  • 4.  SAS year function – Get Year from Date Variable
  • 5.  countw SAS – Count Number of Words in a String
  • 6.  SAS weekday function – Get Day of Week from Date Variable
  • 7.  SAS left() Function – Left Align Character Variables in Data Step
  • 8.  SAS select when – Evaluating Character Values for Conditional Processing
  • 9.  SAS calculated – Use Columns Created from Select in PROC SQL
  • 10.  Remove Specific Character from String in SAS

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy