Hello! This is my first time asking a question on here, so please excuse me if it is phrased incorrectly, or difficult to understand.
I am working with a panel dataset that has 22 years of observations at the school district/Year level. School districts are identified using the variable "Code," and there is a time variable "Year." I also have a dummy, treatment variable, "Treat," that is 1 when a school district has a wind project within its boundaries, and 0 otherwise. The treatment variable changes from 0 to 1 in the first year that the wind project has come online. However, many districts do not have any wind projects, and thus Treat = 0 across all 22 years of observations. Additionally, wind projects came online at different times in different districts. For example, in district 1 a wind project came online in 2005, while in district 5 a project came online in 2017.
I need to create a new variable "firstYear" that is equal to the first year that a project came online, so for district 1 it would be 2005 and for district 5 it would be 2017, etc. I have tried to use the foreach command to execute this, but I am getting stuck. The code I have tried so far is:
gen firstYear = . *Create firstYear as all missing because I only need these values for districts with wind projects
foreach row of Treat {
replace firstYear = `row' Year if `row' Treat == 1
* Add a column for firstYear and
continue if `row' Treat == 0
}
This is returning a syntax error, which I believe is due to my inexperience with the foreach command, however I am also unsure if the logic I used is correct. I would appreciate help on both fixing the syntax and the logic (if there are other errors).
Additionally, if there is a better way to do this, please let me know!
Thank you!
Related Posts with Identify first time dummy variable = 1 across observations in a panel data set
Categorical dependent variable, how to choose the right modelI would like to Regress the likelihood that collateral are pledged on firm variables, loan contract …
Use Stata program with same name in different directoriesHi, I have a user-written program saved twice in my computer: once inside my system directory's usua…
Calculations of quarterly stock holding data by shareholderDear Statalists, I am dealing with an unbalanced panel data for calculating the share holdings by a…
Interpreting Panel data coefficient estimates where the variable doesnt change across observationsHi, I am looking at the effect of different variables on the recycling rate in England. I have 311 …
How to edit or delete a "signature" that is auto-added to one's post?Many years ago I created a signature (e.g., name & a quotation) that was automatically added to …
Subscribe to:
Post Comments (Atom)
0 Response to Identify first time dummy variable = 1 across observations in a panel data set
Post a Comment