Announcement

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

  • Dates in Stata

    I am trying convert some dates in quarters. I am doing this by going into the variables manager and changing the format to quarters, as seen in the screenshot I've attached. When I do that though it is still in the Stata format (eg. 6973-Q1), instead of a format I can work with (eg. 2021q1) What am I doing wrong?

    Attached Files

  • #2
    Data example with dataex please! I refuse to offer any code until I see a minimal worked example. Please, do see the FAQ (especially section 12 and after, or this page) so you can see how to present your data and code below.

    Oh, and just so you know, no screenshots please. They don't help us, and they don't help us help you. If nobody's yet said it to you, welcome to Statalist.

    Comment


    • #3
      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.

      When you've done that reading, this example will perhaps be understandable.
      Code:
      . generate qtr = qofd(day)
      
      . format %tq qtr
      
      . list, clean noobs
      
                day      qtr  
          31mar2021   2021q1

      Comment


      • #4
        Advice in #2 and #3 is all to the point. William Lisowski is right: some reading around is needed here.

        But it's not absolutely clear what your situation is -- because you don't give a full data example. The example you give suggests to me that your quarterly dates are integers like 20052 for 2005 Q 2. Here I use display or di on individual values, which is a good tactic for working out what is going on and what to do.


        Code:
        . di yq(6973, 1)
        20052
        If that is so, you won't get a better answer by using a different display format and even treating the variable as string won't help.

        Code:
        . di quarterly("20052", "YQ")
        .
        The resolution is different. to split an integer like 20052 into components. 2005 and 2

        Code:
        . di yq(floor(20052/10), mod(20052, 10))
        181
        
        . di %tq yq(floor(20052/10), mod(20052, 10))
        2005q2
        Suppose your existing variable is called date. Then a proper Stata quarterly date variable would be


        Code:
        gen qdate = yq(floor(date/10), mod(date, 10))
        format qdate %tq
        A key principle here is that assigning a date format will only work if the numeric values already match what you want. It's a common fallacy that changing the display format is a way of converting one kind of date variable to another. and it's certainly not a way to make sense of idiosyncratic dates, regardless of how obvious they look to people.


        https://www.stata-journal.com/articl...article=dm0067 says more.


        Comment


        • #5
          Also https://www.stata-journal.com/articl...article=dm0096

          Comment

          Working...
          X