Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: RE: loops question


From   "Martin Weiss" <[email protected]>
To   <[email protected]>
Subject   st: RE: loops question
Date   Tue, 13 Jan 2009 11:46:39 +0100

Line for the server...

" What am I doing wrong?" 

The answer to that depends on how the outcome of your proposed solution
differs from what you want to achieve. So far, as far as I can tell, you
have only told the list half the story...

HTH
Martin


-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Richard Harvey
Sent: Tuesday, January 13, 2009 11:33 AM
To: [email protected]
Subject: st: loops question

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/

*
*   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/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index