Announcement

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

  • Cumulative sum by group

    I have a dataset with grouped by a particular variable. The number of observations (rows) in each group ranges from 3 to 20. I want to first sort by group and date, and then perform a cumulative sum over one of the variables, but by group: In each group, I want to sum all previous values of the variable in that group, and then record this rolling or cumulative sum as another variable. I have no idea how to do this in Stata - please help.

  • #2
    Please study and act on http://www.statalist.org/forums/help#stata

    Code:
    . webuse grunfeld
    
    . bysort company (year) : gen cum_invest = sum(invest)
    
    . l company year *invest if inlist(company, 1, 2), sepby(company)
    
         +------------------------------------+
         | company   year   invest   cum_in~t |
         |------------------------------------|
      1. |       1   1935    317.6      317.6 |
      2. |       1   1936    391.8      709.4 |
      3. |       1   1937    410.6       1120 |
      4. |       1   1938    257.7     1377.7 |
      5. |       1   1939    330.8     1708.5 |
      6. |       1   1940    461.2     2169.7 |
      7. |       1   1941      512     2681.7 |
      8. |       1   1942      448     3129.7 |
      9. |       1   1943    499.6     3629.3 |
     10. |       1   1944    547.5     4176.8 |
     11. |       1   1945    561.2       4738 |
     12. |       1   1946    688.1     5426.1 |
     13. |       1   1947    568.9       5995 |
     14. |       1   1948    529.2     6524.2 |
     15. |       1   1949    555.1     7079.3 |
     16. |       1   1950    642.9     7722.2 |
     17. |       1   1951    755.9     8478.1 |
     18. |       1   1952    891.2     9369.3 |
     19. |       1   1953   1304.4    10673.7 |
     20. |       1   1954   1486.7    12160.4 |
         |------------------------------------|
     21. |       2   1935    209.9      209.9 |
     22. |       2   1936    355.3      565.2 |
     23. |       2   1937    469.9     1035.1 |
     24. |       2   1938    262.3     1297.4 |
     25. |       2   1939    230.4     1527.8 |
     26. |       2   1940    361.6     1889.4 |
     27. |       2   1941    472.8     2362.2 |
     28. |       2   1942    445.6     2807.8 |
     29. |       2   1943    361.6     3169.4 |
     30. |       2   1944    288.2     3457.6 |
     31. |       2   1945    258.7     3716.3 |
     32. |       2   1946    420.3     4136.6 |
     33. |       2   1947    420.5     4557.1 |
     34. |       2   1948    494.5     5051.6 |
     35. |       2   1949    405.1     5456.7 |
     36. |       2   1950    418.8     5875.5 |
     37. |       2   1951    588.2     6463.7 |
     38. |       2   1952    645.5     7109.2 |
     39. |       2   1953      641     7750.2 |
     40. |       2   1954    459.3     8209.5 |
         +------------------------------------+

    Comment


    • #3
      Thank you very much. I can do this kind of stuff in Python, R and Matlab etc, but Stata is a bit foreign to me..

      Comment


      • #4
        ...
        Last edited by Godfrey Smith; 28 Jul 2016, 22:49.

        Comment

        Working...
        X