Announcement

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

  • error for displaying a large number

    Hello,

    To merge, I need to make a specific identifier for each row as you can see below

    Code:
    clear
    ccode1 ccode2 year dyad
    2 20 1920 2020
    2 20 1921 2020
    2 20 1922 2020
    2 20 1923 2020
    2 20 1924 2020
    end
    I executed the code like this

    "gen dyad_year=ccode1*1000000000 + ccode2*10000 + year"

    What I expected to see in dyad_year for the first row is

    2000201920

    and the second row in dyad_year is

    2000201921

    and same for the remaining rows.


    But, when I executed the code above, the results like this

    Code:
    ccode1 ccode2 year dyad dyad_year
    2 20 1920 2020 2000201984
    2 20 1921 2020 2000201984
    2 20 1922 2020 2000201984
    2 20 1923 2020 2000201984
    2 20 1924 2020 2000201984
    end

    I am wondering why my code does not work.

    Thank you



    Last edited by Joonho Kim; 09 Apr 2019, 14:50.

  • #2
    Because your -gen- command did not specify a storage type, Stata automatically creates the new variable as a float. But floats do not have enough bits to accurately represent 10 digit numbers, so you are experiencing precision errors. See -help precision- for more information. The solution is to force Stata to use a data type with greater precision:

    Code:
    gen long dyad_year=ccode1*1000000000 + ccode2*10000 + year
    Also, depending on how you plan to use this identifier, it might be better to make it a string variable using the -egen, concat()- command. See -help egen-. (But don't do this if you will need to do any calculations with it, or if you will need to use it in an -xtset- or -tsset- command.)

    Comment


    • #3
      It's because you're creating a variable stored as float by default and the largest integer a float can hold is 16,777,216. See help data_types . You need to create the variable as double.

      You can get the result you expect from:

      Code:
      gen double dyad_year=ccode1*1000000000 + ccode2*10000 + year
      format dyad_year %11.0g

      ​​​​​​​
      That said, there's no reason to create this variable for a merge. Just use multiple variables in your merge command.

      Code:
      merge 1:1 ccode1 ccode2 year using "filename"

      Comment


      • #4
        Thank you. It works perfectly.

        Comment

        Working...
        X