Dear Statalists, please, could anyone help me?
I have the data set example below and am trying to use egen to calculate by group, but the results for obs with missing values are returning 0. How can I fix this?
Many thanks.
I have the data set example below and am trying to use egen to calculate by group, but the results for obs with missing values are returning 0. How can I fix this?
Many thanks.
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input float(id x1 x2 num den totalnum totalden total) 1 . 3 . .3333333 0 .4242424 0 2 17 10 1.7 .1 2.7555556 .15555556 17.714285 3 . . . . 1.2 .05 24 1 . 11 . .0909091 0 .4242424 0 2 19 18 1.0555556 .05555556 2.7555556 .15555556 17.714285 3 24 20 1.2 .05 1.2 .05 24 end
Code:
gen num = x1/x2 gen den = 1/x2 egen totalnum = total(num), by(id) egen totalden = total(den), by(id) gen total = totalnum/totalden
Comment