Announcement

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

  • adding one day to a date

    Dear Statalisters,

    I have a date variable which is in the following format (it is not a string) :

    Code:
    browse date
    . 30dec1899 23:09:25
    ...
    I would like to add one extra day to this date, if the hours are between 00:00 (midnight) and 04:59 am

    I proceed like this


    Code:
    capt drop hours
    gen hours=hours(date)
    
    
    replace date=date+1 if hours<5
    However stata does not produce any changes (0 replacement made). I checked on the internet already, but I cannot find a solution.

    Thank you for kind your help and your understanding






  • #2
    Dusan,

    Assuming your variable is a Stata datetime variable (format %tc...), the correct function to get the hour of the day is hh() not hours().

    You are also going to have trouble just adding one to get the next day. A Stata datetime variable is the number of milliseconds since 1/1/1960 at midnight, so to add a full day you will have to add 24*60*60*1000 milliseconds (or something less than that if you don't want to add a full 24 hours).

    Regards,
    Joe

    Comment


    • #3
      Thanks a lot Joe!

      Comment


      • #4
        You are using an incorrect function and not considering you need milliseconds.

        Code:
        clear
        set more off
        
        *----- example data -----
        
        input ///
        str25 event
        "30dec1899 23:09:25"
        "30dec1899 01:15:12"
        "30dec1899 04:59:59"
        end
        
        gen double event2 = clock(event, "DMYhms")
        format %tc event2
        list
        
        *----- what you want -----
         
        gen double event3 = event2 + msofhours(24) if hh(event2) < 5
        format %tc event3
        list event?
        Read -help datetime- carefully, as well as the corresponding manual entries.
        You should:

        1. Read the FAQ carefully.

        2. "Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!"

        3. Describe your dataset. Use list to list data when you are doing so. Use input to type in your own dataset fragment that others can experiment with.

        4. Use the advanced editing options to appropriately format quotes, data, code and Stata output. The advanced options can be toggled on/off using the A button in the top right corner of the text editor.

        Comment

        Working...
        X