Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | Nick Cox <njcoxstata@gmail.com> |
To | "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu> |
Subject | Re: st: Duration data- count number of spells with ref to current spell |
Date | Mon, 26 Aug 2013 21:37:14 +0100 |
The cumulative or running sum is given by -sum()-. For a toy dataset apply this separately. . bysort id : gen cumact = sum(activity) . l +------------------------+ | id activity cumact | |------------------------| 1. | 1 1 1 | 2. | 1 3 4 | 3. | 1 5 9 | 4. | 1 7 16 | 5. | 1 9 25 | |------------------------| 6. | 2 2 2 | 7. | 2 4 6 | 8. | 2 6 12 | 9. | 2 8 20 | +------------------------+ The activity before any event is by id : gen before = cumact[_n-1] The activity after any event is by id : gen after = cumact[_N] - cumact In your case, you will want to sort by time, but the main idea should carry over. Nick njcoxstata@gmail.com On 26 August 2013 20:51, Sriram Narayanamoorthy <nsriram@utexas.edu> wrote: > Hello everyone, > > This question is on data generation when working with a > discrete-duration datasets. The dataset I have includes all the > activities undertaken by individuals in a 24 hour period. The day is > divided into 48 half-hour intervals. The start interval of a spell is > denoted by a variable called eventStart. The activity type is denoted > by the variable eventType. > > The task of counting the number of activities of a particular type > undertaken in a day by the person is quite straightforward using egen. > > However, I want to generate a variable as the number of activities of > a particular type undertaken by the individual before/after the > current activity. How do I add the "Before" and "After" dimension to > my variable? To be sure, I want to compare the eventStart of my > current record with all other records for that person and count the > number of records of a particular eventType only if eventStart is less > than (for Before) or greater than (for After) the current start > period. > > Any thoughts? Is there a mata approach to this? Thanks much for the inputs! > > Thanks, > -Sriram > * > * For searches and help try: > * http://www.stata.com/help.cgi?search > * http://www.stata.com/support/faqs/resources/statalist-faq/ > * http://www.ats.ucla.edu/stat/stata/ * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/faqs/resources/statalist-faq/ * http://www.ats.ucla.edu/stat/stata/