My data is in the following format:
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
I would like to create an interaction variable between var1
and var2
where var1
always takes the value of the first year where I have data (in this case year
2000 as the base year).
year | ID | var1 | var2 | var1xvar2 |
---|---|---|---|---|
2000 | 1 | 100 | . | . |
2001 | 1 | 200 | 0.2 | 20 |
2002 | 1 | 300 | 0.3 | 30 |
2000 | 2 | 500 | . | . |
2001 | 2 | 300 | 0.4 | 200 |
2002 | 2 | 400 | 0.1 | 50 |
How can I achieve this?
The following works for me:
bysort ID (year): gen wanted = var1[1] * var2
list, sepby(ID)
+----------------------------------+
| year ID var1 var2 wanted |
|----------------------------------|
1. | 2000 1 100 . . |
2. | 2001 1 200 .2 20 |
3. | 2002 1 300 .3 30 |
|----------------------------------|
4. | 2000 2 500 . . |
5. | 2001 2 300 .4 200 |
6. | 2002 2 400 .1 50 |
+----------------------------------+
The [1] refers to the first observation in each ID panel, which in this case is the lowest value of year because the dataset is sorted by ID and year.