STATA数据合并与追加(一)Merge 您所在的位置:网站首页 stata如何新建一个数据集 STATA数据合并与追加(一)Merge

STATA数据合并与追加(一)Merge

2024-06-28 00:40| 来源: 网络整理| 查看: 265

文章目录 Why Merge?一、Description 简要描述二、Syntax 命令语法1.语法示例2.Options 参数选项Merge命令的options 参数表参数具体说明 3.Remarks and examples 具例说明3.1 Overview 概述部分(1)Merge 1:1 varlist......指定一对一匹配合并(2)Merge 1:m 和 Merge m:1分别指定一对多和多对一匹配合并。(2.1) merge 1:m *varlist*... 指定一对多的匹配合并(2.2)merge m:1 *varlist*... 指定多对一的匹配合并 (3)merge m:m *varlist* 指定多对多合并匹配(4)merge 1:1 _n ..... 执行序列合并 3.2 Basic Description 基本描述3.3 【1:1 merges】 一对一Example 1 | merge 1:1 3.4 【m:1 merges】 多对一Example 2 | merge m:1 3.5 Overlapping variables3.6 【1:m merges】 多对一 结语

Why Merge?

数据合并与追加是进行实证研究中数据处理工作的重要一环,而其中对Merge命令的理解要求较高,需要操作者清楚自己的多个panel合并前后的数据样式、合并方式、对应的关键标识变量与相应合并后结构。

为了加深自己的理解,也希望能帮助到在Merge命令的使用中遇到了一些困难的同志们,故对STATA中的Merge文档进行了翻译和解读,本文不会照搬原文翻译,更多的是进行通俗地解释,部分个人认为不重要的内容也不会赘述,同时对部分内容和文档例子进行了合并。

对于本文的读者而言,关键在于建立具体数据集结构的认识和加深对于“一”和“多”的理解。

笔者认为,“一”与“多”是指所选的关键变量能够唯一识别匹配单个观测值,还是匹配多个观测值。

Source: --> STATA中输入命令 help merge即可查看文档说明

注:因为命令、参数与输出结果多为英文,故本文对主表和使用表的表述大多以英文为主,即master 与 using.

此外,本人非软件专业人士,此次撰文仅做学习、加深理解和经验分享使用,如有遗漏或错误欢迎各位斧正,谢谢!

一、Description 简要描述

1.Merge joins corresponding observations from the dataset currently in memory (called the master dataset) with those from filename.dta (called the using dataset), matching on one or more key variables. merge can perform match merges (one-to-one, one-to-many, many-to-one, and many-tomany), which are often called joins by database people. merge can also perform sequential merges, which have no equivalent in the relational database world.

---------Translation:--------- Merge命令通过匹配一个或者多个关键变量将当前内存中的数据集(称为master dataset,主数据集,通俗讲就是目前已经导入到STATA内存中的数据集)中的相应观察值与其他所用文件中的数据集对应连接起来,具体就是通过filename.dta来指定进行合并的另一个使用数据集(称为using dataset,即合并使用数据集) Merge可以执行匹配合并(包括一对一、一对多、多对一和多对多),也就是数据库中所谓的连接(也就是 join,没学过数据库的同志可以去搜一搜)。 此外Merge也可以进行顺序合并(不同于匹配合并,顺序合并在关系数据库中没有相匹配的概念)

2.Merge is for adding new variables from a second dataset to existing observations. You use merge, for instance, when combining hospital patient and discharge datasets. If you wish to add new observations to existing variables, then see [D] append. You use append, for instance, when adding current discharges to past discharges.

---------Translation:--------- Merge是将第二个数据集中的新变量加入到现有的观察值中。例如,在合并医院患者与出院数据集时就可以采用Merge。 如果您希望对现有变量纵向添加新的观察值或样本,可以考虑命令append。

3.By default, Merge creates a new variable, Merge, containing numeric codes concerning the source and the contents of each observation in the merged dataset. These codes are explained below in the match results table.

---------Translation:--------- 在默认情况下,Merge命令使用后会自动生成一个新的变量_merge,其中包含与合并数据集中的源和每个观察的内容相关的数值代码。下面的匹配结果表对这些代码进行了解释。 通俗地讲,Merge命令运行时,会伴随生成Merge变量,它说明了合并数据中每个观察样本数据的来源(来自主数据集master dataset或合并使用数据集using dataset)

4.Key variables cannot be strLs. If filename is specified without an extension, then .dta is assumed.

---------Translation:--------- 其中需要注意的是: 1)关键匹配变量不能为字符型数据 遇到这种情况可以使用destring [string_var_name],replace force转换为数值型变量 2)如果传入的文件名没有对应拓展名,则假定为.dta格式

二、Syntax 命令语法 1.语法示例

代码如下(示例):

One-to-one merge on specified key variables

通过关键变量进行`一对一`合并 merge 1:1 varlist using filename [,options]

Many-to-one merge on specified key variables

通过关键变量进行`多对一`合并 merge m:1 varlist using filename [,options]

One-to-many merge on specified key variables

通过关键变量进行`一对多`合并 merge 1:m varlist using filename [,options]

Many-to-many merge on specified key variables

通过关键变量进行`多对多`合并 merge m:m varlist using filename [,options]

One-to-one merge by observations

通过观察值进行`一对一`序列合并 merge 1:1 _n using filename [,options] 2.Options 参数选项 Merge命令的options 参数表 optionsDescriptionkeepusing(varlist)合并数据时,在合并数据集using data中保留的变量; 一般默认为全部保留generate用以生成标记Merge结果的新变量名,默认为_mergenogenerate要求不创建_merge变量nolabel不沿用using data中的值标签nonotes不沿用using data中的注释update用using data中的数据更新主表master data中同名变量的缺失值replace用using data中的非缺失值数据更新主表master data中所有同名变量的数据noreport不展示匹配结果输出表force允许字符/数值型不匹配不同时不报错assert(results)指定所需的匹配结果keep(results)指定所需保留的匹配结果sorted不需排序,数据集已经自身排序 参数具体说明

代码如下(示例):

这部分因为暂先用不到就先不写了,占位占位! 3.Remarks and examples 具例说明 3.1 Overview 概述部分

(1) merge 1:1 varlist … specifies a one-to-one match merge. varlist specifies variables common to both datasets that together uniquely identify single observations in both datasets. For instance, suppose you have a dataset of customer information, called customer.dta, and have a second dataset of other information about roughly the same customers, called other.dta. Suppose further that both datasets identify individuals by using the pid variable, and there is only one observation per individual in each dataset. You would merge the two datasets by typing

use customer merge 1:1 pid using other

Reversing the roles of the two files would be fine. Choosing which dataset is the master and which is the using matters only if there are overlapping variable names. 1:1 merges are less common than 1:m and m:1 merges.

---------Translation:---------

(1)Merge 1:1 varlist…指定一对一匹配合并

varlist指定了两个数据集中共同的变量,它们一起唯一地标识两个数据集中的单个观测结果。 例如,假设我们有一个客户信息数据集,称为customer.dta;同时我们拥有第二个数据集,其中包含大致相同客户的其他信息,称为other.dta。 进一步假设两个数据集都使用pid变量来识别个体,并且每个数据集中每个个体只有一次观察。 则可以通过键入来合并两个数据集。 注意:在变量名之间没有重名冲突时,随意颠倒这两个文件的角色(master or using)是可以的。 只有当变量名重叠时,选择某个个数据集是主数据集master dataset,另一个数据集作为使用数据集using dataset才有意义。 1:1 merge不如 1:m 和 m:1 merge常见。

(2) merge 1:m and merge m:1 specify one-to-many and many-to-one match merges, respectively.To illustrate the two choices, suppose you have a dataset containing information about individual hospitals, called hospitals.dta. In this dataset, each observation contains information about one hospital, which is uniquely identified by the hospitalid variable. You have a second dataset called discharges.dta, which contains information on individual hospital stays by many different patients. discharges.dta also identifies hospitals by using the hospitalid variable. You would like to join all the information in both datasets. There are two ways you could do this.

(2.1) merge 1:m varlist … … specifies a one-to-many match merge

use hospitals merge 1:m hospitalid using discharges

would join the discharge data to the hospital data. This is a 1:m merge because hospitalid uniquely identifies individual observations in the dataset in memory (hospitals), but could correspond to many observations in the using dataset.

(2.2) merge m:1 varlist … … specifies a many-to-one match merge

use discahrges merge m:1 hospitalid using hospitals

would join the hospital data to the discharge data. This is an m:1 merge because hospitalid can correspond to many observations in the master dataset, but uniquely identifies individual observations in the using dataset.

---------Translation:---------

(2)Merge 1:m 和 Merge m:1分别指定一对多和多对一匹配合并。

为了说明这两种选择,假设有一个包含有关各个医院的信息的数据集,称为hospitals.dta。 在此数据集中,每次观察都包含关于一家医院的信息,该信息由hospitalid变量唯一标识。

此外,我们有第二个数据集,称为discharges.dta它包含许多不同病人的个人住院信息(也就是多个不同病人可能来自于同一家医院)。discharges.dta可以通过使用hospitalid变量来标识医院。 希望连接两个数据集中的所有信息。 有两种方法可以做到这一点。

(2.1) merge 1:m varlist… 指定一对多的匹配合并 use hospitals merge 1:m hospitalid using discharges

由此(上方的代码)便可将出院数据与医院数据合并。这是一个1:m合并,因为hospitalid在主表master dataset中唯一地标识数据集中的每个单一观测值,但可以对应于使用数据集using dataset中的多个观测值

(2.2)merge m:1 varlist… 指定多对一的匹配合并 use discahrges merge m:1 hospitalid using hospitals

将医院数据与出院数据合并。 这是一个m:1多对一合并,因为hospitalid可以与主数据集master dataset中的多个观测数据对应,但在使用的数据集using dataset中唯一地标识单个观测数据。

所以在这里就已经可以注意到,“一”和“多”的概念,从数据的角度讲,就是看相同的关键变量组是否能唯一识别数据样本,如果可以,那就是“一”,如果对应多个样本,那就是“多”。

(3)merge m:m varlist … specifies a many-to-many match merge. This is allowed for completeness, but it is difficult to imagine an example of when it would be useful. For an m:m merge, varlist does not uniquely identify the observations in either dataset. Matching is performed by combining observations with equal values of varlist; within matching values, the first observation in the master dataset is matched with the first matching observation in the using dataset; the second, with the second; and so on. If there is an unequal number of observations within a group, then the last observation of the shorter group is used repeatedly to match with subsequent observations of the longer group. Use of merge m:m is not encouraged.

---------Translation:---------

(3)merge m:m varlist 指定多对多合并匹配

这是为了保证完整性,但很难想象一个具体的例子来说明什么时候使用它是合适的。 对于m:m多对多合并匹配,关键变量组varlist均不能唯一地识别两个数据集内的观测值。 匹配是通过结合具有相等关键变量值的观测值来实现的;通过值匹配,在主表的第一个观测值与使用表的第一个观测值匹配,相对两表的第二个观测值进行匹配,以此类推等等。 如果一个组内的观测次数不等,那么将重复使用较短数据组的最后一个观测值来匹配较长数据组的后续观测值。 一般不建议使用 merge m:m 进行数据合并

此处文档中对于多对多的匹配的说明不够直接,故作进一步解释。 首先需要明白“多对多”中,“多”是指什么。本人认为,“多”是指确定的关键变量组无法匹配唯一的观测值,而是匹配多个观测值。比如说在记录医院出院数据的主表中,A医院在今天出院的男患者,通过医院id-A、性别-男、date-今天,这三个确定的关键变量组值最终匹配的可能是多个观测值,同理当使用表 using dataset同样有类似情况时,再进行两表merge,就变成了所谓的多对多merge。 那么在这种情况下的匹配规则就变成了,两表(主表和使用表)满足相等关键变量组的观测值进行匹配合并。

(4)merge 1:1 _n … performs a sequential merge. _n is not a variable name; it is Stata syntax for observation number. A sequential merge performs a one-to-one merge on observation number. The first observation of the master dataset is matched with the first observation of the using dataset; the second, with the second; and so on. If there is an unequal number of observations, the remaining observations are unmatched. Sequential merges are dangerous, because they require you to rely on sort order to know that observations belong together. Use this merge at your own risk.

---------Translation:---------

(4)merge 1:1 _n … 执行序列合并

_n不是变量名,而是STATA中对于观测样本数的语法。序列合并对观测数据执行一对一合并。主数据集的第一个序列观测值与使用数据集的第一个序列观测值相匹配,以此类推等等。如果观测值数量不相等,那么其余的观测值也将是不匹配的。 序列合并要求使用者依赖于排序顺序来判断观察值是否可以合成整体,通常使用序列排序会存在较大的风险。

3.2 Basic Description 基本描述

The formal definition for merge behavior is the following: Start with the first observation of the master. Find the corresponding observation in the using data, if there is one. Record the matched or unmatched result. Proceed to the next observation in the master dataset. When you finish working through the master dataset, work through unused observations from the using data. By default, unmatched observations are kept in the merged data, whether they come from the master dataset or the using dataset.

---------Translation:---------

Merge的正式定义如下:从对主表 master dataset的第一个观察值开始,如果在使用数据表 using dataset中找到相应的观察值便进行合并。 同时生成对应变量并赋值以记录成功匹配或未成功匹配的结果。 然后在主数据集中继续进行下一个观察。 当完成对主数据集的处理时,需要对使用数据中未使用的观察结果进行处理。 默认情况下,未匹配的观察数据仍会保存在合并数据中,无论它们来自主数据集 master还是使用数据集 using

3.3 【1:1 merges】 一对一 Example 1 | merge 1:1

首先分别导入使用STATA官方提供的案例,并观察两表结构如下:

use http://www.stata-press.com/data/r15/autosize list

Alt

use http://www.stata-press.com/data/r15/autoexpense list

Alt

此处我们可以看到,表1autosize包含有车款、重量与车身长度三个变量,而表2autoexpense则包含有车款、价格与油耗性能。 最需要关注的是变量make,可以发现其在两表中均可以唯一地标识每一条观测值,也就是对应每一种车款。

通俗地讲,只要变量make的值确定,在两表中均可以找到唯一与其匹配的观测值。这也就满足了“一对一”合并的要求,可以进行该类型的合并,如下:

use http://www.stata-press.com/data/r15/autosize merge 1:1 make using http://www.stata-press.com/data/r15/autoexpense list

在这里插入图片描述

完成合并之后,注意观察_merge变量的具体值。

master onely (1) :合并后观测值数据完全在来自于主表,也就是master表,往往源于使用表的数据缺失,对应观测值的变量值为空using only (2): 合并后观测值数据完全来自于使用表,也就是using表,往往源于主表的数据缺失,对应观测值的变量值为空matched (3):两表成功匹配合并,对应观测值所有变量值分别来自于主表和使用表

一般我们都可以根据自己的需求,通过assert参数来确定合并结果是否是我们所希望的。

比如,我们期望两表能够完全匹配,通过断言参数assert()来输出结果,但如果在合并后并非所有的观测值都能完成匹配,使用assert(match)命令会报错,而且不会输出合并结果表,此时需要我们手动通过tabulate _merge进行结果输出.

use http://www.stata-press.com/data/r15/autosize, clear merge 1:1 make using http://www.stata-press.com/data/r15/autoexpense, assert(match)

merge: after merge, not all observations matched (merged result left in memory)

一般在merge命令执行后便会输出合并结果表(不添加assert参数的情况下),如果有需要再次呈现合并结果,同样可以使用tabulate _merge命令,如果需要明确存在合并问题的观测值,则直接通过list+条件语句进行筛选陈列即可,具体如下:

tabulate _merge

Alt

list if _merge < 3

在这里插入图片描述

后续可以选择将其drop或根据需求进行其他操作.

最后,如果我们希望最终合并得到的数据集中仅含有匹配合并成功的观测值,而不关注是否存在未成功匹配的内容,可以通过使用keep(match)参数来实现这一需求。

由于我们仅保留成功通过关键变量进行匹配合并的观测值,因此一般使用keep(match)参数时没有必要再去生成_merge变量,通过nogenerate可以实现. 具体如下:

use http://www.stata-press.com/data/r15/autosize, clear merge 1:1 make using http://www.stata-press.com/data/r15/autoexpense, keep(match) nogenerate list

在这里插入图片描述

3.4 【m:1 merges】 多对一 Example 2 | merge m:1

关于"多对一"合并同样采用官网的另一组数据及进行举例说明,如下:

use http://www.stata-press.com/data/r15/sforce, clear list #此处为主表 master

在这里插入图片描述

use http://www.stata-press.com/data/r15/dollars list #此处为使用表 using

在这里插入图片描述

注意本例中的两表,在主表master中,变量region本身无法唯一标识观察值,也就是说,同一个region中可能存在多个具有不同的name属性的观测值。 而在使用表 using中,region仍可以唯一标识对应观测值。

这就构成了所谓的“多对一”,即相同的关键变量组,在主表对应识别多个观测值,而在使用表中对应识别唯一的观测值。

具体命令如下:

use http://www.stata-press.com/data/r15/sforce merge m:1 region using http://www.stata-press.com/data/r15/dollars list

多对一合并结果表

同理,如果将之前的using和master进行互换,即为“一对多”合并.

3.5 Overlapping variables

Overlapping variables一般是指两表之间的同名变量。 该部分旨在说明merge如何处理非关键变量的重叠变量,具例如下:

use http://www.stata-press.com/data/r15/overlap1, clear list, sepby(id)

在这里插入图片描述

use http://www.stata-press.com/data/r15/overlap2 list

在这里插入图片描述

通过观察可以发现;

变量id可以作为两表合并的关键变量两表之间存在同名的重叠变量x1和x2

注意到变量id在master中可以标识多个观测值,在using中则唯一标识观测值,因此先考虑采用“多对一”合并,如下:

use http://www.stata-press.com/data/r15/overlap1 merge m:1 id using http://www.stata-press.com/data/r15/overlap2 list, sepby(id)

在这里插入图片描述

通过对比三张表可以发现,即使两表中存在缺失值,合并结果中merge默认对同名重叠变量数据的选择也为保留主表数据.

3.6 【1:m merges】 多对一 use http://www.stata-press.com/data/r15/overlap2, clear merge 1:m id using http://www.stata-press.com/data/r15/overlap1

在这里插入图片描述

结语

以上就是当前阶段对于Merge文档命令的部分翻译和个人理解,欢迎各位参考和斧正!



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有