Announcement

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

  • sum over years of values

    hi,
    i would like to know how do i add up values of a variable over years?

    I have data from 1991 to 2014, I would like to create a variable that for 1991 takes the initial value given in 1991, then for 1992 takes the value of 1992 plus 1991, for 1993 takes the value 1991 plus 1992 plus 1993, etc

    i have the variables year, words (which is the one i want to add up) and code_id (17 different categories)

    year code_id words
    1991 2 33134
    1992 2 26410
    1993 2 65184
    1994 2 185471
    1995 2 54097
    1996 2 25953
    1997 2 54249
    1998 2 31665
    1991 3 7012
    1992 3 34918
    1993 3 36356
    1994 3 12202
    1995 3 5024
    1996 3 12674
    1997 3 52485
    1998 3 15931
    1991 4 44621
    1992 4 40430
    1993 4 54055
    1994 4 89413
    1995 4 54109
    1996 4 51612
    1997 4 45979
    1998 4 37916
    1991 12 51038
    1992 12 39969
    1993 12 48485
    1994 12 235801
    1995 12 45539
    1996 12 47991
    1997 12 27825
    1998 12 75286
    1991 13 10788
    1992 13 4576
    1993 13 14237
    1994 13 15108
    1995 13 9705
    1996 13 11645
    1997 13 23008
    1998 13 21860
    1991 14 3874
    1992 14 980
    1993 14 8960
    1994 14 12986
    1995 14 56786
    1996 14 19230
    1997 14 3159
    1998 14 6880
    1991 15 16792
    1992 15 33096
    1993 15 26275
    1994 15 55868
    1995 15 31947
    1996 15 16578
    1997 15 28570
    1998 15 23513
    1991 16 7118
    1992 16 12603
    1993 16 32021
    1994 16 18391
    1995 16 16255
    1996 16 15329
    1997 16 5801
    1998 16 7191

    thanks for the help!

  • #2
    Code:
    bysort code_id (year) : gen wanted = sum(words)
    assuming that you want summation separately by code_id.

    Comment


    • #3
      Your question is unclear as to the role of code_id in this. Do you want separate running sums over the years for each code_id, or do you want the running sum to ignore code_id and just give you cumulative yearly totals across all values of code_id?

      If the former:
      Code:
      by code_id (year), sort: gen wanted = sum(words)
      If the latter:
      Code:
      by year (code_id), sort: egen yearly_total = total(words)
      by code_id (year), sort: gen wanted = sum(yearly_total)
      Added: Crossed with #2 which expresses similar thoughts.

      By the way, in the future, when showing data examples, please use the -dataex- command to do so. If you are running version 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.



      Last edited by Clyde Schechter; 19 Sep 2019, 18:04.

      Comment


      • #4
        thanks for your answers,

        this worked just fine! : bysort code_id (year) : gen wanted = sum(words)

        Comment

        Working...
        X