Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: Duration data- count number of spells with ref to current spell
From
Roberto Ferrer <[email protected]>
To
Stata Help <[email protected]>
Subject
Re: st: Duration data- count number of spells with ref to current spell
Date
Tue, 27 Aug 2013 00:27:47 +0100
Nick's is better, of course:
*----------------------- input --------------------------------
clear
input person eventStart str20 eventType
1 10 work
1 14 lunch
1 15 work
1 16 work
1 33 shopping
1 34 shopping
end
*----------- what you want ----------------------------
gen dum = 1 if eventType == "work"
gen cumact = sum(dum)
gen before = cumact[_n-1]
gen after = cumact[_N] - cumact
replace before = 0 if _n == 1
list
On Mon, Aug 26, 2013 at 11:43 PM, Roberto Ferrer <[email protected]> wrote:
> This works with your example. Add -by- when generalizing to multiple
> person case.
>
> * ------------------------------ input---------------------------------
>
> clear
> input person eventStart str20 eventType
> 1 10 work
> 1 14 lunch
> 1 15 work
> 1 16 work
> 1 33 shopping
> 1 34 shopping
> end
>
> *-------------------- what you want---------------------------------
>
> gen dum =1 if eventType == "work"
> gen before = sum(dum) -1
> replace before = before + 1 if dum == .
>
> egen tot = max(before)
> gen after = tot - before - 1
> replace after = after + 1 if dum == .
>
> *---------------------clean up-----------------------------
> drop dum tot
> list
>
> On Mon, Aug 26, 2013 at 10:01 PM, Sriram Narayanamoorthy
> <[email protected]> wrote:
>> Hi Nick,
>>
>> Thanks for the response. Though, I am not quite sure I understand the
>> solution. Is the variable "activity" in your toy dataset is the
>> eventType I referred to in my question or is it something else. I
>> think your approach would give me the net time spend in other
>> activities before current activity (or time remaining to spend). What
>> I want to do is count the number of activity of a particular type.
>>
>> I have provided an example below:
>>
>> person eventStart eventType
>> 1 10 work
>> 1 14 lunch
>> 1 15 work
>> 1 16 work
>> 1 33 shopping
>> 1 34 shopping
>>
>>
>> The dataset is sorted in person and eventStart. Say, I want to count
>> the number of "work" events before and after the current event - based
>> on the eventStart variable. This would be my output. For, example for
>> the lunch tour, I have 1 work tours before time period 14 and 2 work
>> tours after time period 14.
>>
>> person eventStart eventType numWorkBefore
>> numWorkAfter
>> 1 10 work 0
>> 2
>> 1 14 lunch 1
>> 2
>> 1 15 work 1
>> 1
>> 1 16 work 2
>> 0
>> 1 33 shopping 3
>> 0
>> 1 34 shopping 3
>> 0
>>
>> Please let me know if I am misunderstanding your solution.
>>
>> 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/