Announcement

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

  • Wrong values when generating an equal variable

    Hi,

    This is a sample of my data:

    id year code code2 test check
    2 2015
    2 2016 85182100 85182100 85182096 4
    3 2012
    3 2013
    3 2014
    3 2015
    3 2016 85182100 85182100 85182096 4
    4 2012
    4 2013
    4 2014
    4 2015
    4 2016 85182100 85182100 85182096 4
    5 2012
    5 2013
    5 2014
    5 2015
    5 2016 85182100 85182100 85182096 4
    .......

    Variable "code" is in string form.

    This is what I did:
    Code:
    gen code2 = code1
    destring code2, replace
    gen test = code2
    gen check = code2-test
    I'm pretty sure I literally just copied variable code2 into the column for variable test, however, the values for some of the observations seem to be different from what it should be. The incorrect values are always = code2 +/- {1,2,3,4}. There are others were correctly copied.

    When I browse the dataset, instead of showing test = 85182096, it shows 8.52e+07. I reformatted it so that variable test shows all the digit as shown above. I don't think this would cause the above problem, however?

    I don't really know why or how this would be the case? It seems really bizarre to me. Thank you for your help.




  • #2
    please use -dataex- in the future when showing data examples - see the FAQ for help on this

    when making exact copies of a variable, please use the -clonevar- command

    in particular, your result from -destring- is a double, while you result from "gen test = code2" is a float, so you have a precision problem; see
    Code:
    help precision

    Comment


    • #3
      Originally posted by Rich Goldstein View Post
      please use -dataex- in the future when showing data examples - see the FAQ for help on this

      when making exact copies of a variable, please use the -clonevar- command

      in particular, your result from -destring- is a double, while you result from "gen test = code2" is a float, so you have a precision problem; see
      Code:
      help precision
      Thank you! I just found out that it was the type mismatch issue and fixed it, so it's all good now. Thank you for the suggestion, however. I will keep this in mind when I post another question here. Thanks!

      Comment

      Working...
      X