Canned functions don't spring to mind, apart from for means.
No matter, your problems all look straight applications of -by:-. Your
specifications are a little vague about details, so
you will probably need to change the specifics here.
Let's assume a variable -year- running from 1985
to 2004, a bundle of separate countries with identifier
-country- and a series of responses, generically -y-.
Four year periods are defined by
gen year_last = 4 * ceil(year/4)
For example, experiments such as
di 4 * ceil(1986/4)
show that the last year of 1985 ... 1988 is 1988.
Your example may be a little more complicated
and other tricks are possible. A more general
division is
bysort country (year) : gen period = ceil(_n/4)
bysort country period (year) : gen year_last = year[_N]
That assumes a balanced panel.
The initial values are
bysort country year_last (year) : gen y_initial = y[1]
Four year averages are
egen y_mean = mean(y), by(country year_last)
If I were calculating four-year growth rates, I
would just probably just go
bysort country year_last (year) :
gen growth = (100 / 4) * (ln(y[_N]) - ln(y[1]))
where the algebra should be checked.
Then it's just a -collapse-.
Nick
[email protected]
Marco Petracco
I am looking for some help on the following problem: I
have a set of data in panel format covering 20 years
and I need to run a regression including averages,
growth rates (exponential) and initial values of some
of the variables over 4 years sub-periods.
In the regression I will not need the original yearly
data, just the data corresponding to the sub-periods.
I thought I could manage this using egen cut and then
some looped egen average with by: and drop/replace
commands to get the data in the format I need, but it
seems a bit long and unwieldy.
I was wondering if there was any specific user-defined
command handling this kind of problem or if there is a
quicker solution than the one I devised.
*
* 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/