Dear Stata users,
My question is about total() function of egen. When I caculate ratio of total A to total B, I should use egen total function twice. Is there any way to avoid this normal procedure?
Can I generate my aimed variable (mean_age in above) with one line of expression?
My question is about total() function of egen. When I caculate ratio of total A to total B, I should use egen total function twice. Is there any way to avoid this normal procedure?
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input byte age long pop 0 129244 1 129031 2 129677 3 131844 4 134233 5 136257 6 138150 7 138177 8 138052 9 138230 10 138593 11 137820 12 136015 13 135181 14 134080 15 132227 16 130176 17 128334 18 126876 19 125581 20 124454 21 123764 22 123594 23 122466 24 120914 25 120375 26 120134 27 120039 28 119797 29 119478 30 119531 31 120137 32 122771 33 123947 34 121942 35 120806 36 119443 37 116626 38 113716 39 110961 40 109775 41 108046 42 104884 43 102155 44 98928 45 96236 46 95056 47 94399 48 94159 49 94360 50 93960 51 93335 52 92909 53 91469 54 89414 55 86034 56 83016 57 82439 58 81545 59 81147 60 77985 61 69666 62 64326 63 62817 64 62087 65 62270 66 60297 67 57748 68 55728 69 52611 70 50107 71 47085 72 43855 73 40987 74 37399 75 34444 76 31505 77 27617 78 24507 79 22632 80 20922 81 19402 82 17868 83 16106 84 14328 85 12723 86 11133 87 9577 88 8080 89 6691 90 5514 91 4476 92 3575 93 2781 94 2098 95 1543 96 1103 97 776 98 530 99 349 100 550 end
Code:
gen age_specific=age*pop egen total_age=total(age_specific) egen total_pop=total(pop) gen mean_age=total_age/total_pop
Comment