Announcement

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

  • subtract x hours from date variable -

    I am trying to construct a time variable x hours before or after a specific date
    Even though I constructed the time variable in tc format I am having leap second adjustment

    What I am trying to do is to have eventtime_1hour seconds part to be exactly 00..What I got is the following:

    lets assume x is 1 hour

    gen a=60*60000 (1 hour in miliseconds)

    . gen eventtime_1hour= eventtime- a

    . format %tc eventtime_1hour

    is there anyway to have eventtime_1hour to be 26mar1989 02:00:00; 24sep1989 03:00:00 etc....

    a eventtime_1hour eventtime
    3600000 26mar1989 02:00:05 26mar1989 03:00:00
    3600000 24sep1989 02:59:43 24sep1989 04:00:00
    3600000 31dec1989 22:59:53 01jan1990 00:00:00
    3600000 31dec1989 23:00:59 01jan1990 00:01:00
    3600000 31dec1989 23:02:04 01jan1990 00:02:00
    3600000 31dec1989 23:03:10 01jan1990 00:03:00
    3600000 31dec1989 23:04:15 01jan1990 00:04:00



  • #2
    Use round(<datetime>, 60*60000)

    Comment


    • #3
      The suggested round() does not solve the problem; it rounds the result to the nearest hour, and I don't think that is what n??? wants (we prefer seeing real names). It is a matter of precision; see help precision. Generate the eventtime variables as double:
      Code:
      gen double eventtime_1hour = eventtime-a

      Comment


      • #4
        I misread the question. I also think Nick did; n??? wanted rounding to the nearest minute, not the nearest hour. I stored the variables of interest as double:

        Code:
        gen double a = 60*60*1000
        gen double eventtime_1hour = round(eventtime+a, 60000)
        format eventtime* %tc
        
        . list eventtime*, clean
                        eventtime      eventtime_1hour 
          1.   26mar1989 02:00:05   26mar1989 03:00:00 
          2.   24sep1989 02:59:43   24sep1989 04:00:00 
          3.   31dec1989 22:59:53   01jan1990 00:00:00 
          4.   31dec1989 23:00:59   01jan1990 00:01:00 
          5.   31dec1989 23:02:04   01jan1990 00:02:00 
          6.   31dec1989 23:03:10   01jan1990 00:03:00 
          7.   31dec1989 23:04:15   01jan1990 00:04:00

        Comment


        • #5
          Thanks so much!
          Nazlı

          Comment

          Working...
          X