Announcement

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

  • How to know the average time a household spent in my sample?

    Hi everyone,

    I have a quick question to ask you. I have a dataset about contract starting dates, and contract ending dates about household electricity consumption in Spain. My aim is:
    • I would like to know how much time spent, on average, households in my sample, please.
    Here is a small -dataex- for my first ID:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long id double(date_contract_start date_contract_end)
    1001 18887 21700
    1001 21701 22431
    1001 22432 22645
    1001 22646 22676
    1001 22677 22735
    1001 22736 23010
    1001 23011 23069
    1001 23070     .
    Could you give me some insights on how to do that?
    Thank you very much in advance.
    Best,

    Michael

    Edit: Sometimes, the date_contract_end is missing, meaning that this household's contract is not terminated.
    Last edited by Michael Duarte Goncalves; 21 Dec 2023, 08:17.

  • #2
    Code:
    g contract_time = date_contract_end - date_contract_start
    egen timeinsample = sum(contract_time), by(id)
    I'm not sure whether HH drop in an out. If not, then this would work too.
    Code:
    egen mark_start = min(date_contract_start), by(id)
    egen mark_end = max(date_contract_end), by(id)
    bys id: g timeinsample = mark_end - mark_start

    Comment


    • #3
      Hi George Ford:

      Thank you so much for your help, and time! Yes, in fact, HH can drop in and out.
      Have a lovely day.

      Best,
      Michael

      Comment

      Working...
      X