Stata如何识别一列中的最大值?

如何求后6年的最大值,比如1935年对应1936-1941年间投资的最大值,1936年对应1937-1942年间投资的最大值,以此类推。

方法1:官方命令

webuse grunfeld,clear
bysort company (year): generate invest_max6 = max(invest[_n+1],invest[_n+2],invest[_n+3],invest[_n+4],invest[_n+5],invest[_n+6])
bysort company (year): replace invest_max6 = max(invest_max6,invest_max6[_n-1]) if missing(invest_max6)
list year invest* if company == 1

 

方法2:社区命令asrol

安装:

net install asrol, from(http://fintechprofessor.com) replace

命令

webuse grunfeld,clear
bys company : asrol invest, window(year -10000 6) stat(max) gen(invest_max6_asrol)
list year invest* if company == 1

方法3:社区命令rangestat

安装:

ssc install rangestat,replace

命令:

webuse grunfeld, clear
rangestat (max) invest, interval(year . 6) by(company)
list year invest* if company == 1