Reputation: 197
I'm using Stata and my panel data set is in the following format:
year | ID | var1 | var2 |
---|---|---|---|
2000 | 1 | 100 | . |
2001 | 1 | 200 | 0.2 |
2002 | 1 | 300 | 0.3 |
2000 | 2 | 500 | . |
2001 | 2 | 300 | 0.4 |
2002 | 2 | 400 | 0.1 |
I would like to create an interaction variable between var2 and var1 where the interaction of var2 with var1 is always based on the previous year. In other words, var2 in 2001 should interact with var1 from 2000. Hence, the output should look like this:
year | ID | var1 | var2 | var1Xvar2 |
---|---|---|---|---|
2000 | 1 | 100 | . | . |
2001 | 1 | 200 | 0.2 | 20 |
2002 | 1 | 300 | 0.3 | 60 |
2000 | 2 | 500 | . | . |
2001 | 2 | 300 | 0.4 | 20 |
2002 | 2 | 400 | 0.1 | 30 |
How can I achieve this? Thank you for your help!
Upvotes: 2
Views: 171
Reputation: 1926
Based on Nick Cox's comment, here is an updated answer to your problem. The update uses the lag operator L1.var1
, which is greatly superior relative to var1[_n-1]
when creating lagged variables. It prevents the code from taking observations from a different panel (in this case, different year
.)
clear all
input year ID var1 var2
2000 1 100 .
2001 1 200 0.2
2002 1 300 0.3
2000 2 500 .
2001 2 300 0.4
2002 2 400 0.1
end
// Declare data to be time series
tsset ID year
// Use Lag operator on var1 (for details see: help tsvarlist)
gen wanted = L1.var1 * var2
list
/* +----------------------------------+
| year ID var1 var2 wanted |
|----------------------------------|
1. | 2000 1 100 . . |
2. | 2001 1 200 .2 20 |
3. | 2002 1 300 .3 60 |
4. | 2000 2 500 . . |
5. | 2001 2 300 .4 200 |
|----------------------------------|
6. | 2002 2 400 .1 30 |
+----------------------------------+ */
Upvotes: 2