SAS programmers often use the Length statement frequently. Understanding the concepts of Length and Precision of SAS variables helps you to efficiently use disk space.
Length of Numeric Variables in SAS
The default length of numeric variables in SAS data sets is 8 bytes. 8 bytes doesn’t mean only 8 digits as most people get confused here. It means you can store up to 16 digits in the variable. The minimum length of the numeric variable in SAS is 3 bytes which can be used to store values up to 8192.
For more details, refer to Numeric Length: Concepts and Consequences.
SAS stores numeric data using floating-point representation. See the table below which specifies the largest integer that can be stored in SAS numeric variables in a specified length:
|Length in Bytes||Largest Integer Represented Exactly||Exponential Notation||Significant Digits Retained|
For example, if your variable holds values from 0 to 100, you can use a length statement to assign the length of 3 to store these numbers. This will save space in your dataset. Below is an example.
data test; length i 3; do i = 1 to 100; output; end; run;
Another example is flag variables whose only values can be either 0 or 1 can also be stored in a variable whose length is 3 bytes.
For more information, see Reducing Data Storage Space for Numeric Variables
If the value of a variable is large than the defined length or it has many significant digits you may lose precision while performing arithmetic calculations.
Points to remember before applying the LENGTH compression on numeric variables include:
The maximum number of variables in a single SAS data set under Windows is 32,767.
This means that observation under Windows cannot be larger than 5MB(ie 5*1024*1024).
Therefore, if you want your data set to contain 32,767 character variables, the largest value each variable can hold is approximately 160 bytes.
You can also see our ultimate guide on SAS Numeric functions and Operators.
Do you have any tips to add Let us know in the comments?