I am posting a question for the first time, so I hope I'm doing it correctly.
I have a panel data set and I want Stata to calculate the mean of a variable within a certain group. The group is identified by "country" and "period". "period" is a three-year period, i.e. there are always three year-observations in one "country period" group. I use the following code:
Code:
bysort country period: egen mean_ideology = mean(ideology)
What happens is:
year | country | period | ideology | mean_ideology |
2007 | Belgium | 1 | 3 | 3 |
2008 | Belgium | 1 | 3 | 3 |
2009 | Belgium | 1 | 3 | 3 |
2010 | Belgium | 2 | 2 | 2 |
2011 | Belgium | 2 | 1 | 2 |
2012 | Belgium | 2 | 3 | 2 |
2013 | Belgium | 3 | 3 | 3 |
2014 | Belgium | 3 | . | 3 |
2015 | Belgium | 3 | . | 3 |
year | country | period | ideology | mean_ideology |
2007 | Belgium | 1 | 3 | 3 |
2008 | Belgium | 1 | 3 | 3 |
2009 | Belgium | 1 | 3 | 3 |
2010 | Belgium | 2 | 2 | 2 |
2011 | Belgium | 2 | 1 | 2 |
2012 | Belgium | 2 | 3 | 2 |
2013 | Belgium | 3 | 3 | . |
2014 | Belgium | 3 | . | . |
2015 | Belgium | 3 | . | . |
Code:
bysort country period: replace ideology = . if
but I have been unsuccessful.
I'd be very grateful for some help.
Thank you in advance!
Rike
0 Response to How to tell Stata not to ignore missing values in bysort: egen mean command
Post a Comment