Announcement

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

  • Create time variable with year, month, date and hour

    Dear Stata users,

    I have data for every hour on every day and it looks the following way:
    01.01.1997 00:00
    01.01.1997 01:00
    ....

    Now I want to create a time variable that Stata recognizes to work with the data.

    I know that the code

    Code:
    gen modate=ym(year,month)
    format modate %tm
    can be used to create a month and year variable but I do not know how to adjust the code to include day and hours.
    I already looked at the help file but was still not able to get a solution.

    Thank you very much in advance for your comment.

  • #2
    if the format is day-month-year,
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str16 time
    "01.01.1997 00:00"
    "01.01.1997 01:00"
    end
    
    gen double wanted = clock(time,"DMYhm")
    format wanted %tc

    Comment


    • #3
      Mia Thovinelle -

      To the helpful example from Øyvind Snilsberg let me add some advice to make your work with Stata dates and times easier in the future.

      Stata's "date and time" variables are complicated and there is a lot to learn. If you have not already read the very detailed Chapter 24 (Working with dates and times) of the Stata User's Guide PDF, do so now. If you have, it's time for a refresher. After that, the help datetime documentation will usually be enough to point the way. You can't remember everything; even the most experienced users end up referring to the help datetime documentation or back to the manual for details. But at least you will get a good understanding of the basics and the underlying principles. An investment of time that will be amply repaid.

      All Stata manuals are included as PDFs in the Stata installation and are accessible from within Stata - for example, through the PDF Documentation section of Stata's Help menu.

      Comment

      Working...
      X