Announcement

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

  • Calculating duration (time)

    Hello,

    I have 2 variables for start and end time in numerical form and 2 dummy variables for am/pm and I am trying to calculate the duration. I created 2 string variables from start and end time. However it is in 12-hour format. So, when I simple take the difference, it is not correct.


    My goal is to create a viable of the duration between start and end time.
    Any help would be appreciated. Here is my code.

    *add zero at the beginning
    gen str starttimeS = string(starttime)
    gen str starttime2 = string(real(starttimeS), "%04.0f")

    gen str endtimeS = string(endtime)
    gen str endtime2 = string(real(endtimeS), "%04.0f")

    *convert str to time format
    gen s = clock(starttime2, "hm")
    format s %tcHH:MM

    gen e = clock(endtime2, "hm")
    format e %tcHH:MM


  • #2
    I have 2 variables for start and end time in numerical form

    I don't know what you mean by this. If these are Stata internal format numerical clock variables then all you havfe to do is subtract. But given that you are going through hoops to first create a string and then convert those to -clock()- variables, I imagine they are some other kind of numeric variables. But without seeing examples of them, it is impossible to give you help here. Please use the -dataex- command to show examples of your actual data.
    If you are running version 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Comment

    Working...
    X