Announcement

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

  • Issue with merging two dataset

    Dear Statalist,
    I am experiencing an issue with the merge fucntion in Stata. I am trying to merge two data files based on a key identifier variable (Stkcd). The two datasets are matched but not matched on the key variable, instead the observations from the using file are simply added to the master file in the order in which they appear in the using file. Any idea why I may be having this issue. Thanks. I attache an example of my databelow (my matched variable is Stkcd)
    Master
    CompanyName1 year Stkcd v1 v2
    Bank A 2015 1 -2.13 1.11
    Bank A 2016 1 -0.10 2.08
    Bank A 2017 1 -7.13 0.29
    Bank A 2018 1 -1.25 1.27
    Bank A 2019 1 -0.91 0.44
    Bank A 2020 1 -1.02 0.92
    Bank A 2021 1 -1.55 -0.11
    Bank B 2015 2 -2.07 -0.09
    Bank B 2016 2 -3.67 0.41
    Bank B 2017 2 -6.12 -0.33
    Bank B 2018 2 -0.51 1.68
    Bank B 2019 2 -1.16 0.66
    Bank B 2020 2 -1.34 1.29
    Bank B 2021 2 -6.26 0.75
    Using
    CompanyName2 SecurityCode_A_StkCd Stkcd
    CompanyA 333 333
    CompanyB 338 338
    Command:
    merge m:1 Stkcd using "using file name .dta"
    Merged data:
    CompanyName1 year Stkcd v1 v2 CompanyNameA2 SecurityCode_A_StkCd
    Bank A 2015 1 -2.13 1.11 CompanyA 333
    Bank A 2016 1 -0.10 2.08 CompanyA 333
    Bank A 2017 1 -7.13 0.29 CompanyA 333
    Bank A 2018 1 -1.25 1.27 CompanyA 333
    Bank A 2019 1 -0.91 0.44 CompanyA 333
    Bank A 2020 1 -1.02 0.92 CompanyA 333
    Bank A 2021 1 -1.55 -0.11 CompanyA 333
    Bank B 2015 2 -2.07 -0.09 CompanyB 338
    Bank B 2016 2 -3.67 0.41 CompanyB 339
    Bank B 2017 2 -6.12 -0.33 CompanyB 340
    Bank B 2018 2 -0.51 1.68 CompanyB 341
    Bank B 2019 2 -1.16 0.66 CompanyB 342
    Bank B 2020 2 -1.34 1.29 CompanyB 343
    Bank B 2021 2 -6.26 0.75 CompanyB 344

  • #2
    Use -dataex- to provide data example. Provide more data from the 'using' dataset. Are you sure you have m:1 relationship between the two dataset and not m:m relationship? It doesn't make sense of what you have posted. Because, if it was a m:1 relationship, the Stkcd 333 and 338 from 'using' would have been added in separate rows to the 'master' dataset as the codes don't appear in the master.
    Last edited by Roman Mostazir; 23 May 2024, 19:58.
    Roman

    Comment

    Working...
    X