Skip to content
9to5sas
  • Index
  • Glossary
Facebook Twitter Instagram Email RSS

  • Start Here
  • Base SAS
  • Advanced SASExpand
    • SAS Macros
    • PROC SQL
  • SAS/STATSExpand
    • SAS Analytics
    • Statistics
  • SAS Programs
9to5sas

9to5sas » SAS PROGRAMS » How to truncate Decimals in SAS?

How to truncate Decimals in SAS?

BySubhro Posted onJanuary 6, 2022November 7, 2022 Last Updated:November 7, 2022
0 Comments

Sometimes you may need to truncate numeric values to a specified number of decimal places without rounding. In this article, you will learn to truncate decimals in SAS, without rounding.

truncate Decimals in SAS

In this blog post, we will discuss how to truncate SAS decimals. Truncating decimals reduces the precision of a number by removing the digits after the decimal point.

This can be useful to simplify a complex number or make the calculation process faster.
There are two main ways to truncate SAS decimals: the TRUNC function or the INT function. Both methods will be discussed in this blog post. We will also provide examples of when and how to use each method.

Page Contents

  • Truncating vs Rounding decimal numbers- The Difference
  • What is rounding to two decimal places?
  • Truncating decimal numbers in SAS without rounding
  • Truncate Decimals in SAS using the INT Function

Truncating vs Rounding decimal numbers- The Difference

The first thing to notice is a pseudo-relation between rounding and truncating.

In simplest terms, truncation means to slice off the decimal portion of a number. This means:

  • Truncating 3.3 returns 3
  • Truncating 3.8 returns 3

What is rounding to two decimal places?

It is a process that returns the closest finite number expressed to two decimal places. This means:

  • Rounding 3.465 to two decimal places returns 3.47
  • Rounding 3.464 to two decimal places returns 3.46

[Recomended Reading: How to Round Numbers in SAS?]

Truncating decimal numbers in SAS without rounding

Sometimes it is necessary to truncate displayed numeric values to a specified number of decimal places without rounding.

For example, if we need to truncate 24536.8746 to 3 decimal places without rounding, the displayed number should be 24536.874 and not 24536.875. This is as compared to the rounded number 24536.875).

For more information on rounding numbers in SAS, see our guide on How to Round Numbers in SAS?

If you think you can truncate numeric values by applying SAS w.d format, think again.

Try running this SAS code:

data test;
    x=24536.8746;
    y=x;
    format y 9.3;
run;
How to truncate Decimals in SAS?
Truncate Decimals in SAS

The TRUNC function might come to your mind, and if you look at the SAS TRUNC function documentation, you will find that it does truncate numeric values. Rather it truncates to a specified number of bytes, which is not the same for numerics.

The TRUNC function truncates a numeric value to a smaller number of bytes, as specified in length and pads the truncated bytes with 0s.

Truncate Decimals in SAS using the INT Function

So, to truncate decimals in SAS, we have a workaround to drop decimal places without rounding.

The INT function returns the integer portion of the argument (truncates the decimal portion). If the argument’s value is within 10**(-12) of an integer, the function results in that integer. If the argument’s value is positive, INT(argument) has the same result as FLOOR(argument).

Let’s take the number 24536.8746 as an example, keeping only 3 digits after the decimal.

We can do it in 3 steps:

  1. Multiply the number by 103, effectively making the decimals part of a whole number.
  2. Then, apply the INT() function to truncate the decimal portion, keeping only the whole portion from the previous step.
  3. Divide the result of step 2 by 103. This will shift the decimal point 3 places to the left.
data test;
number=24536.8746;
step1=10**3;
step2=int(number*step1);
step3=step2/step1;
run;
Truncating decimals in SAS
Truncating decimals in SAS

We hope this article helped you to truncate decimals in SAS. Moreover, if you have other suggestions, suggest them in the comment section below. We will take those lists in our further blog post.

Thanks for reading!

You may also want to see our article on the Length and Precision of SAS Variables.

Every week we'll send you SAS tips and in-depth tutorials

JOIN OUR COMMUNITY OF SAS Programmers!

Check your inbox or spam folder to confirm your subscription.

Post Tags: #Truncate decimal numbers
Subhro

Subhro Kar is an Analyst with over five years of experience. As a programmer specializing in SAS (Statistical Analysis System), Subhro also offers tutorials and guides on how to approach the coding language. His website, 9to5sas, offers students and new programmers useful easy-to-grasp resources to help them understand the fundamentals of SAS. Through this website, he shares his passion for programming while giving back to up-and-coming programmers in the field. Subhro’s mission is to offer quality tips, tricks, and lessons that give SAS beginners the skills they need to succeed.

Facebook Twitter Linkedin

Post navigation

Previous Previous
How to Round Numbers in SAS?
NextContinue
How to Copy datasets in SAS?

SAS Tips in your inbox

Subscribe to 9to5sas for timely SAS tips and news delivered each month.
Learn about the latest articles, and code samples to keep your SAS skills fresh!

Your subscription is successful!

Recent Posts

  • Concatenate strings in SAS: The CAT Functions Demystified
  • 5 Techniques for Quickly Removing Leading Zeros in SAS
  • Troubleshoot Your Proc SQL Code Like a Pro with These 7 Automatic Macro Variables
  • 7 PROC SQL Options You Should Use to Debug Queries
  • How To Use The SAS Proc SQL Order By Statement?
9to5sas
  • Privacy Policy
  • Disclaimer
  • About
  • Contact
Facebook Twitter Instagram RSS Email Pinterest

Copyright © 2023 9TO5SAS, All rights reserved.

Scroll to top
  • 9to5sas Blueprint
  • About
  • About
  • Acceptable use policy
  • calculator
  • Confirm email
  • Contact
  • Contact
  • Cookie Policy
  • DISCLAIMER
  • Getting Started with SAS
  • Glossary
  • Index
  • Post #13801
  • Privacy Policy
  • Privacy policy
  • SAS Programs
  • Styles
  • Subscription confirmed
  • Terms and conditions
  • Thank You