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

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • 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 intnx – Add or Subtract Time from Date Variables in SAS Data Step
  • 2.  How to Select First 100 Observations of SAS Dataset
  • 3.  SAS rename Statement – How to Rename Variables in a Data Step
  • 4.  SAS scan Function – Return nth Word from Character String
  • 5.  SAS Comments – Commenting Your SAS Code the Right Way
  • 6.  countw SAS – Count Number of Words in a String
  • 7.  SAS rand – Generate Random Numbers in a SAS Data Step
  • 8.  SAS yymmdd10. Date Format
  • 9.  SAS Delete Dataset – Use PROC Datasets to Delete SAS Files
  • 10.  SAS calculated – Use Columns Created from Select in PROC SQL

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