Hi everyone, I am trying to run a synthetic control using the user-written command synth (from SSC). I am on Stata 18.
My data are firm–year panel data (unbalanced now). I first pick one treated firm, get its treatment year, keep only the variables I need, declare the panel, and then call synth. However, I get this message:
time variable not set, use tsset varname ...
even though I have already declared the panel.
Here is a simplified version of my code:
[/CODE]
* 1) choose a treated firm
levelsof Firm_ID if Treated == 1, local(TIDs)
scalar g = real(word("`TIDs'",1)) // first treated firm (numeric)
display "g = " g
* 2) get its event year
quietly summarize Treatment_year if Firm_ID == g, meanonly
scalar t0 = r(mean)
display "t0 = " t0
* 3) check pre-treatment years exist
count if Firm_ID == g & Year < t0
if r(N) == 0 {
di as err "Skip: no pre-treatment years for firm " g
exit
}
* 4) keep relevant vars and declare panel
preserve
keep Firm_ID Year DV
* I used xtset here:
xtset Firm_ID Year
cap which synth
if _rc ssc install synth
synth DV, ///
trunit(g) trperiod(t0) unit(Firm_ID) time(Year) ///
xperiod(`=t0-5'(1)`=t0-1') ///
keep("keep_`=g'.dta") replace
restore
end
[/CODE]
When the synth line runs, I get the error about the time variable not being set.
Any pointers would be appreciated. If needed, I can post more of the actual dataset structure, but the above is the logic I am using.
My data structure:
* Example generated by -dataex-. For more info, type help dataex
clear
input Firm_ID float(Year DV Treated Treatment_year)
"017173" 2002 0 0 .
"017173" 2003 0 0 .
"017173" 2004 0 0 .
"017173" 2005 0 0 .
"017173" 2006 0 0 .
"017173" 2007 0 0 .
"017173" 2008 0 0 .
"017173" 2009 0 0 .
"017173" 2010 0 0 .
"017173" 2011 0 0 .
"017173" 2012 0 0 .
"017173" 2013 0 0 .
"017173" 2014 0 0 .
"017173" 2015 0 0 .
"017173" 2016 0 0 .
"017173" 2017 0 0 .
"017173" 2018 0 0 .
"017173" 2019 0 0 .
"062527" 2002 0 0 .
"062527" 2003 0 0 .
"062527" 2004 0 0 .
"062527" 2005 0 0 .
"062527" 2006 0 0 .
"062527" 2007 0 0 .
"062527" 2008 0 0 .
"062527" 2009 0 0 .
"062527" 2010 0 0 .
"062527" 2011 0 0 .
"062527" 2012 0 0 .
"062527" 2013 0 0 .
"062527" 2014 0 0 .
"062527" 2015 0 0 .
"062527" 2016 0 0 .
"062527" 2017 0 0 .
"062527" 2018 0 0 .
"062527" 2019 0 0 .
"031789" 2002 0 0 2004
"031789" 2003 0 0 2004
"031789" 2004 0 1 2004
"023627" 2013 0 1 2016
"023627" 2014 0 1 2016
"023627" 2015 0 1 2016
"023627" 2016 0 1 2016
"023627" 2017 0 1 2016
"023627" 2018 0 1 2016
"023627" 2019 1 1 2016
"190567" 2009 0 1 2011
"190567" 2010 0 1 2011
"190567" 2011 0 1 2011
"190567" 2012 0 1 2011
end
My data are firm–year panel data (unbalanced now). I first pick one treated firm, get its treatment year, keep only the variables I need, declare the panel, and then call synth. However, I get this message:
time variable not set, use tsset varname ...
even though I have already declared the panel.
Here is a simplified version of my code:
[/CODE]
* 1) choose a treated firm
levelsof Firm_ID if Treated == 1, local(TIDs)
scalar g = real(word("`TIDs'",1)) // first treated firm (numeric)
display "g = " g
* 2) get its event year
quietly summarize Treatment_year if Firm_ID == g, meanonly
scalar t0 = r(mean)
display "t0 = " t0
* 3) check pre-treatment years exist
count if Firm_ID == g & Year < t0
if r(N) == 0 {
di as err "Skip: no pre-treatment years for firm " g
exit
}
* 4) keep relevant vars and declare panel
preserve
keep Firm_ID Year DV
* I used xtset here:
xtset Firm_ID Year
cap which synth
if _rc ssc install synth
synth DV, ///
trunit(g) trperiod(t0) unit(Firm_ID) time(Year) ///
xperiod(`=t0-5'(1)`=t0-1') ///
keep("keep_`=g'.dta") replace
restore
end
[/CODE]
When the synth line runs, I get the error about the time variable not being set.
Any pointers would be appreciated. If needed, I can post more of the actual dataset structure, but the above is the logic I am using.
My data structure:
* Example generated by -dataex-. For more info, type help dataex
clear
input Firm_ID float(Year DV Treated Treatment_year)
"017173" 2002 0 0 .
"017173" 2003 0 0 .
"017173" 2004 0 0 .
"017173" 2005 0 0 .
"017173" 2006 0 0 .
"017173" 2007 0 0 .
"017173" 2008 0 0 .
"017173" 2009 0 0 .
"017173" 2010 0 0 .
"017173" 2011 0 0 .
"017173" 2012 0 0 .
"017173" 2013 0 0 .
"017173" 2014 0 0 .
"017173" 2015 0 0 .
"017173" 2016 0 0 .
"017173" 2017 0 0 .
"017173" 2018 0 0 .
"017173" 2019 0 0 .
"062527" 2002 0 0 .
"062527" 2003 0 0 .
"062527" 2004 0 0 .
"062527" 2005 0 0 .
"062527" 2006 0 0 .
"062527" 2007 0 0 .
"062527" 2008 0 0 .
"062527" 2009 0 0 .
"062527" 2010 0 0 .
"062527" 2011 0 0 .
"062527" 2012 0 0 .
"062527" 2013 0 0 .
"062527" 2014 0 0 .
"062527" 2015 0 0 .
"062527" 2016 0 0 .
"062527" 2017 0 0 .
"062527" 2018 0 0 .
"062527" 2019 0 0 .
"031789" 2002 0 0 2004
"031789" 2003 0 0 2004
"031789" 2004 0 1 2004
"023627" 2013 0 1 2016
"023627" 2014 0 1 2016
"023627" 2015 0 1 2016
"023627" 2016 0 1 2016
"023627" 2017 0 1 2016
"023627" 2018 0 1 2016
"023627" 2019 1 1 2016
"190567" 2009 0 1 2011
"190567" 2010 0 1 2011
"190567" 2011 0 1 2011
"190567" 2012 0 1 2011
end


Comment