|
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: RE: loops question
Hi,
Suppose i have only two ids 1 & 2 and two years 1980 and 1981
then i will have four grps. I ran the foloowing code with the trace on
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'
}
and this is what i get...
. foreach d of local m {
2. summ year if grp==`d'
3. local a=`r(mean)'
4. local b=`a'+1
5. di `a' " " `b'
6. drop if month<m(`a'm4) | month>m(`b'm4) & grp==`d'
7. }
- foreach d of local m {
- summ year if grp==`d'
= summ year if grp==1
Variable | Obs Mean Std. Dev. Min Max
-------------+--------------------------------------------------------
year | 312 1980 0 1980 1980
- local a=`r(mean)'
= local a=1980
- local b=`a'+1
= local b=1980+1
- di `a' " " `b'
= di 1980 " " 1981
1980 1981
- drop if month<m(`a'm4) | month>m(`b'm4) & grp==`d'
= drop if month<m(1980m4) | month>m(1981m4) & grp==1
(308 observations deleted)
- }
- summ year if grp==`d'
= summ year if grp==2
Variable | Obs Mean Std. Dev. Min Max
-------------+--------------------------------------------------------
year | 309 1981 0 1981 1981
- local a=`r(mean)'
= local a=1981
- local b=`a'+1
= local b=1981+1
- di `a' " " `b'
= di 1981 " " 1982
1981 1982
- drop if month<m(`a'm4) | month>m(`b'm4) & grp==`d'
= drop if month<m(1981m4) | month>m(1982m4) & grp==2
(332 observations deleted)
- }
- summ year if grp==`d'
= summ year if grp==3
Variable | Obs Mean Std. Dev. Min Max
-------------+--------------------------------------------------------
year | 142 1980 0 1980 1980
- local a=`r(mean)'
= local a=1980
- local b=`a'+1
= local b=1980+1
- di `a' " " `b'
= di 1980 " " 1981
1980 1981
- drop if month<m(`a'm4) | month>m(`b'm4) & grp==`d'
= drop if month<m(1980m4) | month>m(1981m4) & grp==3
(141 observations deleted)
- }
- summ year if grp==`d'
= summ year if grp==4
Variable | Obs Mean Std. Dev. Min Max
-------------+--------------------------------------------------------
year | 142 1981 0 1981 1981
- local a=`r(mean)'
= local a=1981
- local b=`a'+1
= local b=1981+1
- di `a' " " `b'
= di 1981 " " 1982
1981 1982
- drop if month<m(`a'm4) | month>m(`b'm4) & grp==`d'
= drop if month<m(1981m4) | month>m(1982m4) & grp==4
(129 observations deleted)
- }
id year month var1 grp
1 1980 1981m4 21 1
1 1981 1981m4 21 2
1 1981 1981m5 12 2
1 1981 1981m6 25 2
1 1981 1981m7 7 2
1 1981 1981m8 4 2
1 1981 1981m9 16 2
1 1981 1981m10 4 2
1 1981 1981m11 19 2
1 1981 1981m12 16 2
1 1981 1982m1 17 2
1 1981 1982m2 3 2
1 1981 1982m3 17 2
1 1981 1982m4 17 2
2 1980 1981m4 11 3
2 1981 1981m4 11 4
2 1981 1981m5 1 4
2 1981 1981m6 17 4
2 1981 1981m7 22 4
2 1981 1981m8 8 4
2 1981 1981m9 31 4
2 1981 1981m10 16 4
2 1981 1981m11 20 4
2 1981 1981m12 4 4
2 1981 1982m1 13 4
2 1981 1982m2 9 4
2 1981 1982m3 15 4
2 1981 1982m4 24 4
It works for group 2 and 4 but not 1 and 3. I am just not getting it.
rich
2009/1/13 Richard Harvey <[email protected]>:
> hi,
>
> True.. Give that i am forming groups by id and year, that loop is not
> required. I could just say grp=group(id year).
>
> But once the grp had been defined and i run the following code..
>
> 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'
> }
>
> the drop if month<m(`a'm4) | month>m(`b'm4) & grp==`d' should be run
> for each grp seperately should it not?
>
> thanks
> rich
>
>
>
> 2009/1/13 Martin Weiss <[email protected]>:
>> Line for the server...
>>
>> This is much clearer and the story is complete now. It does not sound like a
>> standard task to me. I can spot a problem in
>>
>> sort id year month
>> levelsof id,local(l)
>> foreach c of local l {
>> egen grp=group(id year)
>> }
>>
>> You have at least two levels of id so the -foreach- would cycle at least
>> twice, but after the first run you already have a variable "grp". So the
>> second run through the loop should end with "grp already defined r(110)"?
>> Can you confirm that?(If you do not condition on c in the loop, what is the
>> loop good for anyway?)
>>
>> HTH
>> Martin
>>
>>
>> -----Original Message-----
>> From: [email protected]
>> [mailto:[email protected]] On Behalf Of Richard Harvey
>> Sent: Tuesday, January 13, 2009 12:06 PM
>> To: [email protected]
>> Subject: Re: st: RE: loops question
>>
>> hi martin,
>>
>> I am not sure what you mean by this " So far, as far as I can tell,
>> you have only told the list half the story..."
>>
>> I am sorry if i was not clear earlier. I will try to rephrase it
>>
>> The following is what i have
>> ***************************
>> 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
>>
>>
>> The following is what i want
>> **********************
>> 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
>>
>> and the following is my code that attempts to achieve the above..
>>
>> ********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************
>>
>> and that gives me the following result
>>
>> id year month grp
>> 1181 1980 1981m4 1
>> 1181 1981 1981m4 2
>> 1181 1981 1981m5 2
>> 1181 1981 1981m6 2
>> 1181 1981 1981m7 2
>> 1181 1981 1981m8 2
>> 1181 1981 1981m9 2
>> 1181 1981 1981m10 2
>> 1181 1981 1981m11 2
>> 1181 1981 1981m12 2
>> 1181 1981 1982m1 2
>> 1181 1981 1982m2 2
>> 1181 1981 1982m3 2
>> 1181 1981 1982m4 2
>>
>> So it works fine for the second group but not for the first group.
>>
>> I am wondering why? I hope this is much better.
>>
>>
>> rich
>>
>> p.s what does Line for the server... mean???
>>
>>
>> 2009/1/13 Martin Weiss <[email protected]>:
>>> 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.
>>>
>>> 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/
>>>
>>
>>
>>
>> --
>> 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/
>>
>
>
>
> --
> thanks for your time
> rich
>
--
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/