Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: calculating cumulative values of other observations

From   Eric Booth <>
Subject   Re: st: calculating cumulative values of other observations
Date   Sat, 7 Apr 2012 13:09:35 -0500


On Apr 7, 2012, at 9:42 AM, KOTa wrote:

> thanks Erik, Nick
> both ways work on this sample, and i managed to adjust for a real database also.
> few followup question:
> 1. Eric, any specific reason why you convert time into date format? I
> used it without conversion and it looks fine.
> Also on real data your solution somehow goes into infinite loop after
> finishing all calculations.
I don't know how you edited the code, so I don't know how you produced an infinite loop?  It certainly doesn't do that with the example I gave you.  Also, I created a time/date format version of "date" just out of habit, I find that it makes data management easier.  If you don't need/want it, then skip it.

> 2. Nick, the solution you proposed with "sum" was the one i tried
> initially. The problem is that in real data I have 2 columns for date
> (start_date end_date) and i want to compare start_date [_n] >=
> end_date[all previous]
> Eric's solution works longer, but i could adjust it to work with
> different date columns.
> your solution is works much faster in case there is one date column,
> but i could not figure out how to do the same with 2 different
> columns.

El día 7 de abril de 2012 04:08, Nick Cox <> escribió:
> Why not
> bysort id activity (time): gen priorhrs = sum(time) - time
> Nick

Nick's -sum- solution does work (and is more straightforward than my -replace line)  with a slight tweak - changing the first "time" to "date2" (if you create the time/date var from my example).  Also, if you are using the same variable names from your original example you'd change "time" to "hours".  Adding this to my example works:

  bysort ID activity (date2): gen njc = sum(hours) - hours

Adapting mine and Nick's code to your "real" dataset is another issue completely - if you cannot figure how to adapt our tips to your data/code, then you should post a data & code example/question that is closer to reality.

- Eric
Eric A. Booth
Public Policy Research Institute
Texas A&M University
Office: +979.845.6754

*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index