Announcement

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

  • Number of months employed from spell data

    My data is in spell format, see example below. Spells can be both employment and unemployment spells and can be overlapping, but this shouldn't be too much of a problem for this question.

    Code:
    input spellid persid beg end employment jobid
    1 1 19961210 19961231 0 0
    2 1 19960106 19960812 1 1
    3 1 19960812 19961001 0 0
    end
    I want to find the number of months employed in a year, where the month employed variable ought to be one if the person was at least one day in employment during the month. Any suggestions on how to approach this?

  • #2
    You have problems here on at least 3 levels.

    1. Your data example by itself doesn't seem to come from dataex as if you run that code it produces some nonsensical dates. The beg and end variables need to be long or double to avoid inputting nonsense. Alternatively, if those variables are held as string that needs to be shown in your data example.

    2.. Dates in the form you show are not fit for purpose as the difference between 19961231 and 19970101 "should be" 1 which is not what plain subtraction will give you. That can be fixed, as below, although my code assumes long and would need to be different if you really have string dates.

    3. If spells can span two or more calendar years and you need separate calculations by calendar years, you need more manipulation, but you need to helpsus to help you by giving a data example that does include such spells.


    Code:
    clear 
    input spellid persid long (beg end) employment jobid
    1 1 19961210 19961231 0 0
    2 1 19960106 19960812 1 1
    3 1 19960812 19961001 0 0
    end
    
    format beg end %8.0f 
    
    gen begdate = daily(strofreal(beg, "%8.0f"), "YMD")
    
    format begdate %td 
    
    list 
    
         +-----------------------------------------------------------------------+
         | spellid   persid        beg        end   employ~t   jobid     begdate |
         |-----------------------------------------------------------------------|
      1. |       1        1   19961210   19961231          0       0   10dec1996 |
      2. |       2        1   19960106   19960812          1       1   06jan1996 |
      3. |       3        1   19960812   19961001          0       0   12aug1996 |
         +-----------------------------------------------------------------------+

    Comment


    • #3
      Thank you.

      1. Yes, long.
      2. Yes, they can be string and they still need to be converted from YYYYMMDD to %td format
      3. Ah right, that complication does not arise in my case. Spells do not span over multiple years, at the end of the year they are split into a new spell.

      Comment


      • #4
        So, there doesn’t seem to much to add, as the length of a spell is just the difference between dates (+ 1, if say you regard employment from say 7 June to 8 June as 2 days not 1).

        You can pull year out of a daily date with the year() function.

        Comment


        • #5
          Later in the do-file I transform the data to yearly panel with just persid and year. Before doing that here I'd like to extract the number of months in the year that person was employed. In the example that should be 8 months in 1996 since the employment spell covered January until August.

          Comment


          • #6
            Does that mean counting months in which someone was employed some of the time OR something else?

            Comment


            • #7
              Something like this, but then I kind of destroyed the whole structure of the dataset

              Code:
              clear 
              input spellid persid long (beg end) employment jobid
              1 1 19961210 19961231 0 0
              2 1 19960106 19960812 1 1
              3 1 19960812 19961001 0 0
              end
              
              format beg end %8.0f 
              
              gen begdate = daily(strofreal(beg, "%8.0f"), "YMD")
              gen enddate = daily(strofreal(end, "%8.0f"), "YMD")
              
              
              format begdate %td 
              
              list 
              
              newspell tolong, id(persid) snumber(spellid) stype(employment) begin(begdate) end(enddate) time(t)
              
              format t %td
              gen month = mofd(t)
              format month %tm
              
              gen year = yofd(t)
              format year %ty
              
              gen empl = (employment1==1)
              bysort persid month: egen maxempl = max(empl)
              
              keep persid month maxempl year
              duplicates drop
              
              bysort persid year: egen moempl = total(maxempl)

              Comment


              • #8
                PS: newspell is a great user-written command by Hannes Kröger (https://journals.sagepub.com/doi/pdf...867X1501500110), which however does not work on my real dataset as the operating system refuses to provide me memory.

                Comment


                • #9
                  Originally posted by Nick Cox View Post
                  Does that mean counting months in which someone was employed some of the time OR something else?
                  Counting how many months someone was employed yes.

                  Comment


                  • #10
                    As you've found out, that's a messy calculation: you need to account for (1) two or more months in a spell and (2) two or more spells in a month. Sorry, but I am not volunteering code. The total number of days employed in a year would seem to carry more or less the same information and be much easier to calculate.

                    Comment

                    Working...
                    X