Announcement

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

  • Convert time variable (09mar2020 13:01:54) into a categorical variable or into groups

    Hi!
    I run STATA 17 on a Mac. I have a dataset with 826 observations and around 1,089 variables.

    One of the variables is the time they submitted a form at the clinic "f_submittime", and the data looks like this: "09mar2020 13:01:54". The type is "float" if that helps.
    I want to find a way to just divide all observations into two groups depending on if it was before or after 12.00, no matter what the date was.
    How can I convert this variable into something that I can use to create a new binary variable that is "time after 12" y/n.
    I have searched, but have not been able to find the answer.

    I would appreciate very much if someone could help me!
    Last edited by Andre Thunberg; 22 Nov 2023, 02:16.

  • #2
    See help datetime which explains that it's dangerous to hold date-times in float variables. They should be held in double variables. It would be prudent (but may not be practical) to repeat data import to ensure that.

    That said, the same help provides the information you need.

    Code:
    clear 
    input str20 strdatetime
    "09mar2020 13:01:54"
    "22nov2023 09:35:00"
    end 
    
    gen double numdatetime = clock(strdatetime, "DMY hms")
    format %tc numdatetime 
    
    gen hour = hh(numdatetime)
    gen wanted = hour >= 12 
    
    list 
    
         +---------------------------------------------------------+
         |        strdatetime          numdatetime   hour   wanted |
         |---------------------------------------------------------|
      1. | 09mar2020 13:01:54   09mar2020 13:01:54     13        1 |
      2. | 22nov2023 09:35:00   22nov2023 09:35:00      9        0 |
         +---------------------------------------------------------+

    Comment


    • #3
      Hi,
      Thank you so much Nick Cox for taking the time to answer me.
      At first I was not able to get it to work, but after converting the variable (r_submittime) into string first (using an answer from you in another thread), I think I got it right.

      generate str_r_submit = string(r_submittime, "%tc")
      gen double numdatetime = clock(str_r_submit, "DMY hms")
      format %tc numdatetime
      gen hour = hh(numdatetime)
      gen after12 = hour >= 12

      Looks like this now:
      Click image for larger version

Name:	Screenshot 2023-11-22 at 14.58.27.png
Views:	1
Size:	1.71 MB
ID:	1734734

      Thank you very much again!
      André
      Last edited by Andre Thunberg; 22 Nov 2023, 07:04. Reason: Found out what I did wrong and I think I got it right now.

      Comment


      • #4
        In terms of your example you could go straight to

        Code:
         
         gen hour = hh(r_submittime)
        The first part of #2 was just showing you how to go from a string date-time to a double.

        Comment


        • #5
          Aha, thank you!
          So impressed with how fast you answered and helped me. Thanks a lot!

          Comment

          Working...
          X