Announcement

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

  • Extract Monthly date from Quarterly date

    I had posted a day ago to extract date from string. While Nick solved that, but it created another problem, i.e. monthly date cannot be extracted from quarterly date , see what I tried

    Code:
    clear
    input str7 date
    "1984 Q1" 
    "1984 Q2" 
    "1984 Q3" 
    "1984 Q4" 
    "1985 Q1" 
    "1985 Q2" 
    "1985 Q3" 
    "1985 Q4" 
    "1986 Q1" 
    "1986 Q2" 
    end
    gen q=quarterly(date, "YQ")
    gen ym=ym(year(q), month(q))
    format ym %tm
    However, the second command to extract month from q variable returns unexpected values which are not months as one would expect in different quarters.
    Regards
    --------------------------------------------------
    Attaullah Shah, PhD.
    Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
    FinTechProfessor.com
    https://asdocx.com
    Check out my asdoc program, which sends outputs to MS Word.
    For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

  • #2
    While playing around with the date functions, I found the following solution, if someone needs
    Code:
    gen date2 = dofq( q)
    
    gen ym=ym(year(  date2), month(  date2))
    format ym %tm
    tsset  ym, monthly
    Regards
    --------------------------------------------------
    Attaullah Shah, PhD.
    Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
    FinTechProfessor.com
    https://asdocx.com
    Check out my asdoc program, which sends outputs to MS Word.
    For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

    Comment


    • #3
      The output of help datetime advises us that dofq() converts a quarterly date to a (daily) date and mofd() converts a (daily) date to a monthly date, so
      Code:
      gen ym=mofd(dofq(q))
      should convert a quarterly date to a monthly date, as indeed the following test on the sample data demonstrates.
      Code:
      . gen q=quarterly(date, "YQ")
      
      . format q %tq
      
      . gen ym=mofd(dofq(q))
      
      . format ym %tm
      
      . list, clean
      
                date        q        ym  
        1.   1984 Q1   1984q1    1984m1  
        2.   1984 Q2   1984q2    1984m4  
        3.   1984 Q3   1984q3    1984m7  
        4.   1984 Q4   1984q4   1984m10  
        5.   1985 Q1   1985q1    1985m1  
        6.   1985 Q2   1985q2    1985m4  
        7.   1985 Q3   1985q3    1985m7  
        8.   1985 Q4   1985q4   1985m10  
        9.   1986 Q1   1986q1    1986m1  
       10.   1986 Q2   1986q2    1986m4

      Comment


      • #4
        It's not clear what the real problem is here. Suppose Attaullah has quarterly dates, meaning that the date increases by 1 each quarter.

        William's solution is what he asked for and correctly gives the equivalent monthly dates, which are the first month in the each quarter, and thus increase by 3 each quarter.

        For most analytical purposes, they are no better than the original quarterly dates, and indeed a little more awkward, as you must explain the gaps to tsset or xtset using delta().

        Note that if you merely wanted to see
        months on quarterly dates, then a display format change would suffice:

        Code:
        . di %tq 100
        1985q1
        
        . di %tq_m_CY 100
          Jan 1985



        Comment

        Working...
        X