[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

From |
"de la Garza, Adrian" <ADelagarza@imf.org> |

To |
<statalist@hsphsun2.harvard.edu> |

Subject |
st: RE: No need for loops? |

Date |
Thu, 4 Dec 2003 15:14:32 -0500 |

Nick, This is very insightful. I don't understand, however, what the _n<12 does. Can you please clarify? I don't know why, for instance, if for a country my -ratio- variable is not populated (all missing values) but -program- does have 1s now and then, why -prev12- gets a missing value when -program- = 1 but -next12- gets a 0 (when it should be missing, too)? Also, below you wrote: > by country: replace next12 = . if pronext12 | _n < 12 This should be by country: replace next12 = . if pronext12 > 0 | _n < 12 right? Thanks again. Adrian > -----Original Message----- > From: Nick Cox [mailto:n.j.cox@durham.ac.uk] > Sent: Thursday, December 04, 2003 7:21 AM > To: statalist@hsphsun2.harvard.edu > Subject: st: No need for loops? > > > I hope I don't get to regret making that assertion, > which as said was about "most" cases. However, I didn't say > that I would be able to spot such a solution > myself! > > In this case, progress is indeed possible, bringing > in also an idea from Michael Blasnik on getting > the sum of the previous # observations, without > any loops. > > The average of the previous 12 is the > difference between two cumulative sums, divided > by 12 > > bysort country (date) : > gen prev12 = (sum(ratio[_n-1]) - sum(ratio[_n-13])) / 12 > > However, we only want that if -program- is equal to 1 or > (presumably) it is in fact an average based on 12 measurements. > > by country : replace prev12 = . if program != 1 | _n < 12 > > Looking ahead in time seems trickier until you see that > we just need to reverse time (no science fiction or bizarre > physics involved, just negation): > > gen ndate = -date > > Then everything is just the same, modulo _including_ > the month of a program, > > bysort country (ndate) : > gen next12 = (sum(ratio) - sum(ratio[_n-12])) / 12 > > except that we want to keep track of any program in the next > 11 [sic] months. We just look out for a nonzero sum of -program- > > by country : > gen pronext12 = sum(program[_n-1]) - sum(program[_n-12]) > by country: replace next12 = . if pronext12 | _n < 12 > > I guess that Adrian's other variables are calculated > by variations on this theme. > > sort country date > > Some things could be done differently if you have -tsset- > the data. This code assumes no gaps within panels. > > The device of reversed time is exemplified further > at > > http://www.stata.com/support/faqs/data/dropmiss.html > > http://www.stata.com/support/faqs/data/missing.html > > Nick > n.j.cox@durham.ac.uk > > P.S. There is a quite different way to approach > some of this using the program -tsspell- from SSC. In > this you define a spell as starting with > -program- being 1 and then block the subsequent > months making use of a created variable _seq. > > de la Garza, Adrian > > Sent: 04 December 2003 04:10 > > To: statalist@hsphsun2.harvard.edu > > Subject: st: No need for loops? > > > > > > I am trying to learn from Nick Cox's suggestion that no > > loops are needed in most cases for data management. > > However, I can't think of a way to do the following without a loop. > > > > I have the next database in panel format and I need to take > > averages within countries when my dummy variable 'program' > > indicates it. Basically, I need to generate a variable that > > computes the 12-month average for the observations > > immediatly prior to a 1 in 'program'. > > > > Take Brazil in 1983m3, when program = 1. I want my > > generated variable, let's call it ratio_before, to get the > > value of the 12-month average right before 1983m3, and put > > this value in the same line where program = 1. I know this > > average should be 0.00639. > > > > Then, I also want to take averages for the subsequent > > 12-months (including the month when program = 1), for the > > 12-month period after that, etc. These are always annual > > (12-month) averages for the first year when the program is > > implemented as indicated by the 1 in the dummy, then for > > the 2nd year, for the 3rd year, etc., and we can call these > > variables ratio_1y, ratio2y, ratio3y, ratio4y, and ratio5y. > > All of these computed averages should go in the same line > > where program = 1. > > > > The only thing is: I should stop taking the averages for > > the subsequent years if a new program is implemented (i.e., > > if I find another 1 in my 'program' variable within the > > period for the average computed). > > > > I would very much appreciate any insight on this dull data > > management procedure. > > Thanks. > > > > date country ratio program > > 1982m1 bra 0.001943 0 > > 1982m2 bra 0.003863 0 > > * > * For searches and help try: > * http://www.stata.com/support/faqs/res/findit.html > * http://www.stata.com/support/statalist/faq > * http://www.ats.ucla.edu/stat/stata/ > * * For searches and help try: * http://www.stata.com/support/faqs/res/findit.html * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/

**Follow-Ups**:**st: RE: RE: No need for loops?***From:*"Nick Cox" <n.j.cox@durham.ac.uk>

- Prev by Date:
**RE: Furlongs and other measures [was: Re: st: RE: Re: Re: printing resized graphs]** - Next by Date:
**st: RE: No need for loops?** - Previous by thread:
**st: Johnson's Distributions** - Next by thread:
**st: RE: RE: No need for loops?** - Index(es):

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