Announcement

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

  • Difference in Two Times

    Hi all-

    I am trying to create a loop to iterate over all the values in two variables "starttime" and "endtime" to find the difference between the two, in time format. They are formatted as such:

    10nov2017 13:14:24

    The type is double. I have been very unsuccessful thus far. Any suggestions?

  • #2
    Welcome to Statalist.

    The statement of your problem is unclear. Suppose you have 100 observations, each with a value for starttime and endtime. Do you mean you want to calculate the 100 differences - one in each of the 100 observations? If so, looping is not required - subtraction of starttime from endtime should produce the difference between the two times, expressed in units of milliseconds, and you can then apply a suitable format to display this value in a way that meets your needs.

    Stata's "datetime" 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.

    If I've misunderstood your problem, you should next review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. Note especially sections 9-12 on how to best pose your question.

    The more you help others understand your problem, the more likely others are to be able to help you solve your problem.
    Last edited by William Lisowski; 10 Nov 2017, 19:58.

    Comment


    • #3
      Guest:
      as as aside to Williams' excellent advice, you may want to try something along the following lines (please note that dealing with dates is one of the most interesting but at the same time demanding Stata features; see, just to start off, -help f_date- and related entries in Stata .pdf manual):
      Code:
      set obs 3
      g start_date="10nov2017 13:14:24" in 1
      replace start_date="11nov2017 14:14:24" in 2
      replace start_date="12nov2017 15:14:24" in 3
      g end_date="13nov2017 10:10:10"
      g double num_start_date = clock(start_date, "DMYhms")
      format num_start_date %tc
      g double num_end_date = clock(end_date, "DMYhms")
      format num_end_date %tc
      g diff = hours( num_end_date - num_start_date) *this last line of code will give you a difference between the two dates expressed in hours*
      Last edited by sladmin; 13 Nov 2017, 08:28. Reason: anonymize original poster
      Kind regards,
      Carlo
      (Stata 19.0)

      Comment

      Working...
      X