• 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
  • VBA
  • 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.  Concatenating Strings in a SAS Data Step
  • 2.  SAS %eval() Function – Evaluate Expressions in SAS Macro
  • 3.  How to Combine Datasets Vertically in SAS
  • 4.  SAS Remove Formats from Dataset with PROC DATASETS
  • 5.  SAS where in – Subset Data by Multiple Values in Data Step
  • 6.  SAS intnx – Add or Subtract Time from Date Variables in SAS Data Step
  • 7.  SAS month function – Get Month from Date Variable
  • 8.  SAS Percent Format – Formatting Number as Percent in SAS Dataset
  • 9.  SAS Not In – How to Check if Variable is Not in List of Values
  • 10.  SAS Remove Labels from Dataset with PROC DATASETS

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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy