Announcement

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

  • fill in missing years b/w start year and end year

    I am trying to fill in intermediate years when I have start and end years.

    For example, consider the following dataset that includes firmid and the starting and ending years that the firm experienced a problem:

    Code:
    clear*
    input firmid startyear endyear
    11 2000 2002
    11 2002 2003
    12 2001 2003
    12 2004 2004
    13 2004 2005
    13 2005 2005
    end
    I would like the output to be a firm-year panel with a column that indicated if that particular firm-year had a problem or not (where problem start and end dates came from the above data sample). The difficulty I have is when there is more than 1 year between the start and end date. I am not sure how to fill those years in. The output would look like this (there is a firm-year panel for 6 years for each of the 3 unique firms and a problem variable in the output set is equal to 1 if there was a problem in that particular year and zero otherwise):

    Code:
    clear*
    input firmid year problem
    11 2000 1
    11 2001 1
    11 2002 1
    11 2003 1
    11 2004 0
    11 2005 0
    12 2000 0
    12 2001 1
    12 2002 1
    12 2003 1
    12 2004 1
    12 2005 0
    13 2000 0
    13 2001 0
    13 2002 0
    13 2003 0
    13 2004 1
    13 2005 1
    end
    Thank you in advance.
    Last edited by Kyle Smith; 11 Mar 2022, 17:26.

  • #2
    Code:
    preserve
    
    keep firmid
    duplicates drop
    expand 6
    by firmid, sort: gen year = 1999 + _n
    tempfile rectangular
    save `rectangular'
    
    restore
    joinby firmid using `rectangular'
    gen byte problem = inrange(year, startyear, endyear)
    collapse (max) problem, by(firmid year)

    Comment


    • #3
      Clyde Schechter : that is the second time you've helped me in the last cpl days. Thanks alot. Wonderful and efficient coding. Have a great weekend.

      Comment

      Working...
      X