Announcement

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

  • Splitting a numeric variable

    Hello everybody,

    I conducted a survey and want to code the completion time of the participants.
    I have the following variables:

    For example for one id:
    -startdate (e.g. "24jul2022 09:28:56")
    -datestamp (e.g. "24jul2022 09:35:11")
    Both are numeric variables.

    -> Now I want to calculate the time the participant needed to complete the survey.
    How do I calculate it from the above mentioned variables ? I thought that I at first have to seperate the date from the time, but how ?

    Thank you so much in advance1

    Best regards,

    Else Distel

  • #2
    Please give data examples (FAQ Advice #12).

    What you want is just the difference between two variables, and you might want to adjust the units of measurement at the same time. Here seconds and minutes seem possible units.

    Code:
    clear 
    set obs 1 
    gen double startdate = clock("24jul2022 09:28:56", "DMY hms")
    gen double datestamp = clock("24jul2022 09:35:11", "DMY hms")
    format * %tc 
    
    g difference_s = (datestamp - startdate) / 1000 
    g difference_m = difference_s / 60 
    
    list 
    
         +---------------------------------------------------------------+
         |          startdate            datestamp   differ~s   differ~m |
         |---------------------------------------------------------------|
      1. | 24jul2022 09:28:56   24jul2022 09:35:11        375       6.25 |
         +---------------------------------------------------------------+

    Comment


    • #3
      And for those using the most recent versions of Stata there is also:
      Code:
      gen difference_s = clockdiff_frac(startdate, datestamp, "s")
      gen difference_m = clockdiff_frac(startdate, datestamp, "m")
      which produces the same results.

      I am fond of clockdiff_frac(), and the related clockdiff(), because it avoids the need to introduce the number of milliseconds per second, etc. Matter of taste, really.

      Comment

      Working...
      X