Announcement

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

  • Subtracting hours

    Good evening, I would like to kindly ask for help subtracting these two times. The variable cs_wake_time is the time the person wakes up, and cs_bed_time is the time they go to bed.
    In order to subtract the times, I tried converting them to a time format using the following:

    gen cs_wake_timea = clock(cs_wake_time, "hm")
    format cs_wake_timea %tcHH:MM

    But the result is only missing values.


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str5(cs_wake_time cs_bed_time)
    "9 am" "11 pm"
    "9 am" "11 pm"
    "8 am" "12 am"
    "6 am" "10 pm"
    "7 am" "10 pm"
    "7 am" "1 am" 
    "9 am" "11 pm"
    "7 am" "1 am" 
    "7 am" "1 am" 
    "7 am" "12 am"
    end

  • #2
    Originally posted by Adolfo Aramburu View Post
    I tried converting them to a time format using the following:

    gen cs_wake_timea = clock(cs_wake_time, "hm")

    . . . But the result is only missing values.
    There are no minutes in your dataset. Try something like the following instead.
    Code:
    generate double cs_wake_timea = clock(cs_wake_time, "h")

    Comment


    • #3
      This is a case where I would ignore Stata's clock time based on milliseconds and use my own logic. That may seem perverse, but there you go.

      In your data we need to deal not just with a mix of am and pm for bed time but also instances of 12 am.

      The point of checking for negative and missing values of the wanted variable is to check for puzzling or inconsistent input (e.g. entering times the wrong way round). .

      Plotting wake against bed would be a simple check.

      Another check could be

      Code:
      list *_time if !inlist(word(cs_wake_time, 2), "am", "pm") | !inlist(word(cs_bed_time, 2), "am", "pm")
      Code:
      clear
      input str5(cs_wake_time cs_bed_time)
      "9 am" "11 pm"
      "9 am" "11 pm"
      "8 am" "12 am"
      "6 am" "10 pm"
      "7 am" "10 pm"
      "7 am" "1 am" 
      "9 am" "11 pm"
      "7 am" "1 am" 
      "7 am" "1 am" 
      "7 am" "12 am"
      end
      
      order cs_bed_time
      
      gen bed = real(word(cs_bed_time, 1))
      replace bed = bed + 12 if word(cs_bed_time, 2) == "am" & bed < 12 
      
      gen wake = real(word(cs_wake_time, 1)) + 12 
      
      gen wanted = wake - bed 
      
      sort wake bed 
      
      list if wanted < 0 
      
      list if wanted == .  
      
      list, sepby(bed)
      
           +-------------------------------------------+
           | cs_bed~e   cs_wak~e   bed   wake   wanted |
           |-------------------------------------------|
        1. |    10 pm       6 am    10     18        8 |
        2. |    10 pm       7 am    10     19        9 |
           |-------------------------------------------|
        3. |    12 am       7 am    12     19        7 |
           |-------------------------------------------|
        4. |     1 am       7 am    13     19        6 |
        5. |     1 am       7 am    13     19        6 |
        6. |     1 am       7 am    13     19        6 |
           |-------------------------------------------|
        7. |    12 am       8 am    12     20        8 |
           |-------------------------------------------|
        8. |    11 pm       9 am    11     21       10 |
        9. |    11 pm       9 am    11     21       10 |
       10. |    11 pm       9 am    11     21       10 |
           +-------------------------------------------+

      Comment

      Working...
      X