这是之前某些国家某个变量全部缺失的一般化情况。比如,一项多国家研究,某个指标(eat)在一些国家缺失值比较高,如何删掉缺失值高于一定比例的该变量的数据?
如何确实比例超过30%,则删除该国家的该变量的记录。Stata代码如下:
tab Country eat ,m row bysort Country : gen mis = missing(eat) by Country : replace mis = sum(mis) by Country : replace mis = mis[_N]/_N global p=0.3 drop if mis >$p
反向思考,计算非缺失值的比例来实现。
egen npresent = count(eat), by(Country) //bysort Country: gen mpercent=1-npresent/_N bysort Country: drop if npresent/_N <= 0.7
演示数据:删除种族中union缺失值比例超过31%的记录。
//方法1 webuse nlswork,clear tab race union ,row m egen npresent = count(union), by(race) bysort race: drop if npresent/_N <= 0.69 tab race union ,row m //方法2 webuse nlswork,clear<br />tab race union ,row m bysort race : gen mis = missing(union) by race : replace mis = sum(mis) by race : replace mis = mis[_N]/_N global p=0.31 drop if mis >$p
很好