Bookmark and Share

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


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


From   "Martin Weiss" <[email protected]>
To   <[email protected]>
Subject   st: AW: lag and lead average comparison from a specific point in panel data
Date   Thu, 11 Mar 2010 09:51:46 +0100

<> 


Here are the means of the lags:

*************
clear*

inp int ID Year byte(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
end

bys ID (Year): gen byte indicator=sum(Z)==0

by ID: egen lagtotal=total(indicator*X2)
by ID: egen lags=total(indicator)
gen lagmean=(lagtotal/lags)*indicator
drop lagtotal lags
li, noo sepby(ID)
*************

What would the lead mean look like for ID 222?


HTH
Martin


-----Ursprüngliche Nachricht-----
Von: [email protected]
[mailto:[email protected]] Im Auftrag von MoZo
Gesendet: Donnerstag, 11. März 2010 06:59
An: [email protected]
Betreff: st: lag and lead average comparison from a specific point in panel
data 

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/


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


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index