Announcement

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

  • Time range dummies for a minimum number of months

    Hello,

    I have a dataset with spells on employment situations (spelltype) and the begin and end of these spells. The dates are in a monthly format, but counted from January 1983 onwards as 1,2,... For 2017 the months would be between 409 and 420.
    I need to create dummies per year if the spell lasted at least 7 months in a certain year. I am unsure if there is any code that identifies time ranges.

    I would appreciate any help!

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(spelltyp begin end)
     6 390 444
     3 406 411
    15 410 411
    15 415 417
     3 421 422
     3 424 427
     3 430 434
     3 436 439
     3 442 444
     6 380 444
    end
    label values spelltyp spelltyp
    label def spelltyp 3 "[3] (bis 04) teilzeit/geringfuegig; (ab 05) teilzeit", modify
    label def spelltyp 6 "[6] in Rente-Ruhestand", modify
    label def spelltyp 15 "[15] Minijob (bis 400 Euro)", modify
    label values begin begin
    label def begin 380 "[380] Aug 2014", modify
    label def begin 390 "[390] Jun 2015", modify
    label def begin 406 "[406] Okt 2016", modify
    label def begin 410 "[410] Feb 2017", modify
    label def begin 415 "[415] Jul 2017", modify
    label def begin 421 "[421] Jan 2018", modify
    label def begin 424 "[424] Apr 2018", modify
    label def begin 430 "[430] Okt 2018", modify
    label def begin 436 "[436] Apr 2019", modify
    label def begin 442 "[442] Okt 2019", modify
    label values end end
    label def end 411 "[411] Mar 2017", modify
    label def end 417 "[417] Sep 2017", modify
    label def end 422 "[422] Feb 2018", modify
    label def end 427 "[427] Jul 2018", modify
    label def end 434 "[434] Feb 2019", modify
    label def end 439 "[439] Jul 2019", modify
    label def end 444 "[444] Dez 2019", modify

  • #2
    Thank you for using -dataex- on your very first post!

    The coding of the dates with a base month of Jan 1983 is unfortunate. Working with Stata internal format monthly date variables simplifies things:
    Code:
    //    FIRST GET THE BEGIN AND END VARIABLES IN STATA INTERNAL FORMAT MONTHLY DATES
    foreach v of varlist begin end {
        gen `v'_sif = `v' + tm(1982m12)
        format `v'_sif %tm
    }
    
    //    FOR EACH YEAR 2015 THROUGH 2019, CREATE AN INDICATOR FOR WHETHER
    //    THE SPELL OVERLAPPED THAT CALENDAR YEAR BY AT LEAST 7 MONTHS
    forvalues y = 2015/2019 {
        gen overlap_`y' = min(end_sif, tm(`y'm12)) - max(begin_sif, tm(`y'm1)) + 1
        gen wanted_`y' = overlap_`y' >= 7
    }
    Notes:
    1. I'm not entirely sure I understand what you meant by "...dummies per year if the spell lasted at least 7 months in a certain year." So if this isn't it, when posting back, please show a hand-worked example of what you do want.
    2. I interpret the duration of a spell as including both the beginning and end months. So if begin = Jan 2019 and end = Feb 2019, I consider that to be 2 months. If you do not intend to count both the begin and end months as part of the spell, then remove the +1 from the -gen overlap_`y'...- command.
    Added: Correction to 2. If you do not wish to count both the beginning and end months as part of the spell duration the correct way to get the correct overlap is:
    Code:
    gen overlap_`y' = min(end_sif - 1, tm(`y'm12)) - max(begin_sif, tm(`y'm1)) + 1
    The reason for doing it this way is that the end point of the overlap period might be the end of the calendar year, not the end of the spell. So you would still want to count the end of the calendar year in the overlap period in that case. This is an edge case which my suggestion in 2 handles incorrectly.
    Last edited by Clyde Schechter; 23 Oct 2024, 11:35.

    Comment


    • #3
      Thanks a lot!
      The code works perfectly and captures exactly what I needed.

      Comment

      Working...
      X