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: bysort, forvalues and _N


From   Constantin Alba <[email protected]>
To   [email protected]
Subject   Re: st: bysort, forvalues and _N
Date   Sat, 26 Jan 2013 01:26:10 +0200

thanks Nick,

that is what i was looking for exactly. and the data actually was like
that (start/end date) originally. so i just used the trick on it. the
results are correct.

I did understand how this trick worked in the example after the link,
but in current example i am missing it. can you, please, explain the
use of "bysort obsno" here?

thanks

C.

On Fri, Jan 25, 2013 at 10:10 PM, Nick Cox <[email protected]> wrote:
> I wouldn't approach it like that. I don't think a loop is required,
> but rather a different data structure. Also, holding Duration may be
> interesting and useful, but it is just awkward for this problem.
>
> First save the dataset if not already -save-d. Then
>
> gen long obsno = _n
> gen endDate = startDate + Duration
> expand  2
> bysort obsno : gen Date = cond(_n == 1, startDate, endDate)
> bysort obsno : gen inOut = cond(_n == 1, 1, -1)
> bysort PersonID (Date) : gen noActivities = sum(inOut)
>
> Compare e.g. thread starting at
>
> http://www.stata.com/statalist/archive/2012-11/msg01163.html
>
> Nick
>
> On Fri, Jan 25, 2013 at 7:38 PM, Constantin Alba
> <[email protected]> wrote:
>
>> I have a dataset organized by personID, for each personID there are
>> startDate and Duration for various activities.
>>
>> I want to find out how many activities a person performs at the same
>> time (e.g. something like workload)
>>
>> logically i see it like this:
>>
>> sort personID startDate Duration
>>
>> by personID: forvalues i=1/`=_N' {
>>                                    "compare current date with all
>> other prior dates+duration
>>                                        and update counter accordingly"
>>                                       }
>>
>> however, by and forvalues do not work together, so i am trying to find
>> an alternative way to do so.
> *
> *   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