Announcement

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

  • Multiplication errors using gen

    I'm using the code below to generate a new variable that is the unique id multiplies by 100. However, the result is not the correct value.

    Code:
    gen entryid=.
    format entryid %12.0g
    replace entryid=(ExternalReference * 100)
    For instance ExternalReference 1000863 does not end up as 100086300, but rather 100086304

    Does anyone know what could cause this?

    Chris

  • #2
    Hi Chris,
    The answer to your problem resumes to One word: Precision.
    By default, Stata creates all new variables in float format, which stores numbers using 4bytes of information.
    see help data_types
    help precision
    If the number is too long, it will round it up to the nearest binary approximation. Try using "double" instead.

    HTH
    Fernando

    Comment


    • #3
      I was going to suggest using "long" rather than "double". For the example Chris gave, either will work.

      Code:
      clear *
      input ExternalReference
      1000863
      end
      
      generate long id1 = .
      generate double id2 = .
      format id1 id2 %12.0g
      replace id1 = ExternalReference * 100
      replace id2 = ExternalReference * 100
      list
      Output:
      Code:
      . list
      
           +----------------------------------+
           | Extern~e         id1         id2 |
           |----------------------------------|
        1. |  1000863   100086300   100086300 |
           +----------------------------------+

      --
      Bruce Weaver
      Email: [email protected]
      Web: http://sites.google.com/a/lakeheadu.ca/bweaver/
      Version: Stata/MP 18.0 (Windows)

      Comment


      • #4
        Thank you. This solved the problem. It would be nice if Stata outputted a warning message to tell the user that it rounded up numbers.
        Chris

        Comment

        Working...
        X