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
longitudinal analysis with measurements taken at different timesdear stata users, I'm dealing with a longitudinal dataset with measurements taken in different time…
Should I spend more time on improve my programming ability?I am a doctoral student.My major is applied econonmics.My main job is to analyze agricultural proble…
classification of firms into groups based on some criterion Code: input str1 firm int year float(profits) "a" 2000 . "a" 2001 . "a" 2002 . "…
Problem in duplicate case using HIES 2018-19My data has duplicate cases, I want to remove duplicate case if they report '2' as response in varia…
Retaining the 5 closest matches when matching firms using -rangejoin-Hi I have a matched panel data set of treated and control firms, matched on industry (sic2), and 50…
Subscribe to:
Post Comments (Atom)
0 Response to Identify first time dummy variable = 1 across observations in a panel data set
Post a Comment