Hi all, I have a panel data question. specifically I'm trying to create a duration variable that counts the number of years elapsed. I know about tsspell but I do not know how to modify it to the specific problem at hand.
so:
I have a panel country-year dataset. I want the duration to reflect the number of years a country exists in the data. this is easy, after sorting by country year this would simply be a counter equal to _n. (by country: gen counter=_n)
However, the counter needs to be reset to 1 every time an event stops occurring.
so say a country enters the dataset in 1950 and the event is a war that takes place 1952 to 1954 this counter would begin in 1950 as 1 but keep on counting even while the war is ongoing and restart at 1 in 1954. if i created _n this is how it would look. however I want the hypothetical counter (duration) variable to be like this: it equals _n until war ceases. Note that if another war to occur down the line, again it should be reset for the country.
I looked at others' do files and people have had the country code A (assume numeric code) taken on a different value any time event occurs and ceases. I've tried this as well but can't get it right.
Any help would be appreciated. otherwise, I'm going to resort to doing this manually on Excel.
country year war _n counter
A 1950 0 1 1
A 1951 0 2 2
A 1952 1 3 3
A 1953 1 4 4
A 1954 0 5 1
A 1955 0 6 2
A 1956 0 7 3
so:
I have a panel country-year dataset. I want the duration to reflect the number of years a country exists in the data. this is easy, after sorting by country year this would simply be a counter equal to _n. (by country: gen counter=_n)
However, the counter needs to be reset to 1 every time an event stops occurring.
so say a country enters the dataset in 1950 and the event is a war that takes place 1952 to 1954 this counter would begin in 1950 as 1 but keep on counting even while the war is ongoing and restart at 1 in 1954. if i created _n this is how it would look. however I want the hypothetical counter (duration) variable to be like this: it equals _n until war ceases. Note that if another war to occur down the line, again it should be reset for the country.
I looked at others' do files and people have had the country code A (assume numeric code) taken on a different value any time event occurs and ceases. I've tried this as well but can't get it right.
Any help would be appreciated. otherwise, I'm going to resort to doing this manually on Excel.
country year war _n counter
A 1950 0 1 1
A 1951 0 2 2
A 1952 1 3 3
A 1953 1 4 4
A 1954 0 5 1
A 1955 0 6 2
A 1956 0 7 3
Comment