摘自
STATADATAMANAGEMENT
REFERENCEMANUAL
RELEASE 12
m:1 merges
In an m:1 merge, the key variable or variables uniquelyidentify the observations in the using data, but not necessarily in the master data.Suppose you had person-level data within regions and you wished to bring in regionaldata. Here is an example:
. mergem:1 region using filename
master + using = merged result
| id | region | a | | region | x | | id | region | a | x | _merge |
| 1 | 2 | 26 | | 1 | 15 | | 1 | 2 | 26 | 13 | 3 |
| 2 | 1 | 29 | | 2 | 13 | | 2 | 1 | 29 | 15 | 3 |
| 3 | 2 | 22 | | 3 | 12 | | 3 | 2 | 22 | 13 | 3 |
| 4 | 3 | 21 | | 4 | 11 | | 4 | 3 | 21 | 12 | 3 |
| 5 | 1 | 24 | | | | | 5 | 1 | 24 | 15 | 3 |
| 6 | 5 | 20 | | | | | 6 | 5 | 20 | . | 1 |
| | | | | | | | . | 4 | . | 11 | 2 |
To bring in the regional information, we need to mergeon region. The values of region identify individual observations in the using data,but it is not an identifier in the master data.
We show the merged dataset sorted by id because this makesit easier to see how the merged dataset was constructed. For each observation inthe master data, merge finds the corresponding observation in the using data. mergecombines the values of the variables in the using dataset to the observations inthe master dataset.
1:m merges
1:m merges are similarto m:1, except that now the key variablesidentify unique observations in the master dataset.Any datasets that can be merged using an m:1 merge may be merged using a 1:m merge by reversingthe roles of the master and using datasets. Here is the same example as used previously, with the master and using datasets reversed:
. merge 1:m region using filename
master + using = merged result
| region | x | | id | region | a | | region | x | id | a | _merge |
| 1 | 15 | | 1 | 2 | 26 | | 1 | 15 | 2 | 29 | 3 |
| 2 | 13 | | 2 | 1 | 29 | | 1 | 15 | 5 | 24 | 3 |
| 3 | 12 | | 3 | 2 | 22 | | 2 | 13 | 1 | 26 | 3 |
| 4 | 11 | | 4 | 3 | 21 | | 2 | 13 | 3 | 22 | 3 |
| | | | 5 | 1 | 24 | | 3 | 12 | 4 | 21 | 3 |
| | | | 6 | 5 | 20 | | 4 | 11 | . | . | 1 |
| | | | | | | | 5 | . | 6 | 20 | 2 |
This merged result is identical to the merged result in the previous section, except for the sort order and the contentsof merge. This time, we show the merged result sorted by region rather than id. Reversing the roles of the files causes a reversal in the 1s and 2s for merge:where merge was previously 1, itis now 2, and vice versa. These exchangedmerge values reflect the reversed roles of the masterand using data.
For each observation in the master data, merge found the corresponding observation(s) in the using data and then wrote down the matched or unmatched result.Once the master observations wereexhausted, merge wrote down any observations from the using data that were never used.
一言以蔽之,就是1:m
左边的变量的观测值是唯一的(不会出现重复值)
m:1
右边的变量的观测值是唯一的(不会出现重复值)
大概就这意思。。