Statalist


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

Re: st: RE: loops question- solved!


From   "Richard Harvey" <[email protected]>
To   [email protected]
Subject   Re: st: RE: loops question- solved!
Date   Tue, 13 Jan 2009 13:39:50 +0000

Hi martin,

thanks very much

I managed to arrive at the solution, i made two modifications to the
code. I created a indicator variable and then dropped the observations
from outside the loop also i changed the condition statement.

egen grp=group(id year)
gen ind=.

set 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'
replace ind=1 if month>m(`a'm4) & month <= m(`b'm4) & grp==`d'
}
drop if ind~=1

and that gives me exactly what i want..

    id   year     month   var1   grp   ind
     1   1980    1980m4      7     1     1
     1   1980    1980m5     14     1     1
     1   1980    1980m6      3     1     1
     1   1980    1980m7     16     1     1
     1   1980    1980m8     18     1     1
     1   1980    1980m9      4     1     1
     1   1980   1980m10      2     1     1
     1   1980   1980m11      0     1     1
     1   1980   1980m12     13     1     1
     1   1980    1981m1     20     1     1
     1   1980    1981m2     11     1     1
     1   1980    1981m3     13     1     1
     1   1980    1981m4     21     1     1
     1   1981    1981m4     21     2     1
     1   1981    1981m5     12     2     1
     1   1981    1981m6     25     2     1
     1   1981    1981m7      7     2     1
     1   1981    1981m8      4     2     1
     1   1981    1981m9     16     2     1
     1   1981   1981m10      4     2     1
     1   1981   1981m11     19     2     1
     1   1981   1981m12     16     2     1
     1   1981    1982m1     17     2     1
     1   1981    1982m2      3     2     1
     1   1981    1982m3     17     2     1
     1   1981    1982m4     17     2     1
     2   1980    1980m4     12     3     1
     2   1980    1980m5      1     3     1
     2   1980    1980m6      8     3     1
     2   1980    1980m7     13     3     1
     2   1980    1980m8      2     3     1
     2   1980    1980m9      8     3     1
     2   1980   1980m10     17     3     1
     2   1980   1980m11     14     3     1
     2   1980   1980m12     35     3     1
     2   1980    1981m1      7     3     1
     2   1980    1981m2      4     3     1
     2   1980    1981m3     21     3     1
     2   1980    1981m4     11     3     1
     2   1981    1981m4     11     4     1
     2   1981    1981m5      1     4     1
     2   1981    1981m6     17     4     1
     2   1981    1981m7     22     4     1
     2   1981    1981m8      8     4     1
     2   1981    1981m9     31     4     1
     2   1981   1981m10     16     4     1
     2   1981   1981m11     20     4     1
     2   1981   1981m12      4     4     1
     2   1981    1982m1     13     4     1
     2   1981    1982m2      9     4     1
     2   1981    1982m3     15     4     1
     2   1981    1982m4     24     4     1


thanks very much for your time

regards
rich

2009/1/13 Martin Weiss <[email protected]>:
> Line for the server...
>
> Are you positively sure the evaluation order is correct in your -drop-
> statement (see [U]13.3)? Use braces where necessary! Note the different
> number of dropped observations in
>
> **********
> sysuse auto, clear
> drop if rep78==4 | f==1 & weight>2500
> sysuse auto, clear
> drop if (rep78==4 | f==1) & weight>2500
> **********
>
> HTH
> Martin
>
> -----Original Message-----
> From: [email protected]
> [mailto:[email protected]] On Behalf Of Richard Harvey
> Sent: Tuesday, January 13, 2009 12:42 PM
> To: [email protected]
> Subject: Re: st: RE: loops question
>
> 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
> *
> *   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/



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