Statalist


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

RE: st: RE: loops question- solved!


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: RE: loops question- solved!
Date   Tue, 13 Jan 2009 17:51:13 -0000

Although you report success, there seems to be a difficulty with your
approach. 

By construction, values with the same value of -grp- all have the same
value of -year-. Conversely, observations in the same group of -grp-
cannot span two calendar years. 

Focus on the qualifier

... if month>m(`a'm4) & month <= m(`b'm4) & grp==`d'

Here a is the year corresponding to d and b is a + 1; however, it
follows that no observation can be in year b and also in group d. 

Seemingly, this problem was not fatal. 

With similar problems in the past, I've found the best approach to
subdivide according to the non-calendar years that are the time units.
For example, there is an -egen- function in -egenmore- with that intent.
The principles behind it may be relevant to your problem. 

Nick 
[email protected] 

Richard Harvey

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



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