人大经济论坛

标题: 【求助】stata中如何merge面板数据? [打印本页]

作者: yummyhpy    时间: 2012-3-30 19:40:28     标题: 【求助】stata中如何merge面板数据?

有两个database,都是面板数据,第一个database里有country,year,kaopen,第二个database里有country,year,fdi, nfa.
有很多国家,每一个国家又对应多个年份的数据。请问怎么在stata中进行merge呢?

我看好多都是merge variable using **.dta  但这里只有一个相同变量啊,比如我用country,但它对应了很多的year啊,这种情况怎么办呢?

新手求各位大侠解答!不胜感激!!

作者: yuedragon    时间: 2012-3-30 20:50:07

这个很简单!在stata中 运行 和help merge ,帮助中有有这样的例子!



    In some datasets, multiple variables are required to identify the
    observations.  Imagine data obtained by observing patients at specific
    points in time so that variables pid and time, taken together, identify
    the observations.  Below we have two such datasets and run a 1:1 merge on
    pid and time,
        . merge 1:1 pid time using filename
            master      +       using        =        merged result
        +-------------+     +-------------+     +-------------------------+
        |pid  time  x1|     |pid  time  x2|     |pid  time  x1  x2  _merge|
        |-------------|     |-------------|     |-------------------------|
        | 14     1   0|     | 14     1   7|     | 14     1   0   7       3|
        | 14     2   0|     | 14     2   9|     | 14     2   0   9       3|
        | 14     4   0|     | 16     1   2|     | 14     4   0   .       1|
        | 16     1   1|     | 16     2   3|     | 16     1   1   2       3|
        | 16     2   1|     | 17     1   5|     | 16     2   1   3       3|
        | 17     1   0|     | 17     2   2|     | 17     1   0   5       3|
        +-------------+     +-------------+     | 17     2   .   2       2|
                                                +-------------------------+
    This is a 1:1 merge because the combination of the values of pid and time
    uniquely identifies observations in both datasets.
    By default, there is nothing about a 1:1 merge that implies that all, or
    even any of, the observations match.  Above five observations matched,
    one observation was only in the master (subject 14 at time 4), and
    another was only in the using (subject 17 at time 2).


作者: sewind_tj    时间: 2012-3-30 23:33:26

merge 1:1 country year using .dta, nogen
作者: qiaqiao    时间: 2012-3-31 14:28:24

我遇到多项合并, 用 merge 1:1 stkcd ym using ""
会提示错误,
variables stkcd ym do not uniquely identify observations in the master data
作者: bbs0805    时间: 2012-4-1 08:28:43

merge 1:1 country year using ,nogen
如果出错,那是你的数据有误:同一年度同一国家出现两次或两次以上的观测
作者: yummyhpy    时间: 2012-4-4 00:54:52

谢谢各位!!已经解决好了!就直接用的merge country year using **.dta  在合并之前要对master和using数据都按country year 要sort一下!
作者: leesisi    时间: 2015-1-27 15:24:23

yummyhpy 发表于 2012-4-4 00:54
谢谢各位!!已经解决好了!就直接用的merge country year using **.dta  在合并之前要对master和using数据 ...
就这样?不用1:1令它一一对应也可以吗?
作者: 6002the_micky    时间: 2015-3-25 20:19:00

上面的方法试过,都不行。。怎么办
作者: yuxinyang88    时间: 2018-2-9 16:33:20

bbs0805 发表于 2012-4-1 08:28
merge 1:1 country year using ,nogen
如果出错,那是你的数据有误:同一年度同一国家出现两次或两次以上的 ...
大神好厉害!
作者: 六小妖    时间: 2018-12-27 11:50:47

leesisi 发表于 2015-1-27 15:24
就这样?不用1:1令它一一对应也可以吗?
能说下具体的命令吗?
作者: 2016ting    时间: 2020-1-22 18:56:48

yummyhpy 发表于 2012-4-4 00:54
谢谢各位!!已经解决好了!就直接用的merge country year using **.dta  在合并之前要对master和using数据 ...
合并之前的工作是什么?可以具体一些吗?谢谢楼主




欢迎光临 人大经济论坛 (https://bbs.pinggu.org/) Powered by Discuz! X2