+ Reply to Thread
Results 1 to 2 of 2

Thread: Need cumulative sum but not for the entire series

  1. #1
    Points: 160, Level: 3
    Level completed: 20%, Points required for next Level: 40

    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need cumulative sum but not for the entire series




    Hello,

    I need to create the cumulative sum for a time variable for each person.

    The first column has the personid. The second column has the information about time spent (time_spent) at each point.

    Now, i want another column which gives me the cumulative time spent (CUMUL_TIME) by the person. Thus, the CUMUL_TIME variable always starts with zero and goes until the maximum value for each person. Can anyone please suggest me a way to do this?

  2. #2
    Points: 2,895, Level: 32
    Level completed: 97%, Points required for next Level: 5

    Posts
    124
    Thanks
    0
    Thanked 56 Times in 50 Posts

    Re: Need cumulative sum but not for the entire series


    Here you go:

    Code: 
    sort cases by personid.
    compute cumul_time = time_spent.
    if (personid = lag(personid)) cumul_time = cumul_time + lag(cumul_time).
    exe.

+ Reply to Thread

           




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts






Advertise on Talk Stats