我正在处理超过200,000行的数据集。我想根据特定条件用下一行或前一行中的值替换缺失的值。下面的循环只运行一次,但只要缺少指定变量的值,我希望它继续运行。数据如下所示:Stata foreach循环替换缺失值
ID primary_ins primary_ins_collegecodebranch
36 GROSSMONT COLLEGE 120800
37 GROSSMONT COLLEGE 120800
38 GROSSMONT COLLEGE 120800
39 SAN DIEGO STATE UNIVERSITY
40 SAN DIEGO STATE UNIVERSITY
41 SAN DIEGO STATE UNIVERSITY 115100
42 DIEGO STATE UNIVERSITY 115100
43 GROSSMONT COLLEGE 120800
44 GROSSMONT COLLEGE 120800
45 FRESNO CITY COLLEGE 130700
gen primary_ins_collegecodebranch=collegecodebranch if primary_ins==college
foreach x of varlist primary_ins_collegecodebranch{
replace primary_ins_collegecodebranch=primary_ins_collegecodebranch[_n+1] if missing(primary_ins_collegecodebranch) & primary_ins==primary_ins[_n+1]
replace primary_ins_collegecodebranch=primary_ins_collegecodebranch[_n-1] if missing(primary_ins_collegecodebranch) & primary_ins==primary_ins[_n-1]
}