Announcement

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

  • Reshaping date into panel

    Dear all,

    I am actually trying to reshape dates into panel data (I will be more explicite below). I however do not find an efficient way to do it.
    I have data at the individual level on unemployment spells, with the following form (06/02/01 means 6th of February 2001)

    id Start End

    1 06/02/01 06/03/01
    2 07/02/01 27/04/10
    3 08/07/09 10/07/09

    Based on this, my objective would be to create a panel with one different line by individual_month-year unemployed. So for the third individual (id=3), I would have just one line indicating she was unemployed at some point in july 2009. For the first individual (id=1), I would have two lines indicating she was unemployed in February and March of 2001. For individual 2, I would have more lines indicating an unemployment spell from february 2001 to April 2010.
    Would you have any efficient way to proceed to such 'reshaping' of the data ?

    Many thanks,
    Alex.

  • #2
    I believe this does what you want.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte id float(start end)
    1 15012 15040
    2 15013 18379
    3 18086 18088
    end
    format %td start
    format %td end
    
    foreach v of varlist start end {
        gen `v'_month = mofd(`v')
    }
    format *_month %tm
    
    expand end_month-start_month + 1
    
    by id, sort: gen month = start_month + _n - 1
    format month %tm
    Notes: 1. This assumes that your variables start and end are Stata internal format date variables, not strings. If they are strings, you must first convert them.

    2. If you are no longer in need of the original start and end variables (or their monthly derivatives start_month and end_month) do feel free to -drop them- at the end.

    In the future, please post example data using the -dataex- command, as I have done above. When you use -dataex- you enable those who want to help you to create a complete and faithful replica of your Stata example with a simple copy/paste observation. Displays of the type you used omit important information (e.g. such as whether start and end are string variables or Stata internal format date variables.) In the absence of that information, responders either make assumptions, which may be wrong and cause the proposed solutions not to work for your data, or skip your post entirely. If you are running Stata version 15.1 -dataex- is part of your official installation. If you are running earlier Stata, you need to install it by running -ssc install dataex-. Either way, run -help dataex- to read the simple instructions for using it. In the future, be sure to use -dataex- every time you show example data.

    Comment

    Working...
    X