Announcement

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

  • Changing a time variable defined as a string

    Dear Stata users, hello;

    I'm working with this data currently:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str7 periode float dettepubliqueendupib
    "2000-T1"  60.5
    "2000-T2"  60.4
    "2000-T3"  59.8
    "2000-T4"  59.7
    "2001-T1"  58.7
    "2001-T2"  59.6
    "2001-T3"  59.7
    "2001-T4"  59.3
    "2002-T1"  59.6
    "2002-T2"  60.9
    "2002-T3"  60.7
    "2002-T4"  61.3
    "2003-T1"  62.7
    "2003-T2"  64.9
    "2003-T3"  65.4
    "2003-T4"  65.4
    "2004-T1"  65.9
    "2004-T2"  67.3
    "2004-T3"    67
    "2004-T4"  66.9
    "2005-T1"  67.8
    "2005-T2"  68.2
    "2005-T3"  67.9
    "2005-T4"  68.2
    "2006-T1"  68.1
    "2006-T2"    67
    "2006-T3"  66.9
    "2006-T4"  65.4
    "2007-T1"  66.5
    "2007-T2"  67.4
    "2007-T3"  66.8
    "2007-T4"  65.5
    "2008-T1"  66.8
    "2008-T2"  67.3
    "2008-T3"  67.8
    "2008-T4"  69.8
    "2009-T1"  74.7
    "2009-T2"  79.8
    "2009-T3"  82.5
    "2009-T4"  84.1
    "2010-T1"  85.9
    "2010-T2"  88.3
    "2010-T3"  86.4
    "2010-T4"  86.3
    "2011-T1"  87.7
    "2011-T2"  89.2
    "2011-T3"  88.4
    "2011-T4"  88.7
    "2012-T1"  91.2
    "2012-T2"  92.6
    "2012-T3"  91.3
    "2012-T4"  91.7
    "2013-T1"  93.3
    "2013-T2"  94.8
    "2013-T3"    94
    "2013-T4"  94.5
    "2014-T1"  96.1
    "2014-T2"  96.7
    "2014-T3"  96.5
    "2014-T4"  96.1
    "2015-T1"    98
    "2015-T2"  98.2
    "2015-T3"  97.7
    "2015-T4"  96.9
    "2016-T1"  98.8
    "2016-T2"  99.9
    "2016-T3"  99.1
    "2016-T4"  98.1
    "2017-T1" 100.8
    "2017-T2" 101.2
    "2017-T3" 100.2
    "2017-T4"  98.7
    "2018-T1"  99.1
    "2018-T2"  99.1
    "2018-T3"  99.3
    "2018-T4"  98.5
    "2019-T1"  99.3
    "2019-T2"    99
    "2019-T3"  99.9
    "2019-T4"  98.1
    "2020-T1" 101.2
    "2020-T2"   113
    "2020-T3" 115.2
    "2020-T4" 114.8
    "2021-T1" 117.7
    "2021-T2" 114.5
    "2021-T3" 115.5
    "2021-T4" 112.7
    "2022-T1" 114.2
    "2022-T2" 112.8
    "2022-T3"   113
    "2022-T4" 111.2
    "2023-T1" 111.9
    "2023-T2" 111.2
    "2023-T3"   111
    "2023-T4" 109.9
    "2024-T1" 110.5
    "2024-T2"   112
    end
    The data describes the evolution of publc debt of a country from the first year quarter of 2000 to the second year quarter of 2024. The first variable is the time variable defined as "year-quarter", and the second variable is the value of the public debt. As you can see, the "periode" time variable is defined as a string, but I want to change it in a way that I could apply the tsset command on it and treat it as a time variable defined as quarters of the years.

    Any help please?

    With many thanks!

  • #2
    Code:
    gen qdate= quarterly(periode, "YQ")
    format qdate %tq
    tsset qdate
    Res.:

    Code:
    . l in 1/10
    
         +-----------------------------+
         | periode   dettep~b    qdate |
         |-----------------------------|
      1. | 2000-T1       60.5   2000q1 |
      2. | 2000-T2       60.4   2000q2 |
      3. | 2000-T3       59.8   2000q3 |
      4. | 2000-T4       59.7   2000q4 |
      5. | 2001-T1       58.7   2001q1 |
         |-----------------------------|
      6. | 2001-T2       59.6   2001q2 |
      7. | 2001-T3       59.7   2001q3 |
      8. | 2001-T4       59.3   2001q4 |
      9. | 2002-T1       59.6   2002q1 |
     10. | 2002-T2       60.9   2002q2 |
         +-----------------------------+

    Comment


    • #3
      Andrew Musau Thanks for the help! It worked.

      Comment


      • #4
        In short, quarterly() works fine even though your string variable does not include q or Q. But why does it work?

        One optimistic idea is that the function is smart enough to recognise T (presumably meaning here "trimestre") as an acceptable abbreviation (in French) indicating quarter.

        In English too, "trimester" has the meaning of a three-month period, but to my understanding as referring most often to the first, second, and third three months of a pregnancy lasting nine months or so. So, that meaning has nothing to do with quarters of a calendar year.

        Some experiments indicate otherwise. quarterly() is just ignoring non-numeric text, not being smart about what that text is. For example, quarterly("2000-M5", "YQ") would yield missing as quarter 5 isn't defined. But quarterly("2000-M4", "YQ") would yield the same result as quarterly("2000-Q4", "YQ") or quarterly("2000-T4", "YQ").

        Comment


        • #5
          Nick Cox Thanks for the response. Yes, I do mean quarters of the year, the group of three months. You are right, the word "trimester" is not that very much used, even more in a professional or academic context. But here, in the case of my data, it has nothing to do with pregnancy.

          Comment

          Working...
          X