Announcement

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

  • Formatting Quarterly-Date Variable

    Hello all,

    I have a quarterly date variable in the form of 2019q1, 2019q2, 2019q3, 2019q4, 2020q1 and so on. I would like to format the variable in such a way that the the q is capitalized. So for example, this formatted variable will look like 2019Q1, 2019Q2, 2019Q3, 2019Q4, 2020Q1 and so on. My quarterly date variable is a string (str6). Can someone help me out?

    Thanks

  • #2
    If your quarterly date variable is a string, the first step is to convert it to a Stata numeric quarterly date variable.

    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.

    With that said, here's some example code that may start you in a useful direction.
    Code:
    . input str6 qtr_str
    
           qtr_str
      1. 2019q1
      2. 2019q2
      3. 2019q3
      4. 2019q4
      5. 2020q1
      6. end
    
    . generate qtr = quarterly(qtr_str,"YQ")
    
    . list, clean
    
           qtr_str   qtr  
      1.    2019q1   236  
      2.    2019q2   237  
      3.    2019q3   238  
      4.    2019q4   239  
      5.    2020q1   240  
    
    . format %tq qtr
    
    . list, clean
    
           qtr_str      qtr  
      1.    2019q1   2019q1  
      2.    2019q2   2019q2  
      3.    2019q3   2019q3  
      4.    2019q4   2019q4  
      5.    2020q1   2020q1  
    
    . format %tqCCYY!Qq qtr
    
    . list, clean
    
           qtr_str      qtr  
      1.    2019q1   2019Q1  
      2.    2019q2   2019Q2  
      3.    2019q3   2019Q3  
      4.    2019q4   2019Q4  
      5.    2020q1   2020Q1  
    
    . describe
    
    Contains data
     Observations:             5                  
        Variables:             2                  
    ------------------------------------------------------------------------------------------------
    Variable      Storage   Display    Value
        name         type    format    label      Variable label
    ------------------------------------------------------------------------------------------------
    qtr_str         str6    %9s                   
    qtr             float   %tqCCYY!Qq            
    ------------------------------------------------------------------------------------------------
    Sorted by: 
         Note: Dataset has changed since last saved.
    
    .

    Comment


    • #3
      Alright thanks William, I will take at the User guide

      Comment

      Working...
      X