Stata中神奇的bys!
************************Stata中神奇的bys!*************************************
**stata如何分组求平均
**https://bbs.pinggu.org/thread-3693767-1-1.html
bys hhid: egen xxx=mean(yyy)
**stata如何计算分组后的累计和
**https://bbs.pinggu.org/thread-3978474-1-1.html
bys group:egen newvar=sum(var1)
**分组后求最大最小值
**https://bbs.pinggu.org/thread-7120538-1-1.html
bys code year:gen cc=max(aa)
**分组计数
***https://bbs.pinggu.org/thread-2700039-1-1.html
bys communityID: egen TP = count(recipient)
bys communityID: egen TA = sum(recipient)
gen rate = TA/TP
**https://bbs.pinggu.org/thread-3957813-1-1.html
by hhid: egen tmp= count(a3000) if a3000 == 1
by hhid: egen num = mean(tmp)
***老年人占比
bys hhid :egen size=count(hhid)
bys hhid:egen size_old=count(age)if age>=65
gen old=size_old/size
clear
use "E:\share\Raw_data\oldsize.dta"
gen d = (age >= 65) if !missing(age)
bys hhid: egen s = sum(d)
bys hhid: egen st = count(d) if !missing(d)
gen old = s/st
**家庭规模
*https://bbs.pinggu.org/thread-1129519-1-1.html
by no, sort: gen fsize=_N
**分组输出相关系数
***https://bbs.pinggu.org/thread-1521459-1-1.html
statsby,by(g) clear: corr y x
**删除分组重复值
**https://bbs.pinggu.org/thread-9475989-1-1.html
duplicates drop pline size,force
***分位数分组
**https://bbs.pinggu.org/thread-912248-1-1.html
sort insto_ma
xtile prop=insto_ma, nq(4)// 以insto_ma的四分位点分成四组
sort prop insto_msd
egen stdI=xtile(insto_msd),by(prop) nq(4)//按prop,以insto_msd的四分位点分成四组