Announcement

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

  • Timespan

    Hi everyone. I am a STATA beginner trying to calculate a time span. I have one variable “in” with date and time for example: 1/1/2021 10:00 (format %tcnn/dd/ccYY_hh:MM). I also have a variable “out” for example: 2/2/2021 12:00. I would like to calculate how many days it is between “in” and “out”. I tried datediff(in, out, day[,1mar]) but it does not work.
    Any suggestions?
    All the best!

  • #2
    perhaps,
    Code:
    gen between = dofc(out-in)

    Comment


    • #3
      Code:
      g timebetween = date2 - date1
      or try

      Code:
      ssc install eperiod

      Comment


      • #4
        Thank you for your rapid replies!

        When I try: “generate Time = Out – In” I get answers like 6000000. What is that? First, I thought maybe seconds, but that’s not correct…

        And when I try ”generate Time = dofc(Out- In)” or “generate Time = dofC(Out- In)” I get answers like 0, 1 or 12. If I am correct that is full days. But I would like to be a little more precise than just full days. Would it be possible to get the answer in hours?

        And finally, I ran “ssc install eperiod”. Then, when I try ”generate Time = eperiod(Out- In) I get “unknown function eperiod()”. How do I initiate eperiod correctly?

        Comment


        • #5
          This was written before post #4.

          By "days between" do you mean 24-hour periods or do you mean calendar days?

          If in is 10/22/2021 22:00 and out is 10/23/2021 06:00 then out is 8 hours after in - less than 24 hours - but on the next day, one calendar day later.

          Consider the following examples.
          Code:
          * set up example data
          clear
          input str16(var1 var2)
          "10/22/2021 22:00" "10/23/2021 06:00"
          end
          generate double t_in  = clock(var1,"MDYhm")
          generate double t_out = clock(var2,"MDYhm")
          format %tc t_in t_out
          drop var1 var2
          * present examples
          generate diff_d = dofc(t_out)-dofc(t_in)
          generate diff_h = (t_out-t_in) / (1000*60*60)
          * results
          list
          Code:
          . list
          
               +-----------------------------------------------------------+
               |               t_in                t_out   diff_d   diff_h |
               |-----------------------------------------------------------|
            1. | 22oct2021 22:00:00   23oct2021 06:00:00        1        8 |
               +-----------------------------------------------------------+
          Stata's "date and time" variables are complicated and there is a lot to learn. If you have not already read the very detailed Chapter 24 (Working with dates and times) of the Stata User's Guide PDF, do so now. If you have, it's time for a refresher. After that, the help datetime documentation will usually be enough to point the way. You can't remember everything; even the most experienced users end up referring to the help datetime documentation or back to the manual for details. But at least you will get a good understanding of the basics and the underlying principles. An investment of time that will be amply repaid.

          All Stata manuals are included as PDFs in the Stata installation (since version 11) and are accessible from within Stata - for example, through the PDF Documentation section of Stata's Help menu.

          Comment


          • #6
            https://www.statalist.org/forums/for...etween-2-dates is the same question, really, but the thread has extra details.

            To spell out a point arising there the units of date-times are ms (milliseconds!)



            Last edited by Nick Cox; 03 Nov 2021, 10:11.

            Comment


            • #7
              Code:
              eperiod date1 date2 , gen(datediff)
              but eperiod only does year, month, or day.

              Comment


              • #8
                Thank you all. I solved the problem using:
                generate TimeMs = Out-In
                generate TimeHrs = TimeMs/3600000
                Then I got my output in hours which works out well for me. Thank you again.

                Comment


                • #9
                  Charlotte Romare, in Stata 16 and 17, we introduced several new functions that make calculations of date and time durations convenient and precise. From another post of yours (on datepart() and clockpart()), I understand you are using Stata 16, where some of these functions may not be available. However, Stata 16 has functions datediff() and clockdiff() that can give you the duration in days between two dates and two datetimes, respectively. clockdiff() can also give the duration in hours (and other units), which may be useful to you. Please make sure you do update all to get the latest. Also, you mentioned that datediff() does not work. Can you tell us why, with an example? We would be concerned if it is not giving the difference you expected.

                  Comment

                  Working...
                  X