Announcement

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

  • Issue with date and time

    Hi Everyone,
    I have a time series data and I want to generate two variables, unable to generate these two variables.

    code for the first variable is gen date1=date(start,"DMY")
    format date1 %td
    code for the first variable is gen double time=clock(start,"DMYhm")
    format time %tc

    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str14 start
    "16/06/23 09:54"
    "16/06/23 10:07"
    "16/06/23 10:11"
    "16/06/23 10:15"
    "16/06/23 10:20"
    "16/06/23 10:25"
    "16/06/23 10:30"
    "16/06/23 10:34"
    "16/06/23 10:40"
    "16/06/23 10:47"
    "16/06/23 10:52"
    "16/06/23 10:55"
    "16/06/23 11:11"
    "16/06/23 11:17"
    "16/06/23 11:22"
    "16/06/23 11:29"
    "16/06/23 11:34"
    "16/06/23 11:38"
    "16/06/23 11:39"
    "16/06/23 11:52"
    "16/06/23 12:01"
    "16/06/23 12:07"
    "16/06/23 12:18"
    "16/06/23 12:20"
    "16/06/23 12:27"
    "16/06/23 12:43"
    "16/06/23 12:49"
    "16/06/23 13:00"
    "16/06/23 13:03"
    "16/06/23 13:17"
    "16/06/23 13:19"
    "16/06/23 13:22"
    "16/06/23 13:24"
    "16/06/23 13:26"
    "16/06/23 14:01"
    "16/06/23 14:16"
    "16/06/23 14:22"
    "16/06/23 14:25"
    "16/06/23 13:46"
    "16/06/23 13:50"
    "16/06/23 14:27"
    "16/06/23 14:30"
    "16/06/23 14:32"
    "16/06/23 13:59"
    "16/06/23 14:34"
    "16/06/23 14:08"
    "16/06/23 14:10"
    "16/06/23 14:42"
    "16/06/23 14:45"
    "16/06/23 14:51"
    "16/06/23 14:49"
    "16/06/23 14:53"
    "16/06/23 14:50"
    "16/06/23 14:59"
    "16/06/23 15:05"
    "16/06/23 15:07"
    "16/06/23 15:12"
    "16/06/23 15:15"
    "16/06/23 15:07"
    "16/06/23 15:11"
    "16/06/23 15:19"
    "16/06/23 15:25"
    "16/06/23 15:30"
    "16/06/23 15:33"
    "16/06/23 15:34"
    "16/06/23 15:54"
    "16/06/23 15:36"
    "16/06/23 15:57"
    "16/06/23 16:01"
    "16/06/23 15:41"
    "16/06/23 15:45"
    "16/06/23 16:04"
    "16/06/23 15:47"
    "16/06/23 15:52"
    "16/06/23 15:55"
    "16/06/23 16:07"
    "16/06/23 16:08"
    "16/06/23 16:10"
    "16/06/23 16:11"
    "16/06/23 16:12"
    "16/06/23 16:13"
    "16/06/23 16:14"
    "16/06/23 16:31"
    "16/06/23 16:20"
    "16/06/23 16:21"
    "16/06/23 16:50"
    "16/06/23 16:26"
    "16/06/23 16:30"
    "16/06/23 16:54"
    "16/06/23 16:32"
    "16/06/23 16:57"
    "16/06/23 16:59"
    "16/06/23 16:33"
    "16/06/23 17:02"
    "16/06/23 16:35"
    "16/06/23 16:37"
    "16/06/23 17:07"
    "16/06/23 17:28"
    "16/06/23 16:39"
    "16/06/23 16:41"
    end
    [/CODE]


    Any help is highly appreciated

    Ashish
    Last edited by Ashish Bandhu; 17 Jun 2023, 02:02.

  • #2
    Dear Ashish,

    Possibly, this post is helpful.
    http://publicationslist.org/eric.melse

    Comment


    • #3
      Eric Melse Thanks for the mention, but the posts linked in #2 don't help here.

      Ashish Bandhu

      The starting point for date-time problems is always

      Code:
      help datetime
      and consulting that shows the problem with your syntax. You need to spell out to Stata which century you want. It's obvious enough to readers that year 23 means 2023, but Stata wants that to be explicit. Further, for the daily date, the clock time is to be ignored.

      Code:
      clear
      input str14 start
      "16/06/23 09:54"
      "16/06/23 10:07"
      "16/06/23 10:11"
      "16/06/23 10:15"
      "16/06/23 10:20"
      end 
      
      gen date1 = daily(start, "DM20Y #")
      format date1 %td 
      gen double time = clock(start, "DM20Y hm")
      format time %tc 
      
      list 
      
           +-------------------------------------------------+
           |          start       date1                 time |
           |-------------------------------------------------|
        1. | 16/06/23 09:54   16jun2023   16jun2023 09:54:00 |
        2. | 16/06/23 10:07   16jun2023   16jun2023 10:07:00 |
        3. | 16/06/23 10:11   16jun2023   16jun2023 10:11:00 |
        4. | 16/06/23 10:15   16jun2023   16jun2023 10:15:00 |
        5. | 16/06/23 10:20   16jun2023   16jun2023 10:20:00 |
           +-------------------------------------------------+
      
      .

      Comment


      • #4
        Even easier, I think, would be:
        Code:
        gen double time=clock(start,"DM20Yhm")
        format time %tc
        gen date1 = dofc(time)
        format date1 %td

        Comment

        Working...
        X