Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • xpose changes large values

    Hi beginner here. When I run xpose on large numbers, the values slightly change. For example:

    Code:
    clear all
    set obs 1
    gen a = 123456789
    xpose, clear
    format * %11.0f
    changes the result to 123456792.

  • #2
    No, it's not changed by -xpose-. It was wrong from the start.

    Code:
    . clear all
    
    . set obs 1
    Number of observations (_N) was 0, now 1.
    
    . gen a = 123456789
    
    . display %12.0f a
       123456792
    This is a precision issue. The default storage type when a new variable is created with -gen- is float. A float does not have the ability to store 9 decimal digits worth of precision. To store that many digits you need either a -double-, or, if the number, as here, is an integer, a -long-. If you -gen long a = 12345789- you will not run into this problem.

    Comment


    • #3
      Thanks you very much Clyde. Originally I was importing data from excel and xpose was changing it. But setting type to double before importing seems to fix the issue.

      Comment


      • #4
        Bob Thomas -

        You are indeed correct that xpose is to blame for the problem you describe in post #3, and Clyde is correct that xpose was not to blame for the problem demonstrated by your example in post #1.

        From help xpose we do see
        Code:
            xpose transposes the data, changing variables into
            observations and observations into variables.  All new
            variables -- that is, those created by the transposition --
            are made the default storage type.
        and further down we see
        Code:
        Options
        
           ...
        
           promote specifies that the transposed data use the most
                compact numeric data type that preserves the original
                data accuracy.
        Here's an example that demonstrates the problem you encountered and the use of the promote option to solve it.
        Code:
        . clear all
        
        . set obs 1
        Number of observations (_N) was 0, now 1.
        
        . generate long a = 123456789
        
        . generate float b = 3.14
        
        . xpose, clear
        
        . describe v1
        
        Variable      Storage   Display    Value
            name         type    format    label      Variable label
        ------------------------------------------------------------
        v1              float   %9.0g                 
        
        . display %12.0f v1[1]
           123456792
        
        . 
        . clear all
        
        . set obs 1
        Number of observations (_N) was 0, now 1.
        
        . generate long a = 123456789
        
        . generate float b = 3.14
        
        . xpose, clear promote
        
        . describe v1
        
        Variable      Storage   Display    Value
            name         type    format    label      Variable label
        ------------------------------------------------------------
        v1              double  %10.0g                
        
        . display %12.0f v1[1]
           123456789
        
        .

        Comment

        Working...
        X