Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • synth says “time variable not set, use tsset varname ...” even after panel declaration

    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

  • #2
    With your example above, the firm identifier (Firm_ID) is a string variable, so the following should terminate with an error:

    * I used xtset here:
    xtset Firm_ID Year
    If you previously encoded and replaced this variable, provide a complete data example that replicates the error.

    Comment


    • #3
      Originally posted by Andrew Musau View Post
      With your example above, the firm identifier (Firm_ID) is a string variable, so the following should terminate with an error:



      If you previously encoded and replaced this variable, provide a complete data example that replicates the error.
      Thank you, Andrew.

      I have uploaded a new example:

      * Example generated by -dataex-. For more info, type help dataex
      clear
      input long num_gvkey float(Year coverage Treated HFA_year)
      122 2002 0 0 .
      122 2003 0 0 .
      122 2004 0 0 .
      122 2005 0 0 .
      122 2006 0 0 .
      122 2007 0 0 .
      122 2008 0 0 .
      122 2009 0 0 .
      122 2010 0 0 .
      122 2011 0 0 .
      122 2012 0 0 .
      122 2013 0 0 .
      122 2014 0 0 .
      122 2015 0 0 .
      122 2016 0 0 .
      122 2017 0 0 .
      122 2018 0 0 .
      122 2019 0 0 .
      508 2002 0 0 .
      508 2003 0 0 .
      508 2004 0 0 .
      508 2005 0 0 .
      508 2006 0 0 .
      508 2007 0 0 .
      508 2008 0 0 .
      508 2009 0 0 .
      508 2010 0 0 .
      508 2011 0 0 .
      508 2012 0 0 .
      508 2013 0 0 .
      508 2014 0 0 .
      508 2015 0 0 .
      508 2016 0 0 .
      508 2017 0 0 .
      508 2018 0 0 .
      508 2019 0 0 .
      476 2002 0 0 2004
      476 2003 0 0 2004
      476 2004 0 1 2004
      324 2013 0 1 2016
      324 2014 0 1 2016
      324 2015 0 1 2016
      324 2016 0 1 2016
      324 2017 0 1 2016
      324 2018 0 1 2016
      324 2019 1 1 2016
      1018 2009 0 1 2011
      1018 2010 0 1 2011
      1018 2011 0 1 2011
      1018 2012 0 1 2011
      end
      label values num_gvkey num_gvkey
      label def num_gvkey 122 "017173", modify
      label def num_gvkey 324 "023627", modify
      label def num_gvkey 476 "031789", modify
      label def num_gvkey 508 "062527", modify
      label def num_gvkey 1018 "190567", modify
      [/CODE]


      Any suggestion or advice is greatly appreciated

      Comment


      • #4
        I looked at synth on SSC but did not try to run it. But these are the error messages it might produce

        Code:
          |                                                 di as err "panel unit variable missing please use -tsset panelvar timevar" |
          |                                                 di as err "panel time variable missing please use -tsset panelvar timevar" |
          |                                                                di as err "treated unit not found in panelvar - check tr()" |
          |                                                   di as err "at least one control unit not found in panelvar - check co()" |
          |                                                di as err "treated unit appears among control units  - check co() and tr()" |
          |                                                   di as err "`unitnames' does not exist as a (string) variable in dataset" |
          |                                         di as err "`unitnames' varies within units of `pvar' - revise unitnames variable " |
          |                                             di as err "period of treatment is not not found in timevar - check trperiod()" |
          |                                           di as err "at least one time period specified in xperiod() not found in timevar" |
          |                                        di as err "at least one time period specified in mspeperiod() not found in timevar" |
          |                                     di as err "at least one time period specified in resultsperiod() not found in timevar" |
          |                                                       di as err "`dvar' does not exist as a (numeric) variable in dataset" |
          |                                    di as err "not a single variable specified. please supply at least a response variable" |
          |                                                          di as err "`p' does not exist as a (numeric) variable in dataset" |
          |                                                        di as err "`var' does not exist as a (numeric) variable in dataset" |
          |                                      di as err "for predictor `var' some specified periods are not found in panel timevar" |
          |                               di as err "wrong number of custom V weights; please specify one V-weight for each predictor" |
          |                                                      di as err "maximize option shownrtolerance cannot be used with synth" |
          |                          di as err "`ulabel': for at least one unit predictor `cvar' is missing for ALL periods specified" |
          |                  di as err "`ulabel': for at least one unit predictor `cvar'(`tilab' is missing for ALL periods specified" |
          |            di as err "`ulabel': for `checkdimis' of out `cono' units outcome variable `cvar' is missing in `tum' `tlabel'" |
          |                                                                di as err "`ulabel': outcome variable missing for `tlabel'" |
        As it expects (demands!) panel data, I can't follow that or why it would ever produce the error message reported in #1.

        It would be good to be told why that is wrong.

        Comment


        • #5
          Originally posted by Fangying Liu View Post
          I have uploaded a new example:
          The identifier in #3 is named num_gvkey, so it couldn’t have generated the error using the code in #1. What I meant in #2 is that you need to create a reproducible example that can independently replicate the error you’re reporting.

          Comment


          • #6
            Originally posted by Andrew Musau View Post

            The identifier in #3 is named num_gvkey, so it couldn’t have generated the error using the code in #1. What I meant in #2 is that you need to create a reproducible example that can independently replicate the error you’re reporting.
            Thanks a lot, Andrew.

            I just checked my code again, it should be:

            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
            And then I got the message:

            . synth DV, trunit(g) trperiod(t0) unit(Firm_ID) time(Year) xperiod(`=t0-5'(1)`=t0-1') keep("keep_`=g'.
            > dta") replace
            time variable not set, use tsset varname ...
            r(111);

            The data example is like:

            Code:
            * Example generated by -dataex-. For more info, type help dataex
            clear
            input float(Firm_ID Year DV Treated Treatment_year)
             122 2002 0 0    .
             122 2003 0 0    .
             122 2004 0 0    .
             122 2005 0 0    .
             122 2006 0 0    .
             122 2007 0 0    .
             122 2008 0 0    .
             122 2009 0 0    .
             122 2010 0 0    .
             122 2011 0 0    .
             122 2012 0 0    .
             122 2013 0 0    .
             122 2014 0 0    .
             122 2015 0 0    .
             122 2016 0 0    .
             122 2017 0 0    .
             122 2018 0 0    .
             122 2019 0 0    .
             508 2002 0 0    .
             508 2003 0 0    .
             508 2004 0 0    .
             508 2005 0 0    .
             508 2006 0 0    .
             508 2007 0 0    .
             508 2008 0 0    .
             508 2009 0 0    .
             508 2010 0 0    .
             508 2011 0 0    .
             508 2012 0 0    .
             508 2013 0 0    .
             508 2014 0 0    .
             508 2015 0 0    .
             508 2016 0 0    .
             508 2017 0 0    .
             508 2018 0 0    .
             508 2019 0 0    .
             476 2002 0 0 2004
             476 2003 0 0 2004
             476 2004 0 1 2004
             324 2013 0 1 2016
             324 2014 0 1 2016
             324 2015 0 1 2016
             324 2016 0 1 2016
             324 2017 0 1 2016
             324 2018 0 1 2016
             324 2019 1 1 2016
            1018 2009 0 1 2011
            1018 2010 0 1 2011
            1018 2011 0 1 2011
            1018 2012 0 1 2011
            end

            I appreciate for your help and hope this time I post the correct code and example

            Comment


            • #7
              Originally posted by Nick Cox View Post
              I looked at synth on SSC but did not try to run it. But these are the error messages it might produce

              Code:
              | di as err "panel unit variable missing please use -tsset panelvar timevar" |
              | di as err "panel time variable missing please use -tsset panelvar timevar" |
              | di as err "treated unit not found in panelvar - check tr()" |
              | di as err "at least one control unit not found in panelvar - check co()" |
              | di as err "treated unit appears among control units - check co() and tr()" |
              | di as err "`unitnames' does not exist as a (string) variable in dataset" |
              | di as err "`unitnames' varies within units of `pvar' - revise unitnames variable " |
              | di as err "period of treatment is not not found in timevar - check trperiod()" |
              | di as err "at least one time period specified in xperiod() not found in timevar" |
              | di as err "at least one time period specified in mspeperiod() not found in timevar" |
              | di as err "at least one time period specified in resultsperiod() not found in timevar" |
              | di as err "`dvar' does not exist as a (numeric) variable in dataset" |
              | di as err "not a single variable specified. please supply at least a response variable" |
              | di as err "`p' does not exist as a (numeric) variable in dataset" |
              | di as err "`var' does not exist as a (numeric) variable in dataset" |
              | di as err "for predictor `var' some specified periods are not found in panel timevar" |
              | di as err "wrong number of custom V weights; please specify one V-weight for each predictor" |
              | di as err "maximize option shownrtolerance cannot be used with synth" |
              | di as err "`ulabel': for at least one unit predictor `cvar' is missing for ALL periods specified" |
              | di as err "`ulabel': for at least one unit predictor `cvar'(`tilab' is missing for ALL periods specified" |
              | di as err "`ulabel': for `checkdimis' of out `cono' units outcome variable `cvar' is missing in `tum' `tlabel'" |
              | di as err "`ulabel': outcome variable missing for `tlabel'" |
              As it expects (demands!) panel data, I can't follow that or why it would ever produce the error message reported in #1.

              It would be good to be told why that is wrong.
              Thanks a lot Nick.

              I have double check the code and example, could you please look at it:

              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

              The message I got from this code:

              . synth DV, trunit(g) trperiod(t0) unit(Firm_ID) time(Year) xperiod(`=t0-5'(1)`=t0-1') keep("keep_`=g'.
              > dta") replace
              time variable not set, use tsset varname ...
              r(111);




              And the data example:

              Code:
              * Example generated by -dataex-. For more info, type help dataex
              clear
              input float(Firm_ID Year DV Treated Treatment_year)
               122 2002 0 0    .
               122 2003 0 0    .
               122 2004 0 0    .
               122 2005 0 0    .
               122 2006 0 0    .
               122 2007 0 0    .
               122 2008 0 0    .
               122 2009 0 0    .
               122 2010 0 0    .
               122 2011 0 0    .
               122 2012 0 0    .
               122 2013 0 0    .
               122 2014 0 0    .
               122 2015 0 0    .
               122 2016 0 0    .
               122 2017 0 0    .
               122 2018 0 0    .
               122 2019 0 0    .
               508 2002 0 0    .
               508 2003 0 0    .
               508 2004 0 0    .
               508 2005 0 0    .
               508 2006 0 0    .
               508 2007 0 0    .
               508 2008 0 0    .
               508 2009 0 0    .
               508 2010 0 0    .
               508 2011 0 0    .
               508 2012 0 0    .
               508 2013 0 0    .
               508 2014 0 0    .
               508 2015 0 0    .
               508 2016 0 0    .
               508 2017 0 0    .
               508 2018 0 0    .
               508 2019 0 0    .
               476 2002 0 0 2004
               476 2003 0 0 2004
               476 2004 0 1 2004
               324 2013 0 1 2016
               324 2014 0 1 2016
               324 2015 0 1 2016
               324 2016 0 1 2016
               324 2017 0 1 2016
               324 2018 0 1 2016
               324 2019 1 1 2016
              1018 2009 0 1 2011
              1018 2010 0 1 2011
              1018 2011 0 1 2011
              1018 2012 0 1 2011
              end

              Comment


              • #8
                I don't use this command and am not familiar with the technique. I have the same answer as in #4: I can't see this error message in the code, and so am wondering where it comes from. Before you call synth in your code, you could go

                Code:
                set trace on 
                set tracedepth 1
                and then us show where in the code the error message is produced. You may need to bump 1 to 2, or 3, and so on.

                Comment


                • #9
                  Originally posted by Fangying Liu View Post
                  I just checked my code again, it should be:

                  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

                  The data example is like:

                  Code:
                  * Example generated by -dataex-. For more info, type help dataex
                  clear
                  input float(Firm_ID Year DV Treated Treatment_year)
                  122 2002 0 0 .
                  122 2003 0 0 .
                  122 2004 0 0 .
                  122 2005 0 0 .
                  122 2006 0 0 .
                  122 2007 0 0 .
                  122 2008 0 0 .
                  122 2009 0 0 .
                  122 2010 0 0 .
                  122 2011 0 0 .
                  122 2012 0 0 .
                  122 2013 0 0 .
                  122 2014 0 0 .
                  122 2015 0 0 .
                  122 2016 0 0 .
                  122 2017 0 0 .
                  122 2018 0 0 .
                  122 2019 0 0 .
                  508 2002 0 0 .
                  508 2003 0 0 .
                  508 2004 0 0 .
                  508 2005 0 0 .
                  508 2006 0 0 .
                  508 2007 0 0 .
                  508 2008 0 0 .
                  508 2009 0 0 .
                  508 2010 0 0 .
                  508 2011 0 0 .
                  508 2012 0 0 .
                  508 2013 0 0 .
                  508 2014 0 0 .
                  508 2015 0 0 .
                  508 2016 0 0 .
                  508 2017 0 0 .
                  508 2018 0 0 .
                  508 2019 0 0 .
                  476 2002 0 0 2004
                  476 2003 0 0 2004
                  476 2004 0 1 2004
                  324 2013 0 1 2016
                  324 2014 0 1 2016
                  324 2015 0 1 2016
                  324 2016 0 1 2016
                  324 2017 0 1 2016
                  324 2018 0 1 2016
                  324 2019 1 1 2016
                  1018 2009 0 1 2011
                  1018 2010 0 1 2011
                  1018 2011 0 1 2011
                  1018 2012 0 1 2011
                  end
                  ]
                  From the above, the error message that I get is:

                  Code:
                  . synth DV, trunit(g) trperiod(t0) unit(Firm_ID) time(Year) xperiod(`=t0-5'(1)`=t0-1') keep("keep_`=g'.dta") replace
                  trunit() invalid -- invalid number
                  r(121);
                  Are you sure you ran the exact same code with the exact same data that you posted? Please make sure you do. In fact, post the entire log — the data example above isn’t too large to include.

                  Comment


                  • #10
                    Originally posted by Andrew Musau View Post

                    From the above, the error message that I get is:

                    Code:
                    . synth DV, trunit(g) trperiod(t0) unit(Firm_ID) time(Year) xperiod(`=t0-5'(1)`=t0-1') keep("keep_`=g'.dta") replace
                    trunit() invalid -- invalid number
                    r(121);
                    Are you sure you ran the exact same code with the exact same data that you posted? Please make sure you do. In fact, post the entire log — the data example above isn’t too large to include.
                    Dear Andrew,

                    Many thanks for your reply. It is my first time to post the code and thanks a lot for your help

                    I went back and simplified my code for a single treated firm as you suggested. Here is the code I am currently using:

                    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
                    However, when I run the synth command I still get:
                    Code:
                      Synthetic Control Method for Comparative Case Studies  First Step: Data Setupnot a single variable specified. please supply at least a response variable
                    r(198);
                    To make this reproducible, here is an example of my data using -dataex-:

                    Code:
                    • Example generated by -dataex-. For more info, type help dataex clear input float(Firm_ID Year DV Treated Treatment_year) 122 2002 0 0 . 122 2003 0 0 . 122 2004 0 0 . 122 2005 0 0 . 122 2006 0 0 . 122 2007 0 0 . 122 2008 0 0 . 122 2009 0 0 . 122 2010 0 0 . 122 2011 0 0 . 122 2012 0 0 . 122 2013 0 0 . 122 2014 0 0 . 122 2015 0 0 . 122 2016 0 0 . 122 2017 0 0 . 122 2018 0 0 . 122 2019 0 0 . 508 2002 0 0 . 508 2003 0 0 . 508 2004 0 0 . 508 2005 0 0 . 508 2006 0 0 . 508 2007 0 0 . 508 2008 0 0 . 508 2009 0 0 . 508 2010 0 0 . 508 2011 0 0 . 508 2012 0 0 . 508 2013 0 0 . 508 2014 0 0 . 508 2015 0 0 . 508 2016 0 0 . 508 2017 0 0 . 508 2018 0 0 . 508 2019 0 0 . 476 2002 0 0 2004 476 2003 0 0 2004 476 2004 0 1 2004 324 2013 0 1 2016 324 2014 0 1 2016 324 2015 0 1 2016 324 2016 0 1 2016 324 2017 0 1 2016 324 2018 0 1 2016 324 2019 1 1 2016 1018 2009 0 1 2011 1018 2010 0 1 2011 1018 2011 0 1 2011 1018 2012 0 1 2011 end
                    From this example, for instance, firm 476 is treated in 2004, and I would like to use the pre-treatment years as the donor pool.

                    I am probably misunderstanding something about how synth is parsing the command or the data structure, but I cannot see why it keeps saying “not a single variable specified” even though I pass DV as the outcome. By the way, I have checked my variables,

                    Code:
                     describe Firm_ID Year DV
                    
                    Variable      Storage   Display    Value
                        name         type    format    label      Variable label
                    -------------------------------------------------------------------------------------------------------
                    Firm_ID         float   %9.0g                 
                    Year            float   %9.0g                 
                    DV              float   %9.0g
                    If you have time, I would be very grateful for any hint on what I am doing wrong.

                    Comment


                    • #11
                      Originally posted by Nick Cox View Post
                      I don't use this command and am not familiar with the technique. I have the same answer as in #4: I can't see this error message in the code, and so am wondering where it comes from. Before you call synth in your code, you could go

                      Code:
                      set trace on
                      set tracedepth 1
                      and then us show where in the code the error message is produced. You may need to bump 1 to 2, or 3, and so on.
                      Thanks a lot for your asking.

                      I went back and simplified my code for a single treated firm as you suggested. Here is the code I am currently using:

                      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
                      However, when I run the synth command I still get:
                      Code:
                        Synthetic Control Method for Comparative Case Studies
                      
                       First Step: Data Setup
                      
                      
                      not a single variable specified. please supply at least a response variable
                      r(198);
                      To make this reproducible, here is an example of my data using -dataex-:

                      Code:
                      • Example generated by -dataex-. For more info, type help dataex clear input float(Firm_ID Year DV Treated Treatment_year) 122 2002 0 0 . 122 2003 0 0 . 122 2004 0 0 . 122 2005 0 0 . 122 2006 0 0 . 122 2007 0 0 . 122 2008 0 0 . 122 2009 0 0 . 122 2010 0 0 . 122 2011 0 0 . 122 2012 0 0 . 122 2013 0 0 . 122 2014 0 0 . 122 2015 0 0 . 122 2016 0 0 . 122 2017 0 0 . 122 2018 0 0 . 122 2019 0 0 . 508 2002 0 0 . 508 2003 0 0 . 508 2004 0 0 . 508 2005 0 0 . 508 2006 0 0 . 508 2007 0 0 . 508 2008 0 0 . 508 2009 0 0 . 508 2010 0 0 . 508 2011 0 0 . 508 2012 0 0 . 508 2013 0 0 . 508 2014 0 0 . 508 2015 0 0 . 508 2016 0 0 . 508 2017 0 0 . 508 2018 0 0 . 508 2019 0 0 . 476 2002 0 0 2004 476 2003 0 0 2004 476 2004 0 1 2004 324 2013 0 1 2016 324 2014 0 1 2016 324 2015 0 1 2016 324 2016 0 1 2016 324 2017 0 1 2016 324 2018 0 1 2016 324 2019 1 1 2016 1018 2009 0 1 2011 1018 2010 0 1 2011 1018 2011 0 1 2011 1018 2012 0 1 2011 end
                      From this example, for instance, firm 476 is treated in 2004, and I would like to use the pre-treatment years as the donor pool.

                      I am probably misunderstanding something about how synth is parsing the command or the data structure, but I cannot see why it keeps saying “not a single variable specified” even though I pass DV as the outcome. By the way, I have checked my variables,

                      Code:
                       describe Firm_ID Year DV
                      
                      Variable      Storage   Display    Value
                          name         type    format    label      Variable label
                      -------------------------------------------------------------------------------------------------------
                      Firm_ID         float   %9.0g                
                      Year            float   %9.0g                
                      DV              float   %9.0g
                      If you have time, I would be very grateful for any hint on what I am doing wrong.

                      Comment


                      • #12
                        Sorry; I can’t help further. The advice to set a trace still stands.

                        Comment


                        • #13
                          Originally posted by Nick Cox View Post
                          Sorry; I can’t help further. The advice to set a trace still stands.
                          Thanks Nick, I just tried the trace. And I got this:

                          Code:
                          . set trace on
                          
                          . set tracedepth 1
                          
                          . 
                          . 
                          . 
                          . * 1) choose a treated firm
                          . levelsof Firm_ID if Treated == 1, local(TIDs)
                            --------------------------------------------------------------------------- begin levelsof ---
                            - version 18.0
                            - syntax varname [if] [in] [, Separate(str) MISSing Local(name local) Clean MATROW(name) MATCE
                          > LL(name) HEXadecimal ]
                            - tempname frm
                            - local useframe 0
                            - if (`"`if'`in'"' != "") {
                            = if (`"if Treated == 1"' != "") {
                            - local hold_frame `c(frame)'
                            = local hold_frame default
                            - frame put `varlist' `if' `in', into(`frm')
                            = frame put Firm_ID if Treated == 1 , into(__000000)
                            - frame change `frm'
                            = frame change __000000
                            - local useframe 1
                            - }
                            - if (`"`separate'"' == "") {
                            = if (`""' == "") {
                            - local separate " "
                            - }
                            - local typ : type `varlist'
                            = local typ : type Firm_ID
                            - if (substr("`typ'", 1, 3) == "str") {
                            = if (substr("float", 1, 3) == "str") {
                              NoHexadecimal `hexadecimal'
                              NoMatrow `matrow'
                              }
                            - mata: st_rclear()
                            - capture {
                            - mata:levelsof_wrk("`varlist'", "`typ'", "`missing'", "`matcell'", "`matrow'", "`clean'", `"`
                          > separate'"', "`hexadecimal'")
                            = mata:levelsof_wrk("Firm_ID", "float", "", "", "", "", `" "', "")
                            - }
                            - if _rc > 0 {
                              mata:clear_levelsof()
                              if "`useframe'" == "1" {
                              frame change `hold_frame'
                              }
                              di as error "failed to compute"
                              exit 198
                              }
                            - mata:clear_levelsof()
                            - if "`useframe'" == "1" {
                            = if "1" == "1" {
                            - frame change `hold_frame'
                            = frame change default
                            - }
                            - if ("`local'" != "") {
                            = if ("TIDs" != "") {
                            - c_local `local' `"`r(levels)'"'
                            = c_local TIDs `"12 17 19 22 33 45 47 54 63 64 68 69 73 74 78 79 81 83 84 89 93 99 101 102 103
                          >  104 109 112 117 123 130 134 135 140 145 146 147 157 158 169 171 175 178 179 185 192 194 196 1
                          > 97 198 202 204 208 211 212 216 217 218 220 221 222 229 235 236 241 243 245 250 262 264 265 266
                          >  268 270 280 283 284 286 294 295 299 310 315 316 321 322 324 325 326 331 333 337 338 339 340 3
                          > 44 346 347 350 355 359 360 363 365 366 367 368 369 370 373 374 375 377 381 386 389 390 391 395
                          >  396 402 408 410 412 418 419 424 426 427 430 433 435 437 438 440 441 442 445 446 447 449 452 4
                          > 53 454 456 458 463 465 466 468 473 474 475 476 478 480 488 492 494 498 499 501 505 507 509 511
                          >  515 517 519 522 524 526 527 536 539 541 544 548 549 559 567 573 574 576 577 584 585 589 596 6
                          > 02 603 607 614 616 617 618 628 635 636 638 642 646 654 655 656 657 658 659 660 662 663 664 669
                          >  673 692 694 696 698 699 700 701 707 709 710 711 712 715 716 723 724 734 736 740 743 746 747 7
                          > 50 754 755 756 757 759 761 764 765 766 770 773 774 776 777 783 790 797 801 802 803 805 807 808
                          >  811 813 815 818 822 825 826 828 829 831 834 835 836 839 840 842 843 844 847 848 853 860 863 8
                          > 71 875 877 882 886 891 893 895 896 899 901 903 906 908 909 910 911 912 913 916 917 919 922 925
                          >  926 927 930 933 935 936 937 938 940 943 944 945 946 947 954 955 958 960 962 963 970 972 979 9
                          > 80 981 984 988 990 993 994 995 998 999 1001 1002 1003 1004 1007 1008 1009 1011 1015 1016 1017 
                          > 1018 1021 1022 1027 1030 1032 1035 1036 1038 1042"'
                            - }
                            - di as text `"`r(levels)'"'
                            = di as text `"12 17 19 22 33 45 47 54 63 64 68 69 73 74 78 79 81 83 84 89 93 99 101 102 103 1
                          > 04 109 112 117 123 130 134 135 140 145 146 147 157 158 169 171 175 178 179 185 192 194 196 197
                          >  198 202 204 208 211 212 216 217 218 220 221 222 229 235 236 241 243 245 250 262 264 265 266 2
                          > 68 270 280 283 284 286 294 295 299 310 315 316 321 322 324 325 326 331 333 337 338 339 340 344
                          >  346 347 350 355 359 360 363 365 366 367 368 369 370 373 374 375 377 381 386 389 390 391 395 3
                          > 96 402 408 410 412 418 419 424 426 427 430 433 435 437 438 440 441 442 445 446 447 449 452 453
                          >  454 456 458 463 465 466 468 473 474 475 476 478 480 488 492 494 498 499 501 505 507 509 511 5
                          > 15 517 519 522 524 526 527 536 539 541 544 548 549 559 567 573 574 576 577 584 585 589 596 602
                          >  603 607 614 616 617 618 628 635 636 638 642 646 654 655 656 657 658 659 660 662 663 664 669 6
                          > 73 692 694 696 698 699 700 701 707 709 710 711 712 715 716 723 724 734 736 740 743 746 747 750
                          >  754 755 756 757 759 761 764 765 766 770 773 774 776 777 783 790 797 801 802 803 805 807 808 8
                          > 11 813 815 818 822 825 826 828 829 831 834 835 836 839 840 842 843 844 847 848 853 860 863 871
                          >  875 877 882 886 891 893 895 896 899 901 903 906 908 909 910 911 912 913 916 917 919 922 925 9
                          > 26 927 930 933 935 936 937 938 940 943 944 945 946 947 954 955 958 960 962 963 970 972 979 980
                          >  981 984 988 990 993 994 995 998 999 1001 1002 1003 1004 1007 1008 1009 1011 1015 1016 1017 10
                          > 18 1021 1022 1027 1030 1032 1035 1036 1038 1042"'
                          12 17 19 22 33 45 47 54 63 64 68 69 73 74 78 79 81 83 84 89 93 99 101 102 103 104 109 112 117 12
                          > 3 130 134 135 140 145 146 147 157 158 169 171 175 178 179 185 192 194 196 197 198 202 204 208 
                          > 211 212 216 217 218 220 221 222 229 235 236 241 243 245 250 262 264 265 266 268 270 280 283 28
                          > 4 286 294 295 299 310 315 316 321 322 324 325 326 331 333 337 338 339 340 344 346 347 350 355 
                          > 359 360 363 365 366 367 368 369 370 373 374 375 377 381 386 389 390 391 395 396 402 408 410 41
                          > 2 418 419 424 426 427 430 433 435 437 438 440 441 442 445 446 447 449 452 453 454 456 458 463 
                          > 465 466 468 473 474 475 476 478 480 488 492 494 498 499 501 505 507 509 511 515 517 519 522 52
                          > 4 526 527 536 539 541 544 548 549 559 567 573 574 576 577 584 585 589 596 602 603 607 614 616 
                          > 617 618 628 635 636 638 642 646 654 655 656 657 658 659 660 662 663 664 669 673 692 694 696 69
                          > 8 699 700 701 707 709 710 711 712 715 716 723 724 734 736 740 743 746 747 750 754 755 756 757 
                          > 759 761 764 765 766 770 773 774 776 777 783 790 797 801 802 803 805 807 808 811 813 815 818 82
                          > 2 825 826 828 829 831 834 835 836 839 840 842 843 844 847 848 853 860 863 871 875 877 882 886 
                          > 891 893 895 896 899 901 903 906 908 909 910 911 912 913 916 917 919 922 925 926 927 930 933 93
                          > 5 936 937 938 940 943 944 945 946 947 954 955 958 960 962 963 970 972 979 980 981 984 988 990 
                          > 993 994 995 998 999 1001 1002 1003 1004 1007 1008 1009 1011 1015 1016 1017 1018 1021 1022 1027
                          >  1030 1032 1035 1036 1038 1042
                            ----------------------------------------------------------------------------- end levelsof ---
                          
                          . scalar g = real(word("`TIDs'",1)) // first treated firm (numeric)
                          
                          . display "g = " g
                          g = 12
                          
                          . 
                          . * 2) get its event year
                          . quietly summarize Treatment_year if Firm_ID == g, meanonly
                          
                          . scalar t0 = r(mean)
                          
                          . display "t0 = " t0
                          t0 = 2016
                          
                          . 
                          . * 3) check pre-treatment years exist
                          . count if Firm_ID == g & Year < t0
                            14
                          
                          . 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
                            ------------------------------------------------------------------------------ begin xtset ---
                            - version 9.2
                            - syntax [varlist(numeric max=2 default=none)] [, MI *]
                            - if ("`mi'"=="") {
                            = if (""=="") {
                            - u_mi_not_mi_set xtset
                            - }
                            - else {
                              u_mi_check_setvars settime xtset `varlist'
                              }
                            - local nvars : list sizeof varlist
                            - if (`nvars' == 2) {
                            = if (2 == 2) {
                            - tsset `0'
                            = tsset Firm_ID Year
                          
                          Panel variable: Firm_ID (unbalanced)
                           Time variable: Year, 2002 to 2019, but with gaps
                                   Delta: 1 unit
                            - }
                            - else if (`nvars' == 1) {
                            = else if (2 == 1) {
                              PanelSet `0'
                              }
                            - else {
                              syntax [, clear MI PANELNAME(passthru) DISPLAYINDENT(passthru) NOBLANK]
                              if ("`clear'" != "") {
                              syntax , clear [ MI ]
                              tsset, clear `mi'
                              }
                              else {
                              if (`"`_dta[_TStvar]'"' == "") {
                              PanelSet, `mi' `panelname' `displayindent' `noblank'
                              }
                              else {
                              tsset, `mi' `panelname' `displayindent' `noblank'
                              }
                              }
                              }
                            -------------------------------------------------------------------------------- end xtset ---
                          
                          . 
                          . 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
                            ------------------------------------------------------------------------------ begin synth ---
                            - version 9.2
                            - preserve
                            - qui tsset
                            - local tvar `r(timevar)'
                            = local tvar Year
                            - local pvar "`r(panelvar)'"
                            = local pvar "Firm_ID"
                            - if "`tvar'" == "" {
                            = if "Year" == "" {
                              di as err "panel unit variable missing please use -tsset panelvar timevar"
                              exit 198
                              }
                            - if "`pvar'" == "" {
                            = if "Firm_ID" == "" {
                              di as err "panel time variable missing please use -tsset panelvar timevar"
                              exit 198
                              }
                            - syntax anything , TRUnit(numlist min=1 max=1 int sort) TRPeriod(numlist min=1 max=1 int sort
                          > ) [ COUnit(numlist min=2 int sort) xperiod(numlist min=1 >=0 int sort) mspeperiod(numlist min=
                          > 1 >=0 int sort) resultsperiod(numlist min=1 >=0 int sort) unitnames(varlist max=1 string) FIGu
                          > re Keep(string) REPlace customV(numlist) margin(real 0.005) maxiter(integer 1000) sigf(integer
                          >  12) bound(integer 10) nested allopt * ]
                            - tempvar Xco Xcotemp Xtr Xtrtemp Zco Ztr Yco Ytr subsample misscheck conlabel
                            - qui levelsof `pvar',local(levp)
                            = qui levelsof Firm_ID,local(levp)
                            - loc checkinput: list trunit in levp
                            - if `checkinput' == 0 {
                            = if 1 == 0 {
                              di as err "treated unit not found in panelvar - check tr()"
                              exit 198
                              }
                            - if "`counit'" == "" {
                            = if "" == "" {
                            - local counit : subinstr local levp "`trunit'" " ", all word
                            = local counit : subinstr local levp "12" " ", all word
                            - }
                            - else {
                              loc checkinput: list counit in levp
                              if `checkinput' == 0 {
                              di as err "at least one control unit not found in panelvar - check co()"
                              exit 198
                              }
                              loc checkinput: list trunit in counit
                              if `checkinput' == 1 {
                              di as err "treated unit appears among control units  - check co() and tr()"
                              exit 198
                              }
                              }
                            - local clab: value label `pvar'
                            = local clab: value label Firm_ID
                            - if "`unitnames'" != "" {
                            = if "" != "" {
                              capture confirm string var `unitnames'
                              if _rc {
                              di as err "`unitnames' does not exist as a (string) variable in dataset"
                              exit 198
                              }
                              tempvar pcheck
                              qui egen `pcheck' = sd(`pvar') , by(`unitnames')
                              qui sum `pcheck'
                              if r(sd) != 0 {
                              di as err "`unitnames' varies within units of `pvar' - revise unitnames variable "
                              exit 198
                              }
                              local clab "`pvar'"
                              tempvar index
                              gen `index' = _n
                              foreach i in `levp' {
                              qui su `index' if `pvar' == `i', meanonly
                              local label = `unitnames'[`r(max)']
                              local value = `pvar'[`r(max)']
                              qui label define `clab' `value' `"`label'"', modify
                              }
                              label value `pvar' `clab'
                              }
                            - if "`clab'" != "" {
                            = if "" != "" {
                              local tlab: label `clab' `trunit' , strict
                              foreach i in `counit' {
                              local label : label `clab' `i'
                              local colabels `"`colabels', `label'"'
                              }
                              local colabels : list clean colabels
                              local colabels : subinstr local colabels "," ""
                              local colabels : list clean colabels
                              }
                            - di as txt "{hline}"
                          ------------------------------------------------------------------------------------------------
                            - di as res "Synthetic Control Method for Comparative Case Studies"
                          Synthetic Control Method for Comparative Case Studies
                            - di as txt "{hline}"
                          ------------------------------------------------------------------------------------------------
                            - di as txt ""
                          
                            - di as res "First Step: Data Setup"
                          First Step: Data Setup
                            - di as txt "{hline}"
                          ------------------------------------------------------------------------------------------------
                            - qui levelsof `tvar', local(levt)
                            = qui levelsof Year, local(levt)
                            - loc checkinput: list trperiod in levt
                            - if `checkinput' == 0 {
                            = if 1 == 0 {
                              di as err "period of treatment is not not found in timevar - check trperiod()"
                              exit 198
                              }
                            - qui levelsof `tvar' if `tvar' < `trperiod' , local(preperiod)
                            = qui levelsof Year if Year < 2016 , local(preperiod)
                            - if "`xperiod'" == "" {
                            = if "2011 2012 2013 2014 2015" == "" {
                              numlist "`preperiod'" , min(1) integer sort
                              local xperiod "`r(numlist)'"
                              }
                            - else {
                            - loc checkinput: list xperiod in levt
                            - if `checkinput' == 0 {
                            = if 1 == 0 {
                              di as err "at least one time period specified in xperiod() not found in timevar"
                              exit 198
                              }
                            - }
                            - if "`mspeperiod'" == "" {
                            = if "" == "" {
                            - numlist "`preperiod'" , min(1) integer sort
                            = numlist "2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015" , min(1) int
                          > eger sort
                            - local mspeperiod "`r(numlist)'"
                            = local mspeperiod "2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015"
                            - }
                            - else {
                              loc checkinput: list mspeperiod in levt
                              if `checkinput' == 0 {
                              di as err "at least one time period specified in mspeperiod() not found in timevar"
                              exit 198
                              }
                              }
                            - if "`resultsperiod'" == "" {
                            = if "" == "" {
                            - numlist "`levt'" , min(1) integer sort
                            = numlist "2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 201
                          > 8 2019" , min(1) integer sort
                            - local resultsperiod "`r(numlist)'"
                            = local resultsperiod "2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2
                          > 016 2017 2018 2019"
                            - }
                            - else {
                              loc checkinput: list resultsperiod in levt
                              if `checkinput' == 0 {
                              di as err "at least one time period specified in resultsperiod() not found in timevar"
                              exit 198
                              }
                              }
                            - gettoken dvar anything: anything
                            - capture confirm numeric var `dvar'
                            = capture confirm numeric var DV
                            - if _rc {
                              di as err "`dvar' does not exist as a (numeric) variable in dataset"
                              exit 198
                              }
                            - if "`anything'" == "" {
                            = if "" == "" {
                            - di as err "not a single variable specified. please supply at least a response variable"
                          not a single variable specified. please supply at least a response variable
                            - exit 198
                              }
                            -------------------------------------------------------------------------------- end synth ---
                          r(198);
                          
                          end of do-file
                          
                          r(198);

                          Comment


                          • #14
                            Originally posted by Fangying Liu View Post

                            However, when I run the synth command I still get: [code]

                            Synthetic Control Method for Comparative Case Studies

                            First Step: Data Setup


                            not a single variable specified. please supply at least a response variable
                            r(198);
                            You have a dependent variable (named DV) but no predictor (independent) variable(s). You cannot estimate a model with no predictor(s). The command syntax is:

                            Syntax

                            synth depvar predictorvars , trunit(#) trperiod(#) [ counit(numlist) xperiod(numlist) mspeperiod() resultsperiod() nested allopt unitnames(varname) figure
                            keep(file) customV(numlist) optsettings ]
                            See examples of the required data structure in:

                            Code:
                            help synth

                            Comment


                            • #15
                              Originally posted by Andrew Musau View Post

                              You have a dependent variable (named DV) but no predictor (independent) variable(s). You cannot estimate a model with no predictor(s). The command syntax is:



                              See examples of the required data structure in:

                              Code:
                              help synth
                              Thanks a lot, Andrew.
                              I have figured this error already.
                              Have a nice day

                              Comment

                              Working...
                              X