Hi,
I am working with election data and have two datasets (concerned variables explained below):
Dataset 1: Election data
I have the following variables
'e_year' - election year
'l_year' - last election year
'year_b' - years since last election (ranges from 1-5)
Dataset 2: Variables like population, literacy etc. I have created lags of these variable for past 5 years using a for loop
foreach v of varlist farea -ftotal {
replace `v' = 0 if missing(`v')
by state: gen lag1_`v' = `v'[_n-1]
by state: gen lag2_`v' = `v'[_n-2]
by state: gen lag3_`v' = `v'[_n-3]
by state: gen lag4_`v' = `v'[_n-4]
by state: gen lag5_`v' = `v'[_n-5]
}
What I need: When I combine these datasets, I need the lags only for year_b i.e. if election happened 3 years ago then 3 lags, if it happened a year ago then only one lag and so on.
I have a very long way of implementing this. At the moment, I combine the datasets using state and e_year. Then if year_b is 1, I drop each lag for each variable. I tried dropping them using 'lag2_*' but (a) it doesn't work (b) this is still not the efficient way of doing it as I will need to repeat this for each lag
I am certain a better way to implement this exists. I would appreciate if someone can help me with it.
I would want to make the changes after implementing (as I have need my dataset 2 to be combines with another file too)
Thank you
Anna
Related Posts with Efficiently creating lags only for certain years
Panel dataset of Matched sample using rangejoinDear Statalisters, I want to see the effect of a law which came into effect on 1975 on the lnpatent…
Loading do-file editor and other windows on startupI tend to load the do-file editor right after I start Stata. I often also open the data editor and v…
Date precisionIf I do this: . gen double d=clock("20200101 01:01:01", "YMD hms") . format %tC d . browse then …
How to delete observations in panel dataHi all, I have a panel data set where I want to conditionally delete observations. In my case, I am…
Advice regarding Ramsey RESET test resultHello, I'm running an OLS regression with around 2000 observations and a few variables - most of th…
Subscribe to:
Post Comments (Atom)
0 Response to Efficiently creating lags only for certain years
Post a Comment