Announcement

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

  • how to format time in stata

    Hi, i have a column called time_of_death that looks like this

    time_death
    09:00

    11:55

    19:11
    17:16
    05:30
    01:23
    16:35
    14:04
    20:35
    17:56

    19:14
    21:20
    05:30
    12:06

    I want to be able convert it to something that allows me to be able to say the number of deaths that occurred after 7pm (19:00). I also want to create a variable (such as "nightshift death" i,e. between 7pm and 7am), how can I do that?

  • #2
    So, is your variable string or numeric with value labels? Please use a data example as explained in FAQ Advice #12 to make that clear.

    Does after 19:00 mean between 19:00 and 23:59?

    Comment


    • #3
      Assuming time_death is string you can try something like this:
      Code:
      gen hr = substr(time_death, 1, 2)
      destring hr, replace
      gen is_nightshift_death = (inrange(hr, 19, 23) | inrange(hr, 00, 06))

      Comment


      • #4
        they are string. and sorry that's a good question regarding what does "after 1900" means. now that you mention it, I think it's better if can create 4 time periods in a day: 7am to 1pm, 1pm to 7pm, 7pm to 1am, 1am to 7am

        Comment


        • #5
          Here's a data example as desired and some technique building on Aaditya Dar 's helpful post.

          Note that you may not need to play with Stata's date-time functionality if some elementary string and numeric manipulations get you want.

          Do check that the rules are what you want with boundary cases 01:00, 07:00, 13:00, 19:00.

          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input str5 time_death
          "09:00"
          ""     
          "11:55"
          ""     
          "19:11"
          "17:16"
          "05:30"
          "01:23"
          "16:35"
          "14:04"
          "20:35"
          "17:56"
          ""     
          "19:14"
          "21:20"
          "05:30"
          "12:06"
          end
          
          split time_death, parse(":") destring 
          
          rename (time_death?) (hour min)
          
          gen wanted = 1 if inrange(hour, 7, 12) 
          replace wanted = 2 if inrange(hour, 13, 18)
          replace wanted = 3 if inrange(hour, 19, 23) | hour == 0 
          replace wanted = 4 if inrange(hour, 1, 6)
          
          sort time_death 
          
          list , sepby(wanted)
          
               +--------------------------------+
               | time_d~h   hour   min   wanted |
               |--------------------------------|
            1. |               .     .        . |
            2. |               .     .        . |
            3. |               .     .        . |
               |--------------------------------|
            4. |    01:23      1    23        4 |
            5. |    05:30      5    30        4 |
            6. |    05:30      5    30        4 |
               |--------------------------------|
            7. |    09:00      9     0        1 |
            8. |    11:55     11    55        1 |
            9. |    12:06     12     6        1 |
               |--------------------------------|
           10. |    14:04     14     4        2 |
           11. |    16:35     16    35        2 |
           12. |    17:16     17    16        2 |
           13. |    17:56     17    56        2 |
               |--------------------------------|
           14. |    19:11     19    11        3 |
           15. |    19:14     19    14        3 |
           16. |    20:35     20    35        3 |
           17. |    21:20     21    20        3 |
               +--------------------------------+

          Comment

          Working...
          X