Announcement

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

  • Converting a string date into a tsset format

    Hi, I'm trying to covert a string time variable e.g. "25/08/2015 18:19" into a form that I can set in a time series? any help would be greatly appreciated.

    I have tried to use gen newvarname = clock(var1, "YMDhm") but i get a type mismatch error.

    Cheers,
    Daniel

  • #2
    The example you have shown has a format of
    Code:
    gen newvarname = clock(var1, "DMYhm")
    Regards
    --------------------------------------------------
    Attaullah Shah, PhD.
    Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
    FinTechProfessor.com
    https://asdocx.com
    Check out my asdoc program, which sends outputs to MS Word.
    For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

    Comment


    • #3
      I cannot understand that that command gives you a type mismatch error, assuming that var1 is actually the variable that contains "25/08/2015 18:19". When I do it on my setup, I get a missing value output, because your second argument to clock() does not accord with the actual arrangement of the information you show. But type mismatch? What did you actually type? Show us the actual command you typed and Stata's actual output. Also show us the result of -des var1-.

      Anyway, to convert your string time variable to a Stata internal datetime variable that will work with -tsset-, do:

      Code:
      gen double  newvarname = clock(var1, "DMYhm")
      format newvarname %tc
      tsset newvarname
      I put double in bold italics because it is absolutely crucial that you include it. Without that, newvarname will be created as a float (the default type for new variables, unless you override it). But a float does not have enough bits to contain a date time variable with the necessary precision. If you omit the -double- from the command you will find that the time portion of your variable is inexact.

      Added: crossed with #2.
      Last edited by Clyde Schechter; 24 May 2016, 21:37.

      Comment


      • #4
        Oh, I missed the type double
        Regards
        --------------------------------------------------
        Attaullah Shah, PhD.
        Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
        FinTechProfessor.com
        https://asdocx.com
        Check out my asdoc program, which sends outputs to MS Word.
        For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

        Comment


        • #5
          ignore the prior issue, you were correct in that what I was trying to do was not actually in var1, sorry.

          I got the function to work following the advice above but got to the same point Clyde mentioned, that it generates a missing value output. I used the other functions provided to tsset it.

          gen double eventtime = clock(var2, "YMDhm")
          format eventtime %tc
          tsset eventtime

          What can I do to convert this and not generate a missing value output?

          Cheers,
          Daniel

          Comment


          • #6
            Please look at the code posted in #3. Follow it carefully: pay attention to the second argument of clock(). "YMDhm" is not a correct description of your data. You need "DMYhm".

            Comment


            • #7
              Oh yes. That works. Thank you very much for you help.

              Comment

              Working...
              X