Hi,
My code was inspired by the reply on https://www.statalist.org/forums/for...ection-dataset but I am not sure it is completely applicable to my situation.
This is my dataset:
I want to create a new variable cummean_pm25 which is just the cumulative average of "pm25" over station_id in the exact order the stations' spear appear in this dataex example.
what I did:
But this is what I am getting:
This is not working for my code because it is sorting the data by station_id which I do not want. I am guessing this is because of -bysort-. Also, the cumulative average variable is giving the exact values as pm25.
Thanks,
Anisha
My code was inspired by the reply on https://www.statalist.org/forums/for...ection-dataset but I am not sure it is completely applicable to my situation.
This is my dataset:
Code:
* Example generated by -dataex-. For more info, type help dataex clear input byte station_id float pm25 1 239.93213 9 191.94997 10 190.0258 12 154.82065 8 153.44907 7 146.03094 3 145.07002 end
what I did:
Code:
bysort station_id: gen cummean_pm25 = sum(pm25)/ _n
Code:
* Example generated by -dataex-. For more info, type help dataex clear input byte station_id float(pm25 cummean_pm25) 1 239.93213 239.93213 3 145.07002 145.07002 7 146.03094 146.03094 8 153.44907 153.44907 9 191.94997 191.94997 10 190.0258 190.0258 12 154.82065 154.82065 end
Thanks,
Anisha
Comment