Announcement

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

  • numeric format & changed value

    I am using ver.15 and created ID from PIDM (to convert ID to string).
    The PIDM column is %10.0g. However, new ID column is %9.0g when I used 'gen ID = PIDM'.
    I tried format %10.0g and %10.0f, but the ID has been changed and is different with PIDM. See below.
    It should be simple copy but I can't explain or fix this issue. Would you advise me what I did wrong?
    Thanks.


    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long PIDM
    70056666
    70065533
    70075297
    70153329
    70197535
    70222194
    70228218
    70235809
    70290675
    70293111
    70296647
    70310470
    70313455
    70373648
    70404506
    70413121
    70420725
    70422908
    70424551
    70427177
    end
    ------------------ copy up to and including the previous line ------------------

    Click image for larger version

Name:	PIDM to ID.png
Views:	1
Size:	101.7 KB
ID:	1491395


  • #2
    By default your new variable is float. Any format change is not the issue; what can and can't be stored is the problem.

    floats can't always hold those integers exactly: look at your listing to see that there aren't enough bits to do more than hold the nearest multiple of 4 -- and not even that in one case.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long PIDM
    70056666
    70065533
    70075297
    70153329
    70197535
    70222194
    70228218
    70235809
    70290675
    70293111
    70296647
    70310470
    70313455
    70373648
    70404506
    70413121
    70420725
    70422908
    70424551
    70427177
    end
    
    gen ID = PIDM 
    format *ID* %8.0f 
    
    gen diff = ID - PIDM 
    tab diff 
    
    list if diff == 4 
    
           diff |      Freq.     Percent        Cum.
    ------------+-----------------------------------
             -3 |          1        5.00        5.00
             -2 |          4       20.00       25.00
             -1 |          5       25.00       50.00
              0 |          1        5.00       55.00
              1 |          5       25.00       80.00
              2 |          1        5.00       85.00
              3 |          2       10.00       95.00
              4 |          1        5.00      100.00
    ------------+-----------------------------------
          Total |         20      100.00
    
    . 
    . list if diff == 4 
    
         +----------------------------+
         |     PIDM         ID   diff |
         |----------------------------|
     18. | 70422908   70422912      4 |
         +----------------------------+
    Check out clonevar.

    Code:
    . clonevar ID2 = PIDM
    
    . assert ID2 == PIDM

    Comment


    • #3
      Thank you so much Nick for the speedy response. I didn't know that 'float can't always hold those integers exactly.' It never happened before to me. Also, I appreciate your tip for the alternative commands: clonvar. Chul

      Comment


      • #4
        Code:
        help data_types

        Comment


        • #5
          To augment the information in help data_types, here are the limits on storage of decimal integers with full accuracy in the various numeric storage types. The fixed-point variables lose the 27 largest positive values to missing value codes; the similar loss for floating point variables occurs only for the largest exponent, so it doesn't affect the much smaller integer values.

          byte - 7 bits -127 100
          int - 15 bits -32,767 32,740
          long - 31 bits -2,147,483,647 2,147,483,620
          float - 24 bits -16,777,216 16,777,216
          double - 53 bits -9,007,199,254,740,992 9,007,199,254,740,992

          Comment

          Working...
          X