Announcement

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

  • Integer Only for tsset

    Consider the following
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double(time output)
         1.7672256e+12 1089.3023560926383
    1767229199999.9998 1059.8515789484773
    1767232800000.0002  993.9003888320159
         1.7672364e+12 1002.1037581203718
    1767239999999.9998 1014.5429606629094
    1767243600000.0002  1038.115628910161
         1.7672472e+12 1059.0748396748595
    1767250799999.9998 1061.8988612010642
    1767254400000.0002 1095.3012794408835
          1.767258e+12 1142.1110120989183
    end
    format %tcnn/dd/ccYY_hh:MM time
    cls
    
    
    tsset time, clocktime
    When I try and tsset this, Stata complains, saying "time variable must contain only integer values". Why? I don't understand. How may I properly tsset this?


    EDIT: I read I can do something like this
    Code:
    g time = _n
    
    tsset time
    but this is absolutely maddening to me, since Stata should be able to tsset minute by minute data.
    Last edited by Jared Greathouse; 13 Dec 2022, 07:33.

  • #2
    The complaint is more than a little over the top, as the explanation and the solution are both simple:

    Stata doesn't claim to handle date-times to finer than ms (millisecond) precision

    if a date-time reported as ending in .9998 (decimal) is allowed, is Stata expected to look for times reported as ending in .9997 or in .9999 as adjacent, which is going to be hard since Stata can only work in binary at machine level?

    and all you need to do is push your date variable through round() to round instances of .9998 or .0002 or so to the nearest integer.

    This is an instance where Stata insists that you follow its rules -- in your own best interests.

    In any case, your tsset needs to call up the delta() option to do what you want.

    Comment


    • #3
      Okay, something like this perhaps?
      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input double(time output)
           1.7672256e+12 1089.3023560926383
      1767229199999.9998 1059.8515789484773
      1767232800000.0002  993.9003888320159
           1.7672364e+12 1002.1037581203718
      1767239999999.9998 1014.5429606629094
      1767243600000.0002  1038.115628910161
           1.7672472e+12 1059.0748396748595
      1767250799999.9998 1061.8988612010642
      1767254400000.0002 1095.3012794408835
            1.767258e+12 1142.1110120989183
      end
      format %tcnn/dd/ccYY_hh:MM time
      cls
      
      g time2 = round(time,0.0002)
      
      
      tsset time2, format(%tcnn/dd/ccYY_hh:MM) delta(59000)

      Comment


      • #4
        No.

        1. You need just round(time) which is equivalent to round(time, 1) as the needed rounding is to integers, i.e. the visible problem is just some .9998 that should be 1 and some 0.0002 that should be zero.

        2. Date-time calculations are unlikely to work well with ad hoc resolutions that don't fit the advertised repertoire.

        3. round(time, 0.0002) can't work well as almost all multiples of 0.0002 have no exact binary equivalents.

        4. Even if 0.0002 ms were a good unit, 1 minute is 500 * 1000 * 60 times that and 1 hour 60 times that in turn. Where does 59000 come from?

        5. Your generate command tries to put very large integers into a float, which won't work well (unless you have set generate to default to double).

        The solution is simple and direct.


        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input double(time output)
             1.7672256e+12 1089.3023560926383
        1767229199999.9998 1059.8515789484773
        1767232800000.0002  993.9003888320159
             1.7672364e+12 1002.1037581203718
        1767239999999.9998 1014.5429606629094
        1767243600000.0002  1038.115628910161
             1.7672472e+12 1059.0748396748595
        1767250799999.9998 1061.8988612010642
        1767254400000.0002 1095.3012794408835
              1.767258e+12 1142.1110120989183
        end
        
        replace time = round(time, 1)
        tsset time, delta(1 hour) format(%tc)
        
        list
        
            +--------------------------------+
             |               time      output |
             |--------------------------------|
          1. | 01jan2016 00:00:00   1089.3024 |
          2. | 01jan2016 01:00:00   1059.8516 |
          3. | 01jan2016 02:00:00   993.90039 |
          4. | 01jan2016 03:00:00   1002.1038 |
          5. | 01jan2016 04:00:00    1014.543 |
             |--------------------------------|
          6. | 01jan2016 05:00:00   1038.1156 |
          7. | 01jan2016 06:00:00   1059.0748 |
          8. | 01jan2016 07:00:00   1061.8989 |
          9. | 01jan2016 08:00:00   1095.3013 |
         10. | 01jan2016 09:00:00    1142.111 |
             +--------------------------------+

        Comment


        • #5
          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 and are accessible from within Stata - for example, through the PDF Documentation section of Stata's Help menu.

          Comment

          Working...
          X