Announcement

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

  • Adding lines of data by an identifier

    Hi,

    I can't seem to get my head around an issue. So basically I have a dataset with a set of identifers named ID. For each year I have a variable named Entry and a variable Exit. Both Entry and Exit provide a year. The objective would be to create a new "Year" variable and add a line of data for each year between Entry and Exit. So for instance if for ID = 1 Entry would be 2000 and exit 2003 and for ID = 2 Entry would be 2018 and Exit 2020; the final result I would need is:
    ID Entry Exit Year
    1 2000 2003 2000
    1 2000 2003 2001
    1 2000 2003 2002
    1 2000 2003 2003
    2 2018 2020 2018
    2 2018 2020 2019
    2 2018 2020 2020
    Could anyone help me with setting this up?

    Kind regards,

    Gianni

  • #2
    Please use dataex as explained in FAQ Advice #12. I could create a data example from your clear question but that's your job. (Insert emoticon of choice.)

    Code:
    clear 
    input ID Entry Exit 
    1 2000 2003
    2 2018 2020
    end 
    
    gen toexpand = Exit - Entry + 1 
    
    expand toexpand 
    
    bysort ID : gen date = Entry + _n - 1 
    
    list, sepby(ID)
    
         +-------------------------------------+
         | ID   Entry   Exit   toexpand   date |
         |-------------------------------------|
      1. |  1    2000   2003          4   2000 |
      2. |  1    2000   2003          4   2001 |
      3. |  1    2000   2003          4   2002 |
      4. |  1    2000   2003          4   2003 |
         |-------------------------------------|
      5. |  2    2018   2020          3   2018 |
      6. |  2    2018   2020          3   2019 |
      7. |  2    2018   2020          3   2020 |
         +-------------------------------------+

    Comment

    Working...
    X