Announcement

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

  • Quarter variable in string format

    Hi everyone,

    I have this quarter variable that looks like "q11962" but it is in a string format. How can I turn it into a stata quarter variable? Following is a sample of how the variable looks like: the variable quarter is in string format.

    Many thanks!!

    quarter GDP
    q11961 342,687
    q11962 374,518
    q11963 392,297
    q11964 423,930
    q11965 445,966
    q11966 476,611
    q11967 491,587
    q11968 509,913
    q11969 545,634
    q11970 565,257


  • #2
    Thanks for the data example. You have the ingredients you need. There is some minor trickery in selecting them in a form that quarterly() or yq() can understand. Here are two ways to do it, but one is enough. String data are easier to work with using dataex.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str6 quarter
    "q11961"
    "q11962"
    "q11963"
    "q11964"
    "q11965"
    "q11966"
    "q11967"
    "q11968"
    "q11969"
    "q11970"
    end
    
    gen wanted1 = yq(real(substr(quarter, -4, 4)), real(substr(quarter, 2, 1)))
    gen wanted2 = quarterly(substr(quarter, 2, 1) + " " + substr(quarter, -4, 4), "QY")
    
    format wanted* %tq 
    
    list 
    
         +-----------------------------+
         | quarter   wanted1   wanted2 |
         |-----------------------------|
      1. |  q11961    1961q1    1961q1 |
      2. |  q11962    1962q1    1962q1 |
      3. |  q11963    1963q1    1963q1 |
      4. |  q11964    1964q1    1964q1 |
      5. |  q11965    1965q1    1965q1 |
         |-----------------------------|
      6. |  q11966    1966q1    1966q1 |
      7. |  q11967    1967q1    1967q1 |
      8. |  q11968    1968q1    1968q1 |
      9. |  q11969    1969q1    1969q1 |
     10. |  q11970    1970q1    1970q1 |
         +-----------------------------+

    Comment


    • #3
      I can't thank you enough Nick! This is very helpful!

      Comment

      Working...
      X