Chapter Contents

Previous

Next
SAS Companion for the CMS Environment

Using the LENGTH Statement to Save Storage Space

By default, when SAS writes a numeric variable to a SAS data set, it writes the number in IBM double-wide floating-point format (as described in Representation of Floating-Point Numbers ). In this format, 8 bytes are required to store a number in a SAS data set with full precision. However, you can use the LENGTH statement in the DATA step to specify that you want to store a particular numeric variable in fewer bytes.

Using the LENGTH statement can greatly reduce the amount of space that is required to store your data. For example, if you were storing a series of test scores whose values could range from 0 to 100, you could use numeric variables with a length of 2 bytes. This would save 6 bytes of storage per variable for each observation in your data set.

However, you must use the LENGTH statement cautiously in order to avoid losing significant data. One byte is always used to store the exponent and the sign. The remaining bytes are used for the mantissa. When you store a numeric variable in fewer than 8 bytes, the least significant digits of the mantissa are truncated. If the part of the mantissa that is truncated contains any non-zero digits, then precision is lost.

Use the LENGTH statement only for variables whose values are always integers. Fractional numbers lose precision if they are truncated. In addition, you must ensure that the values of your variable will always be represented exactly in the number of bytes that you specify. You can use Significant Digits and Largest Integer by Length for SAS Variables under CMS to determine the largest integer that can be stored in numeric variables of various lengths.

Significant Digits and Largest Integer by Length for SAS Variables under CMS
Length in Bytes Significant Digits Retained Largest Integer Represented Exactly
2 2 256
3 4 65,536
4 7 16,777,216
5 9 4,294,967,296
6 12 1,099,511,627,776
7 14 281,474,946,710,656
8 16 72,057,594,037,927,936

Note:   No warning is issued when the length that you specify in the LENGTH statement results in truncated data.  [cautionend]


Chapter Contents

Previous

Next

Top of Page

Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.