1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| global xlist1 "X1 X2 X3 X4 X5 X6" global xlist2 "X7 X8" global xlist "$xlist1 $xlist2"
foreach v of global xlist1 { egen min_`v'= min(`v') egen max_`v'= max(`v') gen `v'_n = (`v'-min_`v')/(max_`v'-min_`v') egen `v'_sum = sum(`v'_n) gen p_`v'= `v'_n/`v'_sum gen k_`v' = -(1/ln(_N)) egen x_`v' = sum(p_`v'*ln(p_`v')) gen e_`v'=k_`v'*x_`v' gen g_`v'=1-e_`v' }
foreach v of global xlist2 { egen min_`v'= min(`v') egen max_`v'= max(`v') gen `v'_n = (max_`v'-`v')/(max_`v'-min_`v') egen `v'_sum = sum(`v'_n) gen p_`v'= `v'_n/`v'_sum gen k_`v' = -(1/ln(_N)) egen x_`v' = sum(p_`v'*ln(p_`v')) gen e_`v'=k_`v'*x_`v' gen g_`v'=1-e_`v' } egen y=rowtotal(g_*),missing
foreach v of global xlist { gen w_`v'= g_`v'/y gen wn_`v'=w_`v'*`v'_n }
egen X=rowtotal(wn_*),missing foreach v of global xlist { replace X=. if wn_`v'==. }
drop min_* max_* *_n *_sum p_* k_* x_* e_* g_* w_* wn_* y
|