Announcement

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

  • Generate missing years in a dataset

    I want to generate the next set of years in a dataset when the years are not consecutive.


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int year str20 state str7 fips double(mjnotused mjpastyruse) float(mjadcry mjadpry mjadcr mjadpr mjadany rml)
    2002 "Alabama"  "1"   6546000  601000 0 0 0 0 0 0
    2004 "Alabama"  "1"   6651000  621000 0 0 0 0 0 0
    2006 "Alabama"  "1"    682000  101000 0 0 0 0 0 0
    2008 "Alabama"  "1"   6850000  640000 0 0 0 0 0 0
    2010 "Alabama"  "1"   6942000  697000 0 0 0 0 0 0
    2012 "Alabama"  "1"   6962000  816000 0 0 0 0 0 0
    2014 "Alabama"  "1"   7143000  728000 0 0 0 0 0 0
    2015 "Alabama"  "1"   7194000  680000 0 0 0 0 0 0
    2016 "Alabama"  "1"   7054000  812000 0 0 0 0 0 0
    2017 "Alabama"  "1"   6864000 1039000 0 0 0 0 0 0
    2018 "Alabama"  "1"   6752000 1182000 0 0 0 0 0 0
    2019 "Alabama"  "1"   6553000 1186000 0 0 0 0 0 0
    2002 "Delaware" "10" 17891000 1998000 0 0 0 0 0 0
    2004 "Delaware" "10"  9636000 1099000 0 0 0 0 0 0
    2006 "Delaware" "10"  1221000  153000 0 0 0 0 0 0
    2008 "Delaware" "10"  1256000  157000 0 0 0 0 0 0
    2010 "Delaware" "10"  1255000  196000 0 0 0 0 0 0
    2012 "Delaware" "10"  1292000  205000 0 0 0 0 0 0
    2014 "Delaware" "10"  1335000  204000 0 0 0 0 0 0
    2015 "Delaware" "10"  1345000  209000 0 0 0 0 0 0
    2016 "Delaware" "10"  1327000  241000 0 0 0 0 0 0
    2017 "Delaware" "10"  1302000  282000 0 0 0 0 0 0
    2018 "Delaware" "10"  1306000  294000 0 0 0 0 0 0
    2019 "Delaware" "10"  1292000  276000 0 0 0 0 0 0
    end
    For example, I would like to generate 2003 and 2005 for each state in the panel dataset. How would I go about doing this?

  • #2
    For some reason your fips code is a string variable. It needs to be made numeric to proceed:

    Code:
    destring fips, replace
    
    xtset fips year
    tsfill

    Comment


    • #3
      Clyde Schechter, Danye Medhin is my coworker, and I was helping him with some code developed by both himself and (in part) our mentor; I'd never needed to use tsfill because I just make a balanced panel of my units and then merge all my outcomes/covariates, but they didn't opt to do this in their coding. I figured that there'd be a straightforward way to do this, but I didn't quite know how. Good to know the solution is tsfill. Presumably, the next order of business would be to carryforward the missing strings/policy variables.

      Oh, and welcome to Statalist, Danye.

      Comment


      • #4
        Danye:
        welcome to this forum.
        As an aside to Clyde's helpful reply, I think three issues are relevant here:
        1) once you have years 2003 and 2005 when missing in the original dataset, what's your strategy to plug the unobserved values in?
        2) if your original panel dataset was unbalanced, thsi is not an issue for Stata that can handle both balanced and unbalanced panel dataset with no worries;
        3) making-up datasets may bias your analysis.
        Last edited by Carlo Lazzaro; 14 Mar 2022, 13:04.
        Kind regards,
        Carlo
        (StataNow 18.5)

        Comment


        • #5
          Carlo Lazzaro Danye told me that he planned on using linear interpolation to plug in the missing observations, as per advice from our mentor. The goal is to do a two-way fixed effects difference-in-differences analysis using some policy variables they've constructed from a dataset on marijuana laws/policies.

          Comment


          • #6
            Thanks for the details, Jared Greathouse and all the best for your research with Danye Medhin
            Last edited by Carlo Lazzaro; 14 Mar 2022, 13:05.
            Kind regards,
            Carlo
            (StataNow 18.5)

            Comment


            • #7
              Thank you all for the warm welcome, and for the prompt and helpful responses. My colleague Jared has responded precisely, as it pertains to the ultimate goal here.

              Comment

              Working...
              X