Announcement

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

  • Merging Datasets -- Variable Responses Deleted

    Hi all,

    I am merging a number of different datasets and am having trouble with the merge command "deleting" cell values.

    My starting panel dataset looks like this:
    Code:
    county year var1 var2
      1      1   25   30
      1      2   35   22
      1      3   40   50
      2      1   34   30
      2      2   45   44
      2      3   56   21
      3      1   23   12
    I wish to merge it with a dataset that looks like this (i.e., adding two additional variables to the dataset):
    Code:
    county year var3 var4
      1      1   27   28
      1      2   34   32
      1      3   43   42
      2      1   32   33
      2      2   43   24
      2      3   67   23
    However, when I use the merge command Stata "deletes" the original variables (var1 and var2) and turns them into missing values.

    The dataset ends up looking like this:
    Code:
    county year var1 var2 var3 var4
      1      1   .    .   27   28
      1      2   .    .   34   32
      1      3   .    .   43   42
      2      1   .    .   32   33
      2      2   .    .   43   24
      2      3   .    .   67   23
    Here's the basic code I'm using. Sometimes the problem doesn't occur for the first dataset I'm merging and then the deletion happens for the remaining datasets I'm attempting to merge.
    Code:
    merge m:1 county year using "X.dta"
    Any help would be much appreciated. Thank you!
    Matt

  • #2
    I am unable to reproduce your difficulty:
    Code:
    . * Example generated by -dataex-. To install: ssc install dataex
    . clear
    
    . input byte(county year var3 var4)
    
           county      year      var3      var4
      1. 1 1 27 28
      2. 1 2 34 32
      3. 1 3 43 42
      4. 2 1 32 33
      5. 2 2 43 24
      6. 2 3 67 23
      7. end
    
    . tempfile vars_3_4
    
    . save `vars_3_4'
    file C:\Users\CLYDES~1\AppData\Local\Temp\ST_ab74_000001.tmp saved
    
    .  
    . * Example generated by -dataex-. To install: ssc install dataex
    . clear
    
    . input byte(county year var1 var2)
    
           county      year      var1      var2
      1. 1 1 25 30
      2. 1 2 35 22
      3. 1 3 40 50
      4. 2 1 34 30
      5. 2 2 45 44
      6. 2 3 56 21
      7. 3 1 23 12
      8. end
    
    .
    . merge m:1 county year using `vars_3_4'
    
        Result                           # of obs.
        -----------------------------------------
        not matched                             1
            from master                         1  (_merge==1)
            from using                          0  (_merge==2)
    
        matched                                 6  (_merge==3)
        -----------------------------------------
    
    .
    . list, noobs clean
    
        county   year   var1   var2   var3   var4            _merge  
             1      1     25     30     27     28       matched (3)  
             1      2     35     22     34     32       matched (3)  
             1      3     40     50     43     42       matched (3)  
             2      1     34     30     32     33       matched (3)  
             2      2     45     44     43     24       matched (3)  
             2      3     56     21     67     23       matched (3)  
             3      1     23     12      .      .   master only (1)
    Assuming that the examples you showed are representative of your data and that the code you show is really what you did, there would appear to be a problem with your Stata installation. I would undertake the following steps:

    1. Reboot your computer and try again.
    2. If that doesn't work, run -update all- and try again.
    3. If that doesn't work, uninstall Stata, and do a fresh install, followed by -update all- and try again.
    4. If that fails, contact Stata technical support.

    In the future, when showing data examples, please use the -dataex- command to do so, as I have here. If you are running version 15.1 or a fully updated version 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    Comment

    Working...
    X