[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]
st: RE: 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
In this case, progress is indeed possible, bringing
in also an idea from Michael Blasnik on getting
the sum of the previous # observations, without
The average of the previous 12 is the
difference between two cumulative sums, divided
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
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: email@example.com
> 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.
> date country ratio program
> 1982m1 bra 0.001943 0
> 1982m2 bra 0.003863 0
* For searches and help try: