Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: counting across individuals within groups


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: counting across individuals within groups
Date   Thu, 30 Jan 2003 16:45:49 -0000

Buzz Burhans ruminated
>
> I would appreciate help with creating the following value,
> I can't seem to
> get it right:
>
> I have data with 4 vars in each record as follows:
>
> 1. farmID 	is an ID variable for farm
> 2. cowID 	is an ID variable for cow (within farmID)
> 3  enterPen 	is the date on which a cow enters Pen
> 4  exitPen	is the date on which the cow exits Pen
>
> There are multiple cows within each farm, and their
> enterPen and exitPen
> dates vary individually.
>
> What I need is a variable that contains the count of cows
> within the same
> farmID which have entered, but not yet exited the Pen on the date an
> individual cow exits the Pen,
>
> i.e.a count of  "other cows" within the same farmID whose
> enterPen date is
> < exitPen date for each individual cow (within the same
> farmID) and whose
> exitPen date (the "other cows" ) is > the exitPen date for
> the individual
> cow ...
>
> I can't seem to get the ifs to work correctly across
> different records,
> probably because I'm not very good at looping.
>

There may be a way to approach this with -st-
but -st- is not my daily bread, so I will
pass on that and try a direct approach.

I don't think -if- enters here, nor need
any looping. I surmise that you can get exactly
what you want with -by-. It's just that I
don't understand exactly what you want.

Any cow is still within the farm until the
day it exits. I don't see that -enterPen-
is relevant so long as -enterPen- <= -exitPen-.

A step towards what you want is, I think,

bysort farmID (exitpen) : gen enteredsofar = _n
qui by farmID exitpen :
	replace enteredsofar = enteredsofar[_N]

The number of other cows appears to be central,
which means subtract 1 from this, I guess.

Nick
[email protected]

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index