Announcement

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

  • Double to date converting issues

    Dear Statalisters,

    I have in my dataset a double variable that contains date as following 9/15/2015 8:50.

    I would like to have it in date format. I proceeded like this :

    Code:
    gen new_date=clock(original_double,"MDY hm")
    Instead of clock, I also used tc and date functions, but without any success...

    Thank you for your kind help

    P.S I've also tried : "MM/DD/YYYY hh:mm" but this also didn't work


  • #2
    I would also like to extract day, month and year from new_date...

    Thanks a lot for your help, I'm a bit desperate, I've been trying different things for a while now

    Comment


    • #3
      The code below does not help either

      Code:
      generate new_=clock(string(original_double,"%10.0g"),"MDYhm")

      Comment


      • #4
        Dear Dusan,

        -clock()- requires your original date variable to exist as a string. For example,

        Code:
        input str18 date
        "9/15/2015 8:50"
        end
        gen double newdate = clock(date,"MDY hm")
        format %tc newdate
        list
        You may also find this blog on datetime useful.

        Comment


        • #5
          Thank you verymuch Ashish!

          Another issue that I'm having is that when I convert original_double to string, I need to use the force option because STATA returns : "Loss of information" message... What should I do about this?

          Thanks again

          Comment


          • #6
            You will want to follow the guidance in help datetime, which is without a doubt the most visited documentation on my system, with the second-most-visited being Chapter 24 (Working with dates and times) of the Stata User's Guide PDF available from the PDF Documentation item on Stata's Help menu. Before working with dates and times, any Stata user should read the very detailed Chapter 24 thoroughly. After that, the help documentation will usually be enough to point the way. Some people may be able to remember everything without have to continually refer to the documentation, but I for one am not such a person.

            With that said, in help datetime you want to see the section on SIF-to-SIF conversion, which suggests that the following will start you on your way.
            Code:
            generate new_date = dofc(original_double)
            format new_date %td

            Comment


            • #7
              Thanks a lot William! This is exactly what I needed!
              Best

              Dusan

              Comment

              Working...
              X