stata数据清理与高效语法

stata--数据填充,频率最高的值填充

2019-08-23  本文已影响0人  古城路揸fit人

由来

如果有个年龄是空缺的,且都是学生的话,可以尝试用一个班级出生年最多的年份去替换,空缺学生的出生年份,具体的操作如下

duplicates tag a02a w2clsids if year==2015, generate(dup) //生成dup变量统计某个a02a在w2中重复的频数
bysort w2clsids: egen max_dup = max(dup)  //提取出频数最多的频数
gen age_max = a02a if max_dup  == dup //将频数最多的年份新建成一个age_max变量,但是这个时候那些非频数最多的样本是.,接下来就要用填充的方式,将.填充成频数最多的值

bysort  w2clsids: ipolate age_max w2clsids if year==2015, gen(iage_max) epolate //具体填充
replace a02a = iage_max if year == 2015&a02a==.
cap drop iage_max  dup age_max max_dup
上一篇下一篇

猜你喜欢

热点阅读