Announcement

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

  • Is there a way to separate a dataset based on dates?

    Hi everyone,

    I have a quick question, probably easy for most of you. I want to separate a dataset into two chunks, based on dates, and then produce some summary statistics related to it, but I don't know how to do that in the best way.
    Basically, I want to divide my sample into contracts than began before 01jun2021, and those than began on 01jun2021 onwards, and then produce some tables with descriptive statistics about it.

    At the end, I want to retrieve my original sample, if possible please

    Here is a dataex:


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long id double(date_contract_start date_contract_end) long idcontrato
    1001 18887 21700    1001
    1001 21701 22431  451697
    1001 22432 22645 1236132
    1001 22646 22676 1730454
    1001 22677 22735 2082075
    1001 22736 23010 2172904
    1001 23011 23069 2872183
    1001 23070     . 3107888
    end
    format %td date_contract_start
    format %td date_contract_end
    Your help is really appreciated.
    Thanks!

    Best,

    Michael

  • #2
    You can use if qualifiers such as


    Code:
    ... if date_contract_start < mdy(6, 1, 2021)
    or with a different operator >= -- or use a binary variable such

    Code:
    gen group = date_contract_start >= mdy(6, 1, 2021)
    within a call to by: or by(). There are other date functions that would be fine.

    Dates aren't always complicated. Here the way that Stata holds dates leads to direct manipulations.

    Comment


    • #3
      Hi Nick,

      Beautiful. Works nicely!


      Thanks again for your help. Lovely afternoon.
      Best,

      Michael

      Comment

      Working...
      X