问题描述
我有一个非常大的数据集,但为了简短起见,我使用以下示例演示了数据:
* Example generated by -dataex-. To install: ssc install dataex
clear
input float(patid death dateofdeath)
1 0 .
2 0 .
3 0 .
4 0 .
5 1 15007
6 0 .
7 0 .
8 1 15526
9 0 .
10 0 .
end
format %d dateofdeath
我正在尝试根据死亡日期进行病例对照研究的样本。在这个阶段,我需要首先创建一个变量,每个死亡日期对所有参与者重复(因此我们最终得到一个包含 20 个参与者的数据集)和一个 pairid
等价于患者 ID patid
对应的案例。
我为一个案例(有效)创建了一个宏,但我发现很难在循环中为所有案例(其中 death==1
)重复它。
成功的宏如下:
local i "5" //patient id who died
gen pairid= `i'
gen matchedindexdate = dateofdeath
replace matchedindexdate=0 if pairid != patid
gsort matchedindexdate
replace matchedindexdate= matchedindexdate[_N]
format matchedindexdate %d
save temp`i'
我尝试的循环是:
* (min and max patid id)
forval j = 1/10 {
count if patid == `j' & death==1
if r(N)=1 {
gen pairid= `j'
gen matchedindexdate = dateofdeath
replace matchedindexdate=0 if pairid != patid
gsort matchedindexdate
replace matchedindexdate= matchedindexdate[_N]
save temp/matched`j'
}
}
use temp/matched1,clear
forval i=2/10 {
capture append using temp/matched`i'
save matched,replace
}
但我明白了:
invalid syntax
如何进行循环?