Numeric representation issues in SAS
Things are not always what they seem with numbers in SAS.
This is not really the fault of SAS. (add more)
(need to fill in details)
- Comparing calculated non-integer values to constant values
- Inexact accumulation (special case of the above)
- Insufficient storage to hold even an integer exactly (e.g. large 16-digit numbers on other than mainframe platforms)
- Losing precision when storing non-integer values in fewer than 8 bytes
- Changes in numeric representation or calculated values when moving from one platform to another
- Don't use the LENGTH statement to store a numeric variable in fewer than 8 bytes if it might contain non-integer values.
- Be careful when using the LENGTH statement to store numeric variables in fewer than 8 bytes, even if all of its values are integers. Think about whether the values that need to be stored may increase at some later time. Also take into account the possibility that your program and data might at some point migrate to a different platform that has different numeric storage characteristics. Check a reference table or use the ??? function to determine the largest integer that your platform can store in a given number of bytes. Finally, consider using the COMPRESS=BINARY data set option rather than the LENGTH statement to reduce storage requirements for numeric variables.
- Another option to consider is to convert values to integer. For example, currency values can easily be converted to integer by multiplying them by an appropriate factor (e.g., by 100 for USD, CAD, others) and then using the ROUND function.
|Length in bytes||Mainframe||Unix / PC|
(Source: Tech Report TS-654
- Be careful when comparing numeric values that may be non-integer. If the values were arrived at by accumulation or through other calculations, they may not be exactly what you expect. Use the ROUND function as necessary to eliminate these effects.
- Be careful when incrementing the index value in a DO-loop by a non-integer value. You may not get the number of iterations that you expect. Rather than specifying your ending value after TO, you can put the test in a WHILE or UNTIL clause instead, where you can avoid the problem by using ROUND in the comparison.
- Be careful when using PROC FORMAT to specify ranges for numeric values that are not guaranteed to be integers. Either round the values before applying the format, or use the FUZZ= option in PROC FORMAT.
- Be aware that there may be (very slight) differences in numeric values when data sets or programs are moved from one platform or another.
SAS documentation (9.1.3)
Describes how SAS stores numeric values, discusses how to troubleshoot problems relating to floating-point representation, provides instructions on determining how many bytes are needed to store a number accurately, and summarizes problems that may occur when transferring data between operating systems.
Very detailed discussion, including numeric representation theory, how numbers are represented in SAS, when you may have problems with representation error or loss of significance, how to display exactly what you have, and different methods for handling the problems.
Shorter than TS-230. Somewhat less discussion of numeric theory, less formal tone, a number of examples. Suggests things to keep in mind, includes short section on moving data between operating systems.
Contains an excellent summary of how SAS stores numeric data and what issues you can run into, in an overall quality control context.