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/