我试图计算事件发生在特定年龄之前的次数。我有数据显示一生中每个事件的年龄(age_at_event1-age_at_event3),以及我不再对计算此类事件感兴趣的年龄(stop_age)。我想创建一个变量(sum_event),它计算在停止年龄之前感兴趣的事件发生的次数。一个例子:
ID age_event1 age_event2 age_event3 stop_age sum_event
1 10 17 45 34 2
2 23 31 32 54 3
3 25 55 . 32 1
4 21 . . 22 1
如何创建适当的sum_event变量?
答案 0 :(得分:1)
如果您不想reshape
您的数据,那么您可以循环变量并计算:
clear
set more off
*----- example data -----
input ///
ID age_event1 age_event2 age_event3 stop_age sum_event
1 10 17 45 34 2
2 23 31 32 54 3
3 25 55 . 32 1
4 21 . . 22 1
end
list
*----- what you want -----
gen sumevent2 = 0
foreach var of varlist age_event1 age_event2 age_event3 {
replace sumevent2 = sumevent2 + (`var' < stop_age)
}
list
对于遵循某种模式的编号变量,您可以尝试类似:
<snip>
gen sumevent2 = 0
forvalues i = 1/3 {
replace sumevent2 = sumevent2 + (age_event`i' < stop_age)
}
reshape
的另一种方式:
*----- what you want -----
<snip>
reshape long age_event, i(ID) j(j)
bysort ID: egen sumevent2 = total(age_event < stop_age)
reshape wide // if you really need to go back to wide
list