Announcement

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

  • Converting time to string

    Hi All,

    I have a time variable that is stored as a double and display format is %td. How can I convert it to string ?

    Thanks.

  • #2
    Stephen, if it is indeed the time variable, then the format is probably %tc.
    Regardless, just use the string function with an appropriate format, as in this example:

    Code:
    generate s = string(t,"%td")
    Best, Sergiy Radyakin

    Comment


    • #3
      Hi Sergiy,

      It isn't a time variable i.e its a numeric variable with time display format.

      ------------------------------------------------------------------------------------------------------------------
      storage display value
      variable name type format label variable label
      ------------------------------------------------------------------------------------------------------------------
      SubmissionDate double %tc..
      starttime double %tc..
      ------------------------------------------------------------------------------------------------------------------

      Thanks.

      Comment


      • #4
        That's exactly what I mean:
        time=%tc
        date=%td
        (all are numeric).

        Compare to your first email where you write time variable and format %td.

        In your case

        generate s = string(starttime,"%tc") will create a string variable s with the same content as Stata shows you for your starttime variable.

        Best, Sergiy

        Comment


        • #5
          Thanks Sergiy,
          It worked. Appreciated!

          ​And which syntax would convert it to time storage type?

          Comment


          • #6
            There is no such storage type. There are precisely two ways of holding dates in Stata, numeric and string, and you have spanned the range. You started with a numeric variable holding date information and converted it to a string variable. There is no third way.

            This is all documented.

            Code:
             
            help dates and times 
            help data types

            Comment


            • #7
              Thanks Nick. And for numeric storage type, how can extract the day,month, etc........

              Comment


              • #8
                The answer is again in

                Code:
                help dates and times
                As the FAQ Advice signals

                3. Before you post

                Before posting, consider other ways of finding information:
                • the online help for Stata
                • Stata's search command, which can tell you about all built-in Stata commands, all ado-files published in the Stata Journal, all FAQs on the Stata website, www.stata.com, and user-written Stata programs available on the Internet (if you have Stata 12 or earlier, you can use findit to search all these sources at once)
                • the manuals, accessible in .pdf form to all
                4. Elementary questions?

                Beginners' questions are not out of order, but very basic or elementary questions that you should be able to answer for yourself will get little or no reply. There is an underlying expectation that you have tried to read the documentation or basic literature at your level. So, Statalist is not for questions on how to do regression in Stata, the difference between means and medians, or even what instrumental variables are. If you are in doubt about where to draw the line, look at previous questions. If you get this wrong, you are unlikely to get flamed. You just may not get an answer.
                Last edited by Nick Cox; 02 Mar 2015, 06:22.

                Comment


                • #9
                  Thanks Nick. Appreciated!

                  Comment

                  Working...
                  X