Announcement

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

  • How do you count the number of times a contract begins and ends on a given date?

    Hi everyone,

    I would like to count the number of times a contract begins and ends at specific dates.
    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 22646 22676 1730454
    1001 22677 22735 2082075
    1001 23011 23069 2872183
    1001 22432 22645 1236132
    1001 21701 22431  451697
    1001 22736 23010 2172904
    1001 23070     . 3107888
    1005 23131     . 3215923
    1005 22646 22676 1742918
    end
    format %td date_contract_start
    format %td date_contract_end
    For example, I would like to know in my sample how many times a contract began at 17 September 2011 and ended at 31 May 2019. And so on for all possible combinations.
    Could you please give me a hand with that?

    I would like then to do some kind of plot suitable with that please.

    Thank you very much.
    Best,

    Michael

  • #2
    Michael:
    do you mean something along the following lines?
    Code:
    . egen wanted=group( date_contract_start date_contract_end)
    
    
    . list
    
         +--------------------------------------------------+
         |   id   date_co~t   date_co~d   idcont~o   wanted |
         |--------------------------------------------------|
      1. | 1001   17sep2011   31may2019       1001        1 |
      2. | 1001   01jan2022   31jan2022    1730454        4 |
      3. | 1001   01feb2022   31mar2022    2082075        5 |
      4. | 1001   01jan2023   28feb2023    2872183        7 |
      5. | 1001   01jun2021   31dec2021    1236132        3 |
         |--------------------------------------------------|
      6. | 1001   01jun2019   31may2021     451697        2 |
      7. | 1001   01apr2022   31dec2022    2172904        6 |
      8. | 1001   01mar2023           .    3107888        . |
      9. | 1005   01may2023           .    3215923        . |
     10. | 1005   01jan2022   31jan2022    1742918        4 |
         +--------------------------------------------------+
    
    
    . bysort wanted: egen counter=count(wanted) if wanted!=.
    
    
    . list
    
         +------------------------------------------------------------+
         |   id   date_co~t   date_co~d   idcont~o   wanted   counter |
         |------------------------------------------------------------|
      1. | 1001   17sep2011   31may2019       1001        1         1 |
      2. | 1001   01jun2019   31may2021     451697        2         1 |
      3. | 1001   01jun2021   31dec2021    1236132        3         1 |
      4. | 1001   01jan2022   31jan2022    1730454        4         2 |
      5. | 1005   01jan2022   31jan2022    1742918        4         2 |
         |------------------------------------------------------------|
      6. | 1001   01feb2022   31mar2022    2082075        5         1 |
      7. | 1001   01apr2022   31dec2022    2172904        6         1 |
      8. | 1001   01jan2023   28feb2023    2872183        7         1 |
      9. | 1005   01may2023           .    3215923        .         . |
     10. | 1001   01mar2023           .    3107888        .         . |
         +------------------------------------------------------------+
    
    drop wanted
    
    .
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Hi Carlo Lazzaro:

      Yes, that's what I wanted!
      Thank you so much for your help and time.

      Lovely morning.
      Michael

      Comment


      • #4
        Hi everyone,

        Perhaps just one more question:
        • Do you think there is a way to plot what is suggested in post #2 in a readable way? I need your suggestions and comments, please.
        I really appreciate any help you can provide.
        All the best,
        Michael

        Comment

        Working...
        X