Announcement

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

  • microseconds and Stata

    Hi,

    I have a dataset that contains timestamps at the microsecond level but I dont know how I can use that information in Stata.
    Does Stata stops at the millisecond level?

    Thanks!

  • #2
    In principle Stata can handle any time period as a variable, see here: http://www.stata.com/manuals13/ddatetime.pdf
    Your question might be more on how to have Stata understand the current format of your date/time variable? Would be useful to post what that looks like.

    Comment


    • #3
      Hi Jorrit

      the data looks like

      03/02/2015 15:32:23: 453439

      Comment


      • #4
        A few very limited experiments indicate that you can create a double clock time variable in milliseconds using clock() and then add a fractional part successfully. I don't know what display formats would make sense.

        Comment


        • #5
          Hi Jean Luc and Nick,

          I am facing a similar problem and was wondering if either of you found a solution.

          I have one variable in %tc format (CCYY-NN-DD_HH:MM:SS) and then one numeric variable that is in microseconds (so just a number ie a fraction of the second, something like 123456). I want to combine these two together, adding a fractional part to my time variable (so I want the %tc format to be CCYY-NN-DD_HH:MM:SS.SSSSSS)

          I've tried converting the microseconds to double clock time format and also to destring both variables, combine them in string and then convert to clock time format, but both ways I only get missing values.

          Do you have any suggestions?

          Thanks in advance!

          Comment


          • #6
            With %tc, the underlying Stata internal form (SIF) is number of milliseconds from 1/1/1960 00:00:00.000.

            I originally read this a milliseconds, so was going to advise: So, you can include the millisecond information that you have by adding it in.
            However, I now see it is microseconds. Since one microsecond = .001 milliseconds, you are necessarily going to lose some precision if you intend to use Stata internal dates to represent this number and/or to use in Stata functions.

            Code:
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input str27 date_hms long microsec
            "2016-04-22 15:32:23" 454439
            "2016-04-22 15:30:00"       33
            "2016-04-22 10:32:23"   458637
            "2016-04-22 10:32:23"  100100
            end
            
            gen double full=clock(date_hms, "YMDhms") + (microsec/1000)
            As for displaying the SIF, Stata does not display the "extra" milliseconds as milliseconds, but rather truncates the display to tenths, hundredths, or thousandths of a second.

            Code:
            format full %tcCCYY-NN-DD_HH:MM:SS.sss
            Alternatively, you may wish to simply display the date/time/microsecond as a string, in which case you could do the following:

            Code:
            gen string_date_hms_micro=string(full, "%tcCCYY-NN-DD_HH:MM:SS")+"  microsec: "+string(microsec, "%-12.0f")
            Stata/MP 14.1 (64-bit x86-64)
            Revision 19 May 2016
            Win 8.1

            Comment


            • #7
              Thank you for the thorough response Carole, very helpful!

              Since losing precision is not an option for me, I've decided to keep the two date variables separately. I can still perform my calculations, just using these two columns.

              Comment


              • #8
                Laura, the other option is to convert your date/time to number of microseconds since (choose date/time) and add them. You won't be able to display the variable in a nice human readable form, but you can use the string display above if that is necessary. This is what Stata does for its date/times. This will allow you to subtract/add/etc. I would expect this number, however, to be huge. So, you may end up losing precision anyway (see http://blog.stata.com/tag/precision/).
                Stata/MP 14.1 (64-bit x86-64)
                Revision 19 May 2016
                Win 8.1

                Comment

                Working...
                X