How to Recognize and Deal with Numeric Representation Error in SAS - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

How to Recognize and Deal with Numeric Representation Error in SAS

Description:

How do computers store numeric data? Overview of floating point binary ... VAX/VMS. 1 bit for the sign. 55 bits for the mantissa. 8 bits for the exponent ... – PowerPoint PPT presentation

Number of Views:50
Avg rating:3.0/5.0
Slides: 24
Provided by: martinedub
Category:

less

Transcript and Presenter's Notes

Title: How to Recognize and Deal with Numeric Representation Error in SAS


1
  • How to Recognize and Deal with Numeric
    Representation Error in SAS

Jeff Martin Serono, Inc.
2
Outline
  • How do computers store numeric data?
  • Overview of floating point binary
  • How does SAS store numeric data?
  • What is numeric representation error?
  • How can you deal with it?

3
Why Worry About This?
  • Brief example
  • data temp
  • initial_val0.3
  • other_val0.13
  • differenceinitial_val-other_val
  • run
  • proc print noobs
  • run

4
Why Worry About This?
  • Output
  • initial_ other_
  • val val difference
  • 0.3 0.3 -5.5511E-17

5
How Do Computers Store Numbers?
  • Integer
  • Decimal or Packed Decimal
  • Floating Point Binary

6
How Do Computers Store Numbers?
  • Integer
  • Pros
  • Simple, straightforward usage and computations
  • Cons
  • Not practical - unable to store fractional values

7
How Do Computers Store Numbers?
  • Decimal or Packed Decimal
  • Pros
  • Able to store fractional values
  • Cons
  • Hardware needs to provide storage for large
    decimals
  • Loss of computational efficiency

8
How Do Computers Store Numbers?
  • Floating Point Binary
  • (used by the SAS System)
  • Pros
  • Able to store fractional values and integer
    values
  • Efficient for computing use of binary numbers
  • Cons
  • Subject to representation error

9
How Does Floating Point Binary Work?
  • Number values are stored as four types of
    information
  • Sign
  • Mantissa
  • Base
  • Exponent

10
How Does Floating Point Binary Work?
  • Example
  • .4321 10 4
  • sign mantissa base
    exponent

11
How Does SAS Store Numbers?
  • Each numeric value is stored in up to 64 bits
    (binary digits) or 8 bytes (groups of 8 bits).
  • Different operating systems may distribute the
    bits differently and have different base value
    (usually 2 or 16).
  • Windows/DOS/Unix
  • 1 bit for the sign,
  • 52 bits for the mantissa
  • 11 bits for the exponent
  • VAX/VMS
  • 1 bit for the sign
  • 55 bits for the mantissa
  • 8 bits for the exponent

12
How Does SAS Store Numbers?
  • Example
  • SEEEEEEE EEEEMMMM MMMMMMMM MMMMMMMM
  • byte 1 byte 2 byte 3 byte 4
  •  
  • MMMMMMMM MMMMMMMM MMMMMMMM MMMMMMMM
  • byte 5 byte 6 byte 7 byte 8
  • S sign
  • E exponent
  • M mantissa

13
The Problem
  • Computers are limited in that they need to use a
    finite set of numbers to represent infinite real
    numbers (note limited bits in previous slide)
  • Affects the amount of precision available in
    which to store the numeric value
  • Computers use binary arithmetic instead of
    decimal arithmetic

14
The Result
  • Values that do not have an exact binary
    representation (ie, 0.1, 1/3, etc.) must be
    approximated
  • Associative law may not apply (ie, (ab)c may
    not equal a(bc))
  • May yield unexpected results
  • Use of different procedures or orders of
    operations may yield different numeric results
    (may cause suspicion of programming error)

15
What do numeric values in SAS really look like?
  • Numeric values in SAS are displayed using
    formatted values rather than the floating point
    representations
  • Even using maximum decimal places will not
    display the full value
  • Only HEX16. format displays the exact value of a
    numeric variable
  • Can help track down representation error

16
What do numeric values in SAS really look like?
  • Example
  • data temp
  • initial_val0.3
  • other_val0.13
  • differenceinitial_val-other_val
  • initial_val_hexput(initial_val,hex16.)
  • other_val_hexput(other_val,hex16.)
  • run
  • proc print noobs
  • run

17
What do numeric values in SAS really look like?
  • Output
  • initial_ other_
  • val val difference
  • 0.3 0.3 -5.5511E-17
  • initial_val_hex other_val_hex
  • 3FD3333333333333 3FD3333333333334

18
What can you do?
  • Rounding the numbers
  • Need to determine the number of significant
    digits to round to
  • Can use the SAS ROUND function

19
How the ROUND function works
  • Scales the value based on the round off unit
  • If the value is positive, adds a constant of
    .500000000001 and lowers the result to the
    nearest integer
  • If the value is negative, subtracts constant of
    .500000000001 and raises the result to the
    nearest integer
  • Scales the value back based on the round off unit

20
What else can you do?
  • FUZZ the comparisons
  • Determine an acceptable difference between two
    values
  • If the difference is smaller than the acceptable
    difference, the two values are considered equal

21
More on fuzzing
  • Options in PROC COMPARE allow you to set a fuzz
    factor when comparing datasets. By using the
    CRITERION and FUZZ options, you can consider
    values within a specified difference to be equal.
  • proc compare basebasedata comparecompdata
  • criterion.000000000001 fuzz.000000000001
  • run
  • Can set up a statement in conditionals
  • If abs(a-b) le .000000000001 then do

22
Conclusions
  • Numeric representatation error is a consequence
    of working in a computing environment
  • While there is no definitive solution to the
    problem, methods exist to adjust for it and still
    get the most out of your data
  • Recognizing that it exists is a step forward in
    dealing with it

23
References
  • Numeric Precision 101, available from
  • http//support.sas.com/techsup/technote/ts654.html
  •  
  • Dealing With Numeric Representation Error,
    available from
  • http//support.sas.com/techsup/technote/ts230.html
  •  
  • Numeric Data in SAS Guidelines for Storage and
    Display
  • Paul Gorrell
  • Social Scientific Systems, Inc.
  • Silver Spring, MD    
Write a Comment
User Comments (0)
About PowerShow.com