Announcement

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

  • Trucating date in Stata

    Hello Stata Users,

    i'm looking for a way to represent my dates so that i can have a panel on monthly basis. my dates are a string of both (year,month, day and time). Do i need to truncate it to have just a date without time and if so is anyone having a stata code to do this? here is a sample representation.

    __submission_time
    2017-01-31T04:29:21
    2017-01-31T04:29:22
    2017-01-31T04:29:23
    2017-01-31T10:08:40
    2017-01-31T10:08:43

    Thanks in advance

  • #2
    Please use dataex as requested (FAQ Advice #12).

    Comment


    • #3
      Sorry for C/P production. thanks

      input str19 __submission_time
      "2017-09-11T21:09:36"
      "2017-09-11T21:09:39"
      "2017-09-11T21:09:41"
      "2017-09-11T21:09:43"
      "2017-09-11T21:09:46"
      "2017-09-11T21:09:48"


      Comment


      • #4
        Thanks for the data example. Please see how best to present it below.

        Here are three different ways to get daily dates out of your kind of data.

        Code:
        clear 
        input str19 __submission_time
        "2017-09-11T21:09:36"
        "2017-09-11T21:09:39"
        "2017-09-11T21:09:41"
        "2017-09-11T21:09:43"
        "2017-09-11T21:09:46"
        "2017-09-11T21:09:48"
        end 
        
        gen dailydate1 = daily(substr(__submission_time, 1, 10), "YMD") 
        gen dailydate2 = dofc(clock(__submission_time, "YMD#hms")) 
        
        * numdate is from SSC
        * https://www.statalist.org/forums/forum/general-stata-discussion/general/1308227-numdate-available-on-ssc-program-to-generate-numeric-date-variables
        numdate daily dailydate3 = __submission_time, pattern(YMD#hms) 
        
        list, sep(0) 
        
        
             +-------------------------------------------------------+
             |   __submission_time   dailyd~1   dailyd~2   dailyda~3 |
             |-------------------------------------------------------|
          1. | 2017-09-11T21:09:36      21073      21073   11sep2017 |
          2. | 2017-09-11T21:09:39      21073      21073   11sep2017 |
          3. | 2017-09-11T21:09:41      21073      21073   11sep2017 |
          4. | 2017-09-11T21:09:43      21073      21073   11sep2017 |
          5. | 2017-09-11T21:09:46      21073      21073   11sep2017 |
          6. | 2017-09-11T21:09:48      21073      21073   11sep2017 |
             +-------------------------------------------------------+
        Of these only numdate by default assigns a daily date display format. Follow either with the others with something like

        Code:
        format dailydate1 %td
        To get a monthly date, push any of these through mofd() or just work on the first seven characters, i.e. substr(__submission_time, 1, 7)

        Comment


        • #5
          Thanks Nick,

          This has been of great help. I appreciate alot.

          Comment

          Working...
          X