Announcement

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

  • generating time duration from strings

    Hi Everyone,
    I am using Italian Time use data. Since I have the time at which an episode begins and the time at which the episode ends, is there a way to tell STATA that those strings are duration (i.e. is there a way to generate the duration of an episode once having the time at which you start doing that activity and the time at which you end doing it) ?
    Since my data are confidential I will post an example of my dataset:
    Code:
    input str2(starthour startminute endhour endminute)
    "04" "00" "09" "50"
    "09" "50" "10" "00"
    "10" "00" "10" "10"
    "10" "10" "11" "30"
    "11" "30" "12" "50"
    "12" "50" "15" "00"
    "15" "00" "16" "00"
    "16" "00" "16" "50"
    "16 "50" "17" "50"
    "17" "50" "18" "00"
    "18" "00" "18" "30"
    "18" "30" "18" "50"
    "18" "50" "21" "00"
    "21" "00" "21" "30"
    "21" "30" "22" "00"
    "22" "00" "22" "40"
    "22" "40" "04" "00"
    where "starthour" is the hour at which you start doing a given activity, "startminute" is the minute in which you start a given activity, "endhour" and "endmin" is the time at which you end doing that activity!
    Thank you all in advance,
    Andrea

  • #2
    Perhaps this sample code will start you in a useful direction. And thank you for including sample data that spanned two days - that's always something that needs to be considered.
    Code:
    clear
    input str2(starthour startminute endhour endminute)
    "04" "00" "09" "50"
    "09" "50" "10" "00"
    "10" "00" "10" "10"
    "10" "10" "11" "30"
    "11" "30" "12" "50"
    "12" "50" "15" "00"
    "15" "00" "16" "00"
    "16" "00" "16" "50"
    "16" "50" "17" "50"
    "17" "50" "18" "00"
    "18" "00" "18" "30"
    "18" "30" "18" "50"
    "18" "50" "21" "00"
    "21" "00" "21" "30"
    "21" "30" "22" "00"
    "22" "00" "22" "40"
    "22" "40" "04" "00"
    end
    destring starthour startminute endhour endminute, replace
    format starthour startminute endhour endminute %02.0f
    replace endhour = endhour+24 if endhour < starthour 
    generate long min_dur = (endhour-starthour)*60 + (endminute-startminute)
    Code:
    . list, noobs sepby(starthour)
    
      +----------------------------------------------------+
      | starth~r   startm~e   endhour   endmin~e   min_dur |
      |----------------------------------------------------|
      |       04         00        09         50       350 |
      |----------------------------------------------------|
      |       09         50        10         00        10 |
      |----------------------------------------------------|
      |       10         00        10         10        10 |
      |       10         10        11         30        80 |
      |----------------------------------------------------|
      |       11         30        12         50        80 |
      |----------------------------------------------------|
      |       12         50        15         00       130 |
      |----------------------------------------------------|
      |       15         00        16         00        60 |
      |----------------------------------------------------|
      |       16         00        16         50        50 |
      |       16         50        17         50        60 |
      |----------------------------------------------------|
      |       17         50        18         00        10 |
      |----------------------------------------------------|
      |       18         00        18         30        30 |
      |       18         30        18         50        20 |
      |       18         50        21         00       130 |
      |----------------------------------------------------|
      |       21         00        21         30        30 |
      |       21         30        22         00        30 |
      |----------------------------------------------------|
      |       22         00        22         40        40 |
      |       22         40        28         00       320 |
      +----------------------------------------------------+

    Comment

    Working...
    X