• 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 Nearest Integer in SAS

Round Number to Nearest Integer in SAS

April 12, 2022 Leave a Comment

To round a number to the nearest integer in SAS, you can use the SAS round() function. By default, round() rounds to the nearest integer.

data data_with_rounding;
	set data;
        round_to_integer = round(num);
run;

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

To round to the nearest integer in a SAS data step, you can use the sas round() function.

By default, the SAS round() function rounds to the nearest integer. If you wanted to change this behavior, you could pass another argument defining how many decimal places you wanted to keep.

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 integer, we can use the following SAS code.

data data_rounded_to_integer;
	set data;
        round_to_integer = round(num);
run;

This results in the following dataset.


        num   round_to_integer 
1   84.3187                84
2  19.23498                19
3   5.61295                 6
4  -0.45324                 0
5   -6.5123                -7
6 -100.2382              -100

Other SAS Rounding Methods to Round Numbers

Rounding numbers to the nearest integer can be 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;

With the SAS ceil() and floor() functions, you ensure that you are going to the integer you want, and not just letting round() determine the direction you are rounding your data.

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

Other Articles You'll Also Like:

  • 1.  SAS rename Statement – How to Rename Variables in a Data Step
  • 2.  Set Multiple Datasets in SAS Data Step
  • 3.  Using SAS to Find Mean by Group with PROC MEANS
  • 4.  Date Format ddmmmyyyy in SAS
  • 5.  SAS %eval() Function – Evaluate Expressions in SAS Macro
  • 6.  SAS include – Execute Code from Other Files in SAS with %include
  • 7.  SAS calculated – Use Columns Created from Select in PROC SQL
  • 8.  SAS intnx – Add or Subtract Time from Date Variables in SAS Data Step
  • 9.  SAS _n_ – How to Use the Automatic Variable _n_ in a Data Step
  • 10.  PROC Format – Create User-Defined Formats for Variables 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