Suppose that I have the following variable, hhid, age, and x.
I calculate the mean by group (hhid) with
and I also want to do that again, but only for age > 50. The desired results are shown as "mean_x50". Any suggestions? Thanks.
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input float(hhid age x mean_x mean_x50) 1 45 1 2.5 2.5 1 51 2 2.5 2.5 1 65 3 2.5 2.5 1 33 4 2.5 2.5 2 30 5 6.5 7.5 2 45 6 6.5 7.5 2 60 7 6.5 7.5 2 62 8 6.5 7.5 end
HTML Code:
bys hhid: egen mean_x = mean(x)
Comment