Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# Re: st: Generate a more complex lagged variable

 From Nick Cox <[email protected]> To [email protected] Subject Re: st: Generate a more complex lagged variable Date Tue, 1 Feb 2011 16:48:53 +0000

```You are referring to your code and not showing it to us, so quite what
you did is unclear.
Perhaps you tried something like

gen H[2] = P[1]

and as you say Stata doesn't like that.

It seems that that you have panel data. Setting that aside you would have

gen H = P[1] in 2
replace H = a * H[_n-1]  + (1 - a) * P[_n-1] in 3/L

In a panel context that would need to be something like

bysort firmid (time) : gen H = P[1] if _n == 2
by firmid : replace H = a * H[_n-1]  + (1 - a) * P[_n-1] if _n > 3

A basic tutorial on -by:- and subscripts is accessible at

SJ-2-1  pr0004  . . . . . . . . . . Speaking Stata:  How to move step by: step
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
Q1/02   SJ 2(1):86--102                                  (no commands)
explains the use of the by varlist : construct to tackle
a variety of problems with group structure, ranging from
simple calculations for each of several groups to more
advanced manipulations that use the built-in _n and _N

There would be some gain and no loss in using L.H and L.P after a
Alternatively, in Mata subscripts behave more like what you
(reasonably) seem to expect.

Nick

On Tue, Feb 1, 2011 at 4:30 PM, Ari Dothan <[email protected]> wrote:
> Hi all,
> In the formula H_t=a*H_t-1+(1-a)*P_t-1, P is an existing variable per firm/year.
> Assuming that H[2]=P[1], and taking a=0.1: How can I build the column
> of H_t by firm?
> Since it understands [x] as weights, Stata indicates that "weights are
> not allowed", so my problem becomes the specific calculation of each
> observation in the H_t column.
*
*   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/
```