Reputation: 2405
I have a dataset that looks like this
State Year Policy other_variables
a 2000 0 18
a 2000 0 19
.
.
.
a 2001 1 86
a 2001 1 23
The poicy value is constant within each state and year. But it changes for different state and different year. The other_variables are different for each observation.
I want to generate lags of the policy value for each state. However, I cannot use xtset state year
and then use the L
operator. There are repeated values within each state year combination. I know that collapsing the dataset, generate lag variables and then merge back to the dataset would work. My question is is there an easy way to do this operation?
Upvotes: 1
Views: 726
Reputation: 37208
This may help:
clear
input str1 State Year Policy
a 2000 0
a 2000 0
a 2001 1
a 2001 1
end
bysort State (Year) : gen diff = Policy - Policy[_n-1] if Year == Year[_n-1] + 1
by State Year: replace diff = diff[_n-1] if missing(diff)
list, sepby(State Year)
+------------------------------+
| State Year Policy diff |
|------------------------------|
1. | a 2000 0 . |
2. | a 2000 0 . |
|------------------------------|
3. | a 2001 1 1 |
4. | a 2001 1 1 |
+------------------------------+
Upvotes: 1