Announcement

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

  • Date format

    Hi,

    I would like to convert to a standard date format a variable currently in this format:
    Thursday, May 02, 2019

    Any help would be greatly appreciated!

    Thanks!

  • #2
    You may fiddle with something like: format myvar %tdDay,_Month,_dd,_CCYY
    Last edited by Marcos Almeida; 02 May 2019, 15:21.
    Best regards,

    Marcos

    Comment


    • #3
      I think you mean that your current variable is a string variable and you want to convert it to a numeric Stata Internal Format daily variable. If so, this should start you in the right direction.
      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str22 date_s
      "Thursday, May 02, 2019"
      end
      
      generate date = daily(date_s,"#MDY")
      format date %td
      list, noobs
      Code:
      . list, noobs
      
        +------------------------------------+
        |                 date_s        date |
        |------------------------------------|
        | Thursday, May 02, 2019   02may2019 |
        +------------------------------------+
      The key is using "#" in the input mask to skip over the day of the week. Its use is described in the output of help datetime translation.

      But, if what I did is completely unfamiliar to you, then the following advice may help. Apologies if you're already beyond that point and just didn't know how to construct the input mask.

      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 (since version 11) and are accessible from within Stata - for example, through the PDF Documentation section of Stata's Help menu.

      Added in edit: my post crossed with that of Marcos; we obviously have much different ideas from your description of what you are trying to do. To improve future posts, please take a few moments to review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. Note especially sections 9-12 on how to best pose your question. It's particularly helpful to copy commands and output from your Stata Results window and paste them into your Statalist post using code delimiters [CODE] and [/CODE], and to use the dataex command to provide sample data, as described in section 12 of the FAQ.

      The more you help others understand your problem, the more likely others are to be able to help you solve your problem.

      Comment


      • #4
        Thanks for your answer Marcos!

        I'm trying to get it to a more readable format that would be something like 02may19, or 02/05/2019. Would you know how to generate a new variable in this format, from the date format I currently have?

        Thanks a lot for your help.

        Comment


        • #5
          That is exactly what I needed, thank you! And thank you for the detailed explanation, it is very useful. I had checked the stata guide but was stuck - I missed the #.

          All good now!

          Comment

          Working...
          X