Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.

# st: lag and lead average comparison from a specific point in panel data

 From "MoZo" To Subject st: lag and lead average comparison from a specific point in panel data Date Thu, 11 Mar 2010 00:58:55 -0500

```I have a panel dataset which is set up as follow:

ID	Year	X1	X2	Z
111   1990  89 	21	0
111	1991	29	.	0
111	1992	87	76	1
111	1993	87	98	0
111	1994	45	88	0
222	1990	98	88	0
222	1991	98	44	0
222	1992	45	71	1
.
.
.
.
999	1990	76	.	0
999	1991	54	43	0
999	1992	65	32	1
999	1993	65	87	0
999	1994	76	32	0

I am trying to compare the average of X2 in last 2 years and that in lead 2
years from Z=1. In order to do that I create two new variables: one for the
average of 2 lagged years and another for the average of 2 lead years.

I am calculating an average of X2 in last 2 years (2 years lagged) prior to
an event Z (Z=1) in each panel.
This is what I did.

. sort ID Year

. by ID: egen avgX2lag2yr=mean(l(1/2).X2) if Z==1

But I keep getting error as " unknown function l()"
Obviously l(1/2).X2 was not recognized here.

I tried to as follow as well:

by ccode: egen avgX2lag2yr =(l.X2+l2.X2)/2 if Z==1

But in some cases, X2 are missing. So I can't just put a constant number to
average the sum without risking inaccurate result.

Thank you in advance for help.

Mike

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