It's the sequential nature of the process that's getting you.
Try doing it all at once:
replace team=31
replace team = cond(sub_out == 1, team[_n - 1] - 2^(player - 1), ///
cond(sub_in == 1, team[_n - 1] + 2^(player - 1), ///
team[_n-1])) if _n>1
Tom
-----------------------------------
Thomas J. Steichen
[email protected]
-----------------------------------
-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Eric G. Wruck
Sent: Thursday, January 08, 2009 11:45 AM
To: [email protected]
Subject: st: Handling Play-by-Play Basketball Data
I am trying to analyze basketball data for my favorite team, the
Davidson Wildcats. In particular, I want to see how the team
performs with a given line-up on the floor. Here is an example of
the data:
+--------------------------------------------------------+
| clock davidson player |
|--------------------------------------------------------|
10. | 18:19 0 |
11. | 18:19 0 |
12. | 18:05 REBOUND (OFF) by BARR, Bryant 1 |
13. | 18:05 MISSED JUMPER by PAULHUS GOSSELIN,Max 3 |
14. | 18:01 MISSED LAYUP by ROSSITER, Steve 5 |
|--------------------------------------------------------|
15. | 17:57 REBOUND (DEF) by BARR, Bryant 1 |
16. | 17:44 MISSED JUMPER by LOVEDALE, Andrew 4 |
17. | 17:42 SUB OUT: ROSSITER, Steve 5 |
18. | 17:42 SUB IN : ALLISON, Ben 7 |
19. | 17:42 FOUL by ROSSITER, Steve (P1T1) 5 |
|--------------------------------------------------------|
20. | 17:29 FOUL by LOVEDALE, Andrew (P1T2) 4 |
+--------------------------------------------------------+
I want to create a variable team whose value will tell me who is on
the floor at any one time. I do this by assigning values to the
players & by summing the 2^(player - 1) for the players on the floor;
I want the variable team to contain this sum. The starting players
I've numbered 1 through 5 so that the initial team value is 31 = 2^(1
- 1) + 2^(2 - 1) + 2 ^(3 - 1) + 2^(4 - 1) + 2^(5 - 1). So what I
want to do is subtract 2^(player - 1) from team for that player taken
out & then immediately adding 2^(player - 1) for the player coming
in. I then need to retain this new value until there is another
substitution. Here's the code I am using:
gen team = 1 + 2 + 4 + 8 + 16
replace team = team[_n - 1] - 2^(player - 1) if sub_out == 1
replace team = team[_n - 1] + 2^(player - 1) if sub_in == 1
replace team = team[_n - 1] if sub_out ~= 1 & sub_in ~= 1 & _n > 1
However, I get this in the way of results:
+-------------------------------------------------------+
| davidson player team |
|-------------------------------------------------------|
10. | 0 31 |
11. | 0 31 |
12. | REBOUND (OFF) by BARR, Bryant 1 31 |
13. | MISSED JUMPER by PAULHUS GOSSELIN,Max 3 31 |
14. | MISSED LAYUP by ROSSITER, Steve 5 31 |
|-------------------------------------------------------|
15. | REBOUND (DEF) by BARR, Bryant 1 31 |
16. | MISSED JUMPER by LOVEDALE, Andrew 4 31 |
17. | SUB OUT: ROSSITER, Steve 5 15 |
18. | SUB IN : ALLISON, Ben 7 79 |
19. | FOUL by ROSSITER, Steve (P1T1) 5 79 |
|-------------------------------------------------------|
20. | FOUL by LOVEDALE, Andrew (P1T2) 4 79 |
21. | 0 79 |
22. | STEAL by LOVEDALE, Andrew 4 79 |
23. | TURNOVR by BARR, Bryant 1 79 |
24. | 0 79 |
|-------------------------------------------------------|
25. | 0 79 |
26. | REBOUND (DEF) by LOVEDALE, Andrew 4 79 |
27. | MISSED 3 PTR by BARR, Bryant 1 79 |
28. | REBOUND (DEF) by LOVEDALE, Andrew 4 79 |
29. | ASSIST by CURRY, Stephen 2 79 |
|-------------------------------------------------------|
30. | GOOD! LAYUP by ALLISON, Ben [PNT] 7 79 |
31. | REBOUND (DEF) by CURRY, Stephen 2 79 |
32. | TURNOVR by LOVEDALE, Andrew 4 79 |
33. | SUB OUT: BARR, Bryant 1 30 |
34. | SUB OUT: CURRY, Stephen 2 28 |
|-------------------------------------------------------|
35. | SUB IN : ARCHAMBAULT, Will 6 60 |
36. | SUB IN : MCKILLOP, Brendan 8 188 |
37. | 0 188 |
38. | SUB OUT: LOVEDALE, Andrew 4 23 |
39. | SUB OUT: PAULHUS GOSSELIN,Max 3 19 |
|-------------------------------------------------------|
40. | SUB IN : CURRY, Stephen 2 21 |
+-------------------------------------------------------+
This is actually what I want until I get to line 33 when Barr leaves
the court. I want team to be 79 - 2^(1 - 1) = 78 at that point.
When Curry comes on, I want team to be 78 + 2^(2 - 1) = 80. Instead
the calculation is made relative to the initial value for team of 31.
How do I fix this?
Many thanks,
Eric
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/
CONFIDENTIALITY NOTE: This e-mail message, including any attachment(s), contains information that may be confidential, protected by the attorney-client or other legal privileges, and/or proprietary non-public information. If you are not an intended recipient of this message or an authorized assistant to an intended recipient, please notify the sender by replying to this message and then delete it from your system. Use, dissemination, distribution, or reproduction of this message and/or any of its attachments (if any) by unintended recipients is not authorized and may be unlawful.
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/