Announcement

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

  • changing value of observations to cumulative version over time for each id

    Hi Guys,
    I have a panel of frequencies looking like
    # ID Year Freq
    1 1 2000 2
    2 1 2001 3
    3 1 2002 2
    4 2 2000 0
    5 2 2001 3
    6 2 2002 5
    I have more than 2000 IDs and around 20 years. I want to change this to a cumulative version where for each id values of freq will sum up over time like
    Version 2
    # ID Year Freq
    1 1 2000 2
    2 1 2001 5
    3 1 2002 7
    4 2 2000 0
    5 2 2001 3
    6 2 2002 8
    I think I can switch my panel to a wide version and then sum the values using a loop, but I was wondering if there are any other ways to approach this problem in this long format.
    And if I have to switch it to wide, what would be the best way I can define my loop to go over years for each id and sum the values year by year?
    Thanks for the help.
    Leila
    Last edited by leila salarpour; 11 Aug 2018, 06:37.

  • #2
    Use -sum()- within the -gen- command:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(num ID Year Freq wanted_freq)
    1 1 2000 2 2
    2 1 2001 3 5
    3 1 2002 2 7
    4 2 2000 0 0
    5 2 2001 3 3
    6 2 2002 5 8
    end
    
    bysort ID (Year) : gen cum_freq=sum(Freq)
    Stata/MP 14.1 (64-bit x86-64)
    Revision 19 May 2016
    Win 8.1

    Comment


    • #3
      See also

      Code:
      help cumul

      Comment


      • #4
        Thanks a lot!

        Comment

        Working...
        X