Length and Precision of SAS Variables

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.

When you specify the length of SAS variables.SAS stores the value of all numeric variables in floating-point representation.

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.

You can also provide the length of SAS numeric variables with the LENGTH or ATTRIB statement in the DATA step.

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
3 8,192 213 3
4 2,097,152 221 6
5 536,870,912 229 8
6 137,438,953,472 237 11
7 35,184,372,088,832 245 13
8 9,007,199,254,740,992 253 15

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.

NOTE: Use the LENGTH statement to reduce length only for variables whose values are always integers.

Points to remember before applying the LENGTH compression on numeric variables include:

  1. Fractional numbers or numbers with decimals should be left with the default length of 8, as specifying a length less than the minimum required might result in a loss of accuracy due to truncation.
  2.  Not more than 4 bytes of storage is required to store a reasonable SAS date value.
  3. No warnings or error messages are issued in the SAS LOG when the specified length in the LENGTH statement results in the truncation of data.
  4. For numeric variables, the LENGTH statement affects only the data set being created; it does not affect the program data vector.
  5. If the numeric variable contains more than 12 digits, the format changes to E notation. To avoid E notation, you can use best16. format.

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.

However, a DATA step can reference more than 32,767 variables, if you write only 32,767 or fewer variables to the data set. For example, you could drop some variables with a DROP= data set option.

The maximum number of variables a DATA step can reference under Windows is 2,147,483,647.

You can also see our ultimate guide on SAS Numeric functions and Operators.

We hoped this article helped you understand the concepts of Length and Precision of SAS Variables. You may also want to see our SAS Variable conversions article and our guide on SAS Date Functions.

Do you have any tips to add Let us know in the comments?

Please subscribe to our mailing list for weekly updates. You can also find us on Instagram and Facebook.

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

JOIN OUR COMMUNITY OF SAS Programmers!

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.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap