Announcement

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

  • Easy: Converting string time to Stata time - Sorry

    I am trying to convert my String Time (modified_date) which is formatted in:

    2015-10-28 11:09:47.000

    I would like to convert it into stata time
    In the interim I would like to generate a new variable to make sure I don't mess up my original data base

    these are my commands


    1. encode modified_date, gen (Statadate_DMY)
    2. date (Statadate_DMY, "YMDhms")
    3. format Statadate_DMY %td


    For

    1: I want to use the data in modified_date to be transferred into my new variable Statadate_DMY
    2. Telling Stata to convert the string time into Stata time
    3. Telling stata to convert the stata time into human readable time

    It doesn't seem to work - I do get a blue value, but the format is %23.0g

    Apologies I just started using stata....sorry if it's a silly question

  • #2
    Code:
    gen datetime = clock(modified_date,"YMDhms")
    format datetime %tc
    
    gen date = date(substr(modified_date,1,10),"YMD")
    format date %td
    Last edited by Øyvind Snilsberg; 13 Jan 2022, 08:34.

    Comment


    • #3
      Dear Oyvind, Call you please can you explain this:

      gen date = date(substr(modified_date,1,10),"YMD") ?

      Comment


      • #4
        This change to #2 is crucial

        Code:
         
         gen double datetime = clock(modified_date,"YMDhms")
        Wisdom Please If that is your real name, there you go. Otherwise https://www.statalist.org/forums/help#realnames and #3 of https://www.statalist.org/forums/help#adviceextras spell out our firm and explicit request to use a full real name.

        You're asked to read through the FAQ Advice before posting.

        Code:
        help datetime
        is your resource, and explains, for example, that date() yields a numeric daily date variable.

        substr() is a string function that extracts a substring.

        I can't follow all of what you did, as for example

        Code:
        date (Statadate_DMY, "YMDhms")
        is not a complete Stata command and would fail. However, for reasons that could be explained at length encode is not a remedy for fixing strings to dates or times, but as implied the functions documented under the help just given are crucial.

        Comment


        • #5
          substr(modified_date,1,10) extracts the ten first characters of modified_date, i.e., 2015-10-28, and date(substr(modified_date,1,10),"YMD") = date(2015-10-28,"YMD") yields the numerically encoded Stata date.

          Comment


          • #6
            Another way of getting the date variable is to extract it directly from datetime after the latter has been computed.
            Code:
            gen date = dofc(datetime)
            format date %td
            Added: Just to emphasize, it is essentially never correct to create a Stata date or datetime variable using -encode-.

            Comment

            Working...
            X