Monday, December 19, 2016

Hack 4.5 Determining How Large an Integer You Can Store in a Numeric Variable

SAS Programming Professionals,

Did you know that the CONSTANT function can help you to determine how large an integer you can store in a numeric variable with a given number of bytes? 

This can be helpful when you are working at slimming-down SAS data sets by reducing the number of bytes for numeric variables.  If you know—and know for sure—the largest value that will be stored in that variable, and that you will only be using it to store integers, then you can set its byte-size accordingly.

Here is how you can use the CONSTANT function with the EXACTINT constant:

data _null_;

do i = 2 to 8 by 1;

exactint = constant('EXACTINT',i);

put i exactint;

end;

run;

The PUT statement produces the following:

2 32
3 8192
4 2097152
5 536870912
6 137438953472
7 3.5184372E13
8 9.0071993E15

So, a numeric variable created with a length of 4 will allow me to store integers up to a value of 2,097,152. Note that these results may vary depending on the platform you are using to run SAS.

Two other CONSTANT function constants that are worth exploring are:  BIG and SMALL.

·        BIG = constant(‘BIG’); - Returns the largest double-precision floating-point number (8-bytes) that can be represented on your computer.

·        SMALL = constant(‘SMALL’); - Returns the smallest double-precision floating-point number (8-bytes) that can be represented on your computer.

Check out the CONSTANT function for more insights on how numbers are handled on your computing platform in the online documentation at support.sas.com.  Then, see if it makes sense to slim down some of your observations by right-sizing your numeric variables.

Best of luck in all your SAS endeavors!

 ---MMMMIIIIKKKKEEEE
(aka Michael A. Raithel)
Author of the new cult classic for computer programmers:  It Only Hurts When I Hit <ENTER>
Print edition:  http://tinyurl.com/z8bzx2e
Kindle edition: http://tinyurl.com/zypgqa7

The hack above is an excerpt from the book:  Did You Know That?  Essential Hacks for Clever SAS Programmers
http://www.amazon.com/Michael-A.-Raithel/e/B001K8GG90/ref=ntt_dp_epwbk_0

No comments:

Post a Comment