Buzz Burhans ([email protected]) wrote:
> 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.
Buzz Burhans ([email protected]) later gave the following example:
> If it helps to make sense of what I am after, I am interested in the
> association of population density in a pen at the day of exit and outcomes
> for cows that are in certain management pens, i.e. maternity or treatment
> pens. I have dates each cow on a farm entered or left the pens in question,
> what I need is to figure out a count of the cohort present at each cows
> exit from the pen.
>
> . list farmid cowid enterPEN exitPEN enteredsofar Present if farmid ==3,
> table clean
>
> farmid cowid enterPEN exitPEN entere~r
> Count(CowsinPenOnHerExitDate)
> 50. 3 19 01/04/02 01/18/02 1 3
> 51. 3 18 01/04/02 01/22/02 2 2
> 52. 3 20 01/04/02 01/22/02 3 2
> 54. 3 22 01/18/02 02/04/02 5 1
> 55. 3 581 03/01/02 03/24/02 11 2
> 56. 3 25 03/01/02 03/02/02 7
> 57. 3 26 03/01/02 03/21/02 9
> 58. 3 582 03/01/02 03/22/02 10
> 59. 3 579 03/01/02 03/12/02 8
> 60. 3 580 03/08/02 04/01/02 12
> 61. 3 583 03/27/02 04/03/02 13
> 62. 3 13 03/27/02 04/06/02 14
I believe the following code will solve your problem.
sort farmID
gen count = .
local N = _N
forvalues i = 1/`N' {
qui count if exitPen > exitPen[`i'] ///
& enterPen < exitPen[`i'] ///
& farmID == farmID[`i']
qui replace count = r(N) + 1 in `i'
}
Here are the results:
. sort farmID
. gen count = .
(12 missing values generated)
. local N = _N
. forvalues i = 1/`N' {
2. qui count if exitPen > exitPen[`i'] ///
> & enterPen < exitPen[`i'] ///
> & farmID == farmID[`i']
3. qui replace count = r(N) + 1 in `i'
4. }
. list
+----------------------------------------------+
| farmID cowID enterPen exitPen count |
|----------------------------------------------|
1. | 3 19 01/04/02 01/18/02 3 |
2. | 3 18 01/04/02 01/22/02 2 |
3. | 3 20 01/04/02 01/22/02 2 |
4. | 3 22 01/18/02 02/04/02 1 |
5. | 3 581 03/01/02 03/24/02 2 |
|----------------------------------------------|
6. | 3 25 03/01/02 03/02/02 5 |
7. | 3 26 03/01/02 03/21/02 4 |
8. | 3 582 03/01/02 03/22/02 3 |
9. | 3 579 03/01/02 03/12/02 5 |
10. | 3 580 03/08/02 04/01/02 3 |
|----------------------------------------------|
11. | 3 583 03/27/02 04/03/02 2 |
12. | 3 13 03/27/02 04/06/02 1 |
+----------------------------------------------+
--Shannon Driver
[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/