Hi all,
I have a dataset set up as follows
id year month var1
1 1980 1980m1 x
1 1980 1980m2 x
.......................
1 1980 2006m12 x
1 1981 1980m1 x
1 1981 1980m1 x
........................
1 1981 2006m12 x
2 1980 1980m1 x
2 1980 1980m2 x
.......................
2 1980 2006m12 x
2 1981 1980m1 x
2 1981 1980m1 x
........................
2 1981 2006m12 x
for each id year combination i want to keep the months from april of
that year to april of next year.
i.e for id 1 and year 1980 combination i want to keep months from
april 1980 to april 1981.
again for id 1 and year 1981 combination i want to keep months from
april 1981 to april 1982.
then for,
id 2 and year 1980 combination i want to keep months from april 1980
to april 1981.
again for id 2 and year 1981 combination i want to keep months from
april 1981 to april 1982
so the finaldataset should look like
id year month var1
1 1980 1980m4 x
.......................
1 1980 1981m4 x
1 1981 1981m4 x
1 1981 1980m1 x
........................
1 1981 2006m12 x
2 1980 1980m1 x
2 1980 1980m2 x
.......................
2 1980 2006m12 x
2 1981 1980m1 x
2 1981 1980m1 x
........................
2 1981 2006m12 x
My logic was first to create a variable grp, where grp=group(id
year), then loop over the grps to keep the relevant months. What am i
doing wrong?
********start code**********
* create grp as combination of id and year
sort id year month
levelsof id,local(l)
foreach c of local l {
egen grp=group(id year)
}
* loop over the groups to keep the relevant months
levelsof grp, local(m)
foreach d of local m {
summ year if grp==`d'
local a=`r(mean)'
local b=`a'+1
di `a' " " `b'
drop if month<m(`a'm4) | month>m(`b'm4) & grp==`d'
}
*******end code************
--
thanks for your time
rich
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/