Announcement

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

  • Formatting and managing dates, from String to MMYYYY format

    Dear All,
    I am a new user in Stata.
    I am having a basic question and would kindly ask for your help. I want to change the data format from string to the date format (MMYYYY). I have tried this formula date(Date2, "MY") and then created the monthly format (Date3). However, the format is not what I expected.
    Many thanks for your support,
    Chi
    Attached Files

  • #2
    As is documented the function date() is for producing daily dates, not what you want here. (The synonymous function daily() makes this even clearer. )

    The screenshot in this case is readable but please note the request in the FAQ Advice to use dataex

    This fragment shows some technique.

    Code:
    . clear
    
    . set obs 1
    number of observations (_N) was 0, now 1
    
    . gen Date2 = "10/2001"
    
    . gen Date3 = monthly(Date2, "MY")
    
    . format Date3 %tm
    
    . l
    
         +-------------------+
         |   Date2     Date3 |
         |-------------------|
      1. | 10/2001   2001m10 |

    Comment


    • #3
      Dear Nick Cox,
      Many thanks for your answer.
      Chi

      Comment


      • #4
        As you've seen, 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.

        Comment


        • #5
          Thanks you, William

          Comment

          Working...
          X