Announcement

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

  • elapsed time midnight to midnight without date

    Hi,

    what should I replace my clock time string (text like "00:00", "24:00" - so, no date included, mere clock time!) with? I want to
    Code:
    gen double h1m01v10q001_1_e2 = clock(h1m01v10q001_1_e, "hm")
    gen double h1m01v11q001_1_e2 = clock(h1m01v11q001_1_e, "hm")
    and
    Code:
    gen h1m01v50_1_g = (h1m01v11q001_1_e2 - h1m01v10q001_1_e2)/60000/60
    ?
    Stata doesn't calculate any difference between the two times, not even after
    Code:
    replace h1m01v11q001_1_e = h1m01v11q001_1_e + 24*60*60000
    .
    I could, of course,
    Code:
    replace h1m01v11q001_1_e = "23:59" if h1m01v11q001_1_e == "24:00"
    But if the real time difference is 2.00 hours, I don't want 0.02 hours less (23.98 hours).

    So, what may I do about it?
    Last edited by Franz Gerbig; 07 Aug 2019, 06:21.
    Thank you for reading (and some reply)
    Using Stata 16.1
    Extractions (-dataex-) of the data I'm working with is impossible, sorry!

  • #2
    It's not entirely clear what your desired result is. It appears you want the number of hours between two times. Perhaps the following example will start you in a useful direction.
    Code:
    . clear
    
    . set obs 1
    number of observations (_N) was 0, now 1
    
    . generate double t1 = clock("3:15","hm")
    
    . generate double t2 = clock("3:45","hm")
    
    . format %tcHH:MM t1 t2
    
    . generate double d = t2 - t1
    
    . generate dh = d/(1000*60*60)
    
    . list, noobs
    
      +------------------------------+
      |    t1      t2         d   dh |
      |------------------------------|
      | 03:15   03:45   1800000   .5 |
      +------------------------------+
    If this example doesn't prove helpful, please take a few moments to 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. It's particularly helpful to copy commands and output from your Stata Results window and paste them into your Statalist post using code delimiters [CODE] and [/CODE], and to use the dataex command to provide sample data, as described in section 12 of the FAQ.
    Last edited by William Lisowski; 07 Aug 2019, 09:31.

    Comment


    • #3
      After the post #2, I looked again at the title of this topic. It appears to me that you have a very specific question about midnight to midnight that was not obvious from your description.

      The first thing we note is that "24:00" is not a valid time value for Stata. So if your data has "24:00" you need to replace those with "0:00". Consider the following example.
      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str5(var1 var2)
      "3:15"  "3:45" 
      "3:15"  "15:15"
      "15:15" "3:15" 
      "0:00"  "0:00" 
      "0:00"  "24:00"
      "23:45" "0:15" 
      end
      generate double t1 = clock(var1,"hm")
      generate double t2 = clock(var2,"hm")
      format %tcHH:MM t1 t2
      list, clean noobs
      // 24:00 is not a valid time
      drop t1 t2
      replace var1 = "0:00" if var1=="24:00"
      replace var2 = "0:00" if var2=="24:00"
      generate double t1 = clock(var1,"hm")
      generate double t2 = clock(var2,"hm")
      format %tcHH:MM t1 t2
      generate double d = t2 - t1
      generate double dh = d/(1000*60*60)
      replace dh = dh + 24 if dh<=0
      list, clean noobs
      Code:
      . generate double t1 = clock(var1,"hm")
      
      . generate double t2 = clock(var2,"hm")
      (1 missing value generated)
      
      . format %tcHH:MM t1 t2
      
      . list, clean noobs
      
           var1    var2      t1      t2  
           3:15    3:45   03:15   03:45  
           3:15   15:15   03:15   15:15  
          15:15    3:15   15:15   03:15  
           0:00    0:00   00:00   00:00  
           0:00   24:00   00:00       .  
          23:45    0:15   23:45   00:15  
      
      . // 24:00 is not a valid time
      . drop t1 t2
      
      . replace var1 = "0:00" if var1=="24:00"
      (0 real changes made)
      
      . replace var2 = "0:00" if var2=="24:00"
      (1 real change made)
      
      . generate double t1 = clock(var1,"hm")
      
      . generate double t2 = clock(var2,"hm")
      
      . format %tcHH:MM t1 t2
      
      . generate double d = t2 - t1
      
      . generate double dh = d/(1000*60*60)
      
      . replace dh = dh + 24 if dh<=0
      (4 real changes made)
      
      . list, clean noobs
      
           var1    var2      t1      t2           d   dh  
           3:15    3:45   03:15   03:45     1800000   .5  
           3:15   15:15   03:15   15:15    43200000   12  
          15:15    3:15   15:15   03:15   -43200000   12  
           0:00    0:00   00:00   00:00           0   24  
           0:00    0:00   00:00   00:00           0   24  
          23:45    0:15   23:45   00:15   -84600000   .5
      In the future, please do try to provide an example that reproduces whatever problem it is you wish to solve.

      Comment


      • #4
        #2
        It's not entirely clear what your desired result is. It appears you want the number of hours between two times.
        sorry for the unspecified specification of the issue!
        Yes, I want to calculate the elapsed time between midnight (t1 = "00:00") and midnight (t2 = "24:00").

        #3
        The first thing we note is that "24:00" is not a valid time value for Stata. So if your data has "24:00" you need to replace those with "0:00".
        Thank you for this hint - it helped me out
        Last edited by Franz Gerbig; 07 Aug 2019, 09:49.
        Thank you for reading (and some reply)
        Using Stata 16.1
        Extractions (-dataex-) of the data I'm working with is impossible, sorry!

        Comment

        Working...
        X