• 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 / SAS round – Rounding Numbers in a SAS Data Step

SAS round – Rounding Numbers in a SAS Data Step

January 18, 2022 Leave a Comment

Rounding numbers in a SAS data step can be easily done with the SAS round() function.

data data_with_rounding;
	set data;
        round_to_ten = round(num,10); /* rounds to nearest ten */
        round_to_integer = round(num); /* rounds to nearest integer */
        round_to_tenth = round(num,0.1); /* rounds to nearest tenth */
run;

If you want to round up, you can use the SAS ceil() function.

data data_with_ceiling;
	set data;
	ceiling = ceil(num);
run;

If you want to round down, you can use the SAS floor() function.

data data_with_floor;
	set data;
	floor = floor(num);
run;

When working with data, rounding numbers to the nearest integer, decimal or multiple of a number can be very useful.

In SAS, we can round numbers easily. The SAS round() function returns the nearest number depending on the precision we provide.

Rounding Numbers in SAS using SAS round()

We can round numbers in a SAS data step very easily with the SAS round() function.

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;

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 secondary 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.

If you want to round to 2 decimal places, you would pass “0.01” 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

Rounding To Multiples of a Number in SAS using SAS round()

In the example above, I was rounding to multiples of ten. You can use the SAS round() function to round to multiples of any number.

For example, you could round to multiples of 2, 4, 13, 203, 0.123, etc. Just as above, you just need to pass the number you want into the second parameter of round()

data data_with_rounding;
	set data;
        round_to_even = round(num, 2); /* rounds to nearest even number */
        round_to_even = round(num, 5); /* rounds to nearest multiple of 5 */
run;

Other SAS Rounding Methods to Round Numbers

Rounding numbers to the nearest decimal, number, or multiple is useful, but if you want to make sure that you are going in the right direction (up or down), sometimes it is better to use a function different from round().

If you want to round a number down, you can use the SAS floor() function.

data data_with_floor;
	set data;
	floor = floor(num);
run;

If you want to round a number up, you can use the SAS ceil() function.

data data_with_ceiling;
	set data;
	ceil= ceil(num);
run;

Hopefully this article has been useful for you to understand how you can round numbers to the nearest decimal, integer, or multiple with the SAS round() function.

Other Articles You'll Also Like:

  • 1.  SAS right() Function – Right Align Character Variables in Data Step
  • 2.  SAS rand – Generate Random Numbers in a SAS Data Step
  • 3.  intcx SAS – Find Time Periods Between Two Dates in SAS Data Step
  • 4.  PROC Format – Create User-Defined Formats for Variables in SAS
  • 5.  SAS ceil – Round Up to Ceiling of Number in a SAS Data Step
  • 6.  SAS contains() – Check if Variable Contains a String in Where Statement
  • 7.  SAS %eval() Function – Evaluate Expressions in SAS Macro
  • 8.  SAS Comments – Commenting Your SAS Code the Right Way
  • 9.  SAS yymmdd10. Date Format
  • 10.  SAS Lowercase – Make String Lowercase with SAS lowcase function

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