Announcement

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

  • Expand panel data with condition

    Dear Stata users

    I am working with an unbalanced panel where dnr202115 is individual id, dnr2019129_cfar is office id, redovar is year, manfran is month from and mantill is month upto. I want to expand the yearly data into month-year data for each individual id using year manfran and mantill. For example, for invidual id 100000508 (first row), I would like to expand the panel to 2006m4-2006m10. here is my data:

    ----------------------- copy starting from the next line -----------------------
    [CODE]
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str9 dnr202115 long dnr2019129_cfar int redovar byte(manfran mantill)
    "100000508" 219763 2006 4 10
    "100000552" 2395641 2016 1 12
    "100000552" 2395641 2017 1 12
    "100000552" 2395641 2018 1 12
    "100000711" 219698 2015 5 12
    "100000711" 219698 2016 1 12
    "100000711" 219698 2017 1 5
    "100000711" 219698 2018 3 3
    "100000726" 219682 2014 6 12
    "100000726" 219682 2015 1 12
    "100000726" 219682 2016 1 8
    "100000933" 219532 2012 8 12
    "100000933" 219532 2013 1 12
    "100000933" 219532 2014 1 12
    "100000933" 219533 2015 1 12
    "100000933" 219533 2016 1 12
    "100000933" 219533 2017 1 12
    "100000933" 219533 2018 1 12
    "100001438" 681356 2009 3 12
    "100001438" 681356 2010 1 12
    "100001438" 681356 2011 1 12


    Would appreciate any help in this. I tried the following but I dont know how to reshape it as long format:

    forvalues i=2005/2018 {
    forvalues j=1/12 {
    gen emp`i'm`j' = inrange(ym(`i',`j'),ym(redovar, manfran), ym(redovar,mantil))
    }
    }


    Here emp`i'm`j'denotes the employment status dummy for a specific month.

    Thanks,
    Zariab Hossain
    Uppsala University

  • #2
    Starting from your original data:

    Code:
    gen from = ym(redovar, manfran)
    format from %tm
    gen to = ym(redovar, mantill)
    format to %tm
    
    expand to - from + 1
    by dnr202115 dnr2019129 from, sort: gen mdate = from + _n - 1
    format mdate %tm
    isid dnr202115 dnr2019129_cfar mdate, sort
    
    drop redovar manfran mantill from to
    Last edited by Clyde Schechter; 05 Nov 2023, 09:52.

    Comment


    • #3
      Here's another way to do it:

      Code:
      gen toexpand = mantil - manfran + 1 
      expand toexpand 
      bysort dnr202115 dnr2019129_cfar redovar: replace manfran = manfran + _n - 1 
      gen mdate = ym(redovar, manfran)
      format mdate %tm

      Comment

      Working...
      X