# 驗(yàn)前和驗(yàn)后數(shù)據(jù)的橫向合并/縱向追加
[**Source:** Pre and Post Test Data Merge/Append (By Francis at 11/29/2012)](http://www.econometricsbysimulation.com/2012/11/pre-and-post-test-data-mergeappend.html)
假設(shè)現(xiàn)在有以下2組數(shù)據(jù)
- 預(yù)測(cè)試數(shù)據(jù)(**Pre-test**)
- 當(dāng)前數(shù)據(jù)(**Current Data**)[^1]
[^1]: 是指當(dāng)前在 Stata 界面的數(shù)據(jù),又稱主要數(shù)據(jù)( Master Data )窗口數(shù)據(jù)或記憶空間數(shù)據(jù)。
若想要將這兩種數(shù)據(jù)合并在一起,可以有以下兩種選擇
**(1)創(chuàng)建縱向數(shù)據(jù)集(使用 `append` 命令)**
**(2)創(chuàng)建橫向數(shù)據(jù)集(使用 `merge` 命令)**
這兩種數(shù)據(jù)合并方法都是很常見(jiàn)的,具體實(shí)現(xiàn)步驟如下:
? ## 1. 數(shù)據(jù)的生成?
? ### 1.1 預(yù)測(cè)試數(shù)據(jù)的生成(Pre-test Data)
```stata
clear
set obs 1000?
gen ID = _n? ? ? ? ? ? ? ? // unique identifier on each person/student
gen score = rnormal()
forv i=11/99 {? ? ? ? ? ? // generate say 88 variables (v11 through v99)
gen v`i' = runiform()
label var v`i' "Random uniform variable `i' - pretest"
}
save pretest, replace
```
- 通過(guò) `rnormal`? 以及 `runiform` 命令生成包含 **ID** **score** **v11** 等 91 個(gè)變量在內(nèi)的1000個(gè)觀測(cè)值,并保存為預(yù)測(cè)試數(shù)據(jù)。
### 1.2 當(dāng)前數(shù)據(jù)的生成(Current Data)
```stata
gen treatment = rbinomial(1,.3)? ? ? //imagine there is some kind of treatmens
replace score = score+.4*treatment
forv i=11/99 {
local change = rbinomial(1,.1)? ? ? //There is a 10% chance that one of your other variables will be changed
if `change'==1 replace v`i' = v`i'+runiform()
label var v`i' "Random uniform variable `i' - current"
}
save current, replace
clear
```
- 通過(guò) `rbinomial` `runiform` 命令在預(yù)測(cè)試數(shù)據(jù)基礎(chǔ)上對(duì)變量進(jìn)行一定改變,新增 **treatment** 變量后保存為當(dāng)前數(shù)據(jù)?,F(xiàn)在我們得到擁有不同變量的兩個(gè)數(shù)據(jù)集。
******************************************************************
? ## 2. 數(shù)據(jù)文件的合并?
? ### 2.1 數(shù)據(jù)的縱向合并 -- `append` 命令
``` stata
use pretest, clear?
gen phase = "Pretest"? ? ? ? ? ? ? ? ? ? ? // Generating a variable to indicate pretest
append using current? ? ? ? ? ? ? ? ? ? ? ? // append: Appending the data
sum
bysort ID: egen treat = mean(treatment)? ? //Make sure the "treatment" variable is duplicated for every observation.
drop treatment? ? ? ? ? ? ? ? ? ? ? ? ? ? // Drop the old treatment variable
rename treat treatment
clear
```
部分結(jié)果如下:
```?????????????????
-------------+---------------------------------------------------------
? ? ? ? v94 |? ? ? 2,000? ? .5158958? ? .2936431? .0002596? .9973774
? ? ? ? v95 |? ? ? 2,000? ? .4983512? ? .2921214? .0008446? .9998845
? ? ? ? v96 |? ? ? 2,000? ? .5235505? ? .2834689? .0023575? .9992944
? ? ? ? v97 |? ? ? 2,000? ? .503752? ? .2869586? .0008776? .9996563
? ? ? ? v98 |? ? ? 2,000? ? .4861402? ? .2938694? .0000774? .9997599
-------------+---------------------------------------------------------
? ? ? ? v99 |? ? ? 2,000? ? .5110351? ? .2851361? .0003785? ? .999744
? ? ? phase |? ? ? ? ? 0
? treatment |? ? ? 1,000? ? ? ? .299? ? .4580489? ? ? ? ? 0? ? ? ? ? 1
```
? **注意事項(xiàng):**
1. 在絕大部分的縱向合并中,觀察值可以是獨(dú)立的來(lái)自于同一調(diào)查的不同樣本,也可能不是完全獨(dú)立的來(lái)自于不同調(diào)查時(shí)間的相同樣本
2. 合并后,數(shù)據(jù)的變量基本不變,但觀測(cè)個(gè)案增加了 (例如這里的觀測(cè)值由原來(lái)的 1000 變?yōu)?2000 )。
3. 該方法可以將兩個(gè)或多個(gè)數(shù)據(jù)文件進(jìn)行上下對(duì)接,簡(jiǎn)單明了不易出錯(cuò).
? ### 2.2 數(shù)據(jù)的橫向合并 -- `merge` 命令
? 代碼實(shí)現(xiàn)如下:
```stata
use pretest, clear
foreach v of varlist * {
rename `v' `v'_0? ? ? ? ? ? ? ? ? ? ? ? ? // Rename variables so that they will not be overwritten
}
rename ID_0 ID? ? ? ? ? ? ? ? ? ? ? ? ? ? // Make sure only the merging variable keeps the same name.
save pretest_rename, replace
use current, clear? ? ? ? ? ? ? ? ? ? ? ? // Loading the current test data.
foreach v of varlist * {
rename `v' `v'_1? ? ? ? ? ? ? ? ? ? ? ? ? // Rename the variables in the current as well?
}
rename ID_1 ID? ? ? ? ? ? ? ? ? ? ? ? ? ? // Making sure to change ID_1 back to ID
merge 1:1 ID using pretest_rename? ? ? ? ? //Merge the data together
order *, alphabetic
```
? 結(jié)果如下:
```
. merge 1:1 ID using pretest_rename? ? ? ? ?
? ? Result? ? ? ? ? ? ? ? ? ? ? ? ? # of obs.
? ? -----------------------------------------
? ? not matched? ? ? ? ? ? ? ? ? ? ? ? ? ? 0
? ? matched? ? ? ? ? ? ? ? ? ? ? ? ? ? 1,000? (_merge==3)
? ? -----------------------------------------
```
**注意事項(xiàng):**
1. 故橫向合并也是變量的合并,新數(shù)據(jù)的變量增加但觀察個(gè)案可以不變(例如,這里合并之后觀測(cè)值依然為 1000 個(gè))。
2. 實(shí)現(xiàn)數(shù)據(jù)文件的橫向合并的前提是,主要數(shù)據(jù)和使用數(shù)據(jù)必須有一個(gè)(或多個(gè))相同的關(guān)鍵變量
3. 橫向合并中,除關(guān)鍵變量外,其它變量的名稱都不能相同。若兩個(gè)數(shù)據(jù)包含其它的同名變量,則使用數(shù)據(jù)中的變量數(shù)值將被主要數(shù)據(jù)的同名變量數(shù)值取代。