Stata的面向函数式编程——以独董职业背景多元化赫芬达尔指数的计算过程为例
*首先导入原数据clearset more offcd 'E\独董多元化'use example
capture program drop fundhhiprogram define fundhhiversion 16syntax varlist(min=1 max=1) //定义该命令的语法,需传入一个变量参数preserve*把每个独董的背景搞成哑变量,共九个哑变量quietly{forvalues i = 1/9{capture gen `1'`i'=ustrregexm(`1', '`i'')}*分公司年份,计算每种背景的独董人数forvalues i = 1/9{capture bys stkcd year: egen sumfund`i'=sum(`1'`i')}*分公司年份,计算独董总人数capture bys stkcd year: gen dudongsize=_N*计算赫芬达尔指数capture gen sumratio_2=0forvalues i=1/9{capture replace sumratio_2=sumratio_2+(sumfund`i'/dudongsize)^2}gen `1'HHI=1-sumratio_2keep stkcd year `1'HHIduplicates dropsave `1'HHI, replace}restoreend
fundhhi fund
赞 (0)
