Announcement

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

  • How can I calculate the moving annual total?

    Hello,

    I want to calculate the moving annual total for a data set.
    The data is given by country, category, and date (month and year) and I need to calculate the Moving Annual Total by country and category for each month.
    . Whenever the value for a certain entry is zero then the column is simply not reported, so I can't just sort the data by country&category&date and sum up the previous 11 month' values plus the current month's value.
    I've tried this command, which doesn't work:
    rangestat MAT_GBP=Value, interval(date -11 0) by(country category)
    Country Category Value Year Month Date
    Austria Cat_1 753157 2018 January January 2018
    Austria Cat_1 356991 2018 March March 2018
    Austria Cat_1 405159 2018 September September 2018
    Austria Cat_1 567725 2019 May May 2019
    Austria Cat_1 466591 2019 November November 2019
    Austria Cat_2 194156 2018 February February 2018
    Austria Cat_2 359657 2018 March March 2018
    Austria Cat_2 596758 2018 November November 2018
    Austria Cat_2 594976 2018 December December 2018
    Austria Cat_2 731195 2019 January January 2019
    Austria Cat_2 687740 2019 June June 2019
    Austria Cat_2 160119 2019 September September 2019
    Austria Cat_2 253241 2019 November November 2019
    Austria Cat_2 372132 2020 January January 2020


  • #2
    What does "doesn't work" mean here please?

    Your data example is ambiguous. Please show the result of

    Code:
    su Date 
    describe Date 
    list Date in 1/10, nolabel
    Alternatively, please use dataex as requested and explained at https://www.statalist.org/forums/help#stata -- which is particularly important for any date variables.

    Comment


    • #3
      Code:
      ssc install asrol
      
      * Create monthly date. This is fake, you should use actual
      
      gen mdate = _n+440
      format mdate %tm
      
      asrol value, window(date 12) by(country category) gen(MAT_GBP) stat(total)
      
           +-----------------------------------------------------------------------------+
           | country   category    value   year       month     date     mdate   MAT_GBP |
           |-----------------------------------------------------------------------------|
        1. | Austria      Cat_1   753157   2018     January   Jan-18   1996m10    753157 |
        2. | Austria      Cat_1   356991   2018       March   Mar-18   1996m11   1110148 |
        3. | Austria      Cat_1   405159   2018   September   Sep-18   1996m12   1515307 |
        4. | Austria      Cat_1   567725   2019         May   May-19    1997m1   2083032 |
        5. | Austria      Cat_1   466591   2019    November   Nov-19    1997m2   2549623 |
           |-----------------------------------------------------------------------------|
        6. | Austria      Cat_2   194156   2018    February   Feb-18    1997m3    194156 |
        7. | Austria      Cat_2   359657   2018       March   Mar-18    1997m4    553813 |
        8. | Austria      Cat_2   596758   2018    November   Nov-18    1997m5   1150571 |
        9. | Austria      Cat_2   594976   2018    December   Dec-18    1997m6   1745547 |
       10. | Austria      Cat_2   731195   2019     January   Jan-19    1997m7   2476742 |
           |-----------------------------------------------------------------------------|
       11. | Austria      Cat_2   687740   2019        June   Jun-19    1997m8   3164482 |
       12. | Austria      Cat_2   160119   2019   September   Sep-19    1997m9   3324601 |
       13. | Austria      Cat_2   253241   2019    November   Nov-19   1997m10   3577842 |
       14. | Austria      Cat_2   372132   2020     January   Jan-20   1997m11   3949974 |
           +-----------------------------------------------------------------------------+
      Since your query is not clear, you may identify calculations / code that do not match your expectations.
      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

      Working...
      X