Bookmark and Share

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   Nick Cox <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: Duration data- count number of spells with ref to current spell
Date   Tue, 27 Aug 2013 00:11:27 +0100

clear
input person eventStart str20 eventType
1                  10                  work
1                  14                  lunch
1                  15                  work
1                  16                  work
1                  33                  shopping
1                  34                  shopping
2                  12                  sleep
2                  15                  work
2                  20                  gaming
end
bysort person (eventStart) : gen cumact = sum(eventType == "work")
by person  : gen before = cond(_n == 1, 0, cumact[_n-1])
by person  : gen after = cumact[_N] - cumact

Nick
[email protected]


On 26 August 2013 22:01, 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/


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