Announcement

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

  • Create time intervals given date (including time)

    Hi,

    I would like to create 3 time intervals with a dummy variable called "period":
    period = 1: 12:00:00 and below
    period = 2: 12:00:00 to 14:00:00
    period = 3 :14:00:00 and above

    I have a variable "date" that contains the date and time, properly entered in Stata. Here is an example:
    date
    03feb2015 15:33:30

    Here is the code (or pseudo-code) that I have so far:
    * Break down by intervals:
    gen period = 1 if date < 10:00:00
    replace period = 2 if missing(period) & date <= 12:00:00
    replace period = 3 if missing(period) & date <= 14:00:00
    replace period = 4 if missing(period) & date > 14:00:00

    Anyone can help? Thanks a lot!

  • #2
    All you have to do is

    1. read help dates and times

    2. play with examples

    3. work out what you need

    1 and 2 give me in a few seconds

    Code:
     
    . di   hh(clock("24 Feb 2015 09:01:00", "DMY hms"))
    9
    from which what you need in your first example appears to be something like

    Code:
     
    gen hours = hh(mydatetime) 
    gen mycat = cond(hours < 12, 1, cond(hours < 14, 2, 3))
    I say "something like" as

    a. What happens if the time is exactly 12:00:00? You say slightly different things. You may need other functions if getting it right to the nearest minute, second or millisecond really matters.

    b. Your examples are different.

    c. Such a variable would widely not be called a dummy variable.

    but the details are up to you.


    Comment


    • #3
      Nick, thanks a lot for your response. That really helps!

      Comment


      • #4
        How would you modify the code if you wanted to create thin intervals thoughout the day? Say 15 minute intervals? Is there a more efficient way to achieve that? Thanks!

        Comment


        • #5
          I would use some variation on k * floor(datetime/k) where k is say 15 * 60 * 1000

          Comment


          • #6
            Would you be able to provide a bit more details on how to set k? Where is the 1000 from for instance? What would k look like with, say 90 minutes intervals? Thanks a lot!

            Comment


            • #7
              I didn't know the solution was so inscrutable, but the solution for 15 minutes was just making explicit that

              1000 milliseconds = 1 second
              60 seconds = 1 minute
              etc.

              otherwise someone might ask where 900000 appeared from.

              The solution for 90 minutes could be just the solution for 15 minutes with 15 changed to 90 if you were satisfied with the results.

              Comment


              • #8
                Nick Cox i have trade data over a period from 1995 to 2022...now i want to regress the tradd data over 3, 4 and 5 year intervals like i want to reg data over 3 year intervals like 1995, 1997, 2000 and so on, over 4 yr and 5 yr intervals in the same manner...how can i do this in stata...like what is the command to do this...thanks

                Comment


                • #9
                  Sorry but I can't follow #8 well enough to give advice. For example, how is 1995, 1997, 2000 a 3 year interval? I would start a new thread with a clear data example.

                  Comment


                  • #10
                    Nick Cox by 3 year interval i mean if i start with 1995...i would skip 3 years and then again include 1999, then again skiping more 3 years i want to include 2003 in my regression equation...i want a simple and short command to do this in my reg command

                    Comment


                    • #11
                      You've not started a new thread or given a data example and, sorry, I can't follow #10 any further than #8. If you want regression with rolling intervals, consider rolling (official), asreg (SSC) or rangestat (SSC). If you want regressions with disjoint intervals, consider statsby, by: or just using if conditions, as in


                      Code:
                      regress ... if inrange(year, 1995, 1997)
                      where the ... cover whatever else you want.

                      Comment


                      • #12
                        thanks Nick Cox

                        Comment

                        Working...
                        X