Announcement

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

  • How to generate cumulative sum of changes of a variable in a group across time

    Dear All,

    I'm new on Statalist and I'm still figuring out how to use dataex so apologies for my dataset example below. reporter_iso and partner_iso are string variables. Meanwhile, id is generated by egen=group(reporter_iso partner_iso). I need to generate the variable "sum" which cumulatively adds up the changes in "TA_envi_tot" across reporter-partner pairs and years.

    I tried these codes but I don't get to generate the values in the "sum" column below:

    bysort id (year): gen sum=TA_envi_tot[_n] + TA_envi_tot[_n+1] if TA_envi_sum[_n]!=TA_envi_tot[_n-1]
    bysort id (year): replace sum = sum[_n-1] if missing(sum)

    id reporter_iso partner_iso year TA_envi_tot sum
    3271 ATG DEU 1981 0 0
    3271 ATG DEU 1982 0 0
    3271 ATG DEU 1983 0 0
    3271 ATG DEU 1984 36 36
    3271 ATG DEU 1985 36 36
    3271 ATG DEU 1986 36 36
    3271 ATG DEU 1987 67 103
    3271 ATG DEU 1988 67 103
    3271 ATG DEU 1989 67 103
    4217 BDI BEL 1981 3 3
    4217 BDI BEL 1982 3 3
    4217 BDI BEL 1983 3 3
    4217 BDI BEL 1984 35 38
    4217 BDI BEL 1985 35 38
    4217 BDI BEL 1986 35 38
    4217 BDI BEL 1987 35 38
    4217 BDI BEL 1988 36 74
    4217 BDI BEL 1989 36 74
    4217 BDI BEL 1990 36 74


    Thanks a lot for your help!
    Last edited by Dorothea Ramizo; 11 Mar 2021, 01:25.

  • #2
    Cross-posted and answered at https://stackoverflow.com/questions/...-across-time-u

    Please note our policy on cross-posting, which is that you are asked to tell us about it. https://www.statalist.org/forums/help#crossposting

    Comment


    • #3
      Hi Nick, Thanks a lot for the guidance. The policy on cross-posting is noted.

      Comment

      Working...
      X