Each observation in my data presents a player who follows some random pattern. Variables move1
up represent on which moves each player was active. I need to count the number of times each player was active:
The data look as follows (with _count
representing a variable that I would like to generate). The number of moves can also be different depending on simulation.
+------------+------------+-------+-------+-------+-------+-------+-------+--------+ | simulation | playerlist | move1 | move2 | move3 | move4 | move5 | move6 | _count | +------------+------------+-------+-------+-------+-------+-------+-------+--------+ | 1 | 1 | 1 | 1 | 1 | 2 | . | . | 3 | | 1 | 2 | 2 | 2 | 4 | 4 | . | . | 2 | | 2 | 3 | 1 | 2 | 3 | 3 | 3 | 3 | 4 | | 2 | 4 | 4 | 1 | 2 | 3 | 3 | 3 | 1 | +------------+------------+-------+-------+-------+-------+-------+-------+--------+
egen
combined with anycount()
is not applicable in this case because the argument for the value()
option is not a constant integer.
I have made an attempt to cycle through each observation and use egen
rowwise (see below) but it keeps count
as missing (as initialised) and is not very efficient (I have 50,000 observations). Is there a way to do this in Stata?
gen _count =.
quietly forval i = 1/`=_N' {
egen temp = anycount(move*), values( `=`playerlist'[`i']')
replace _count = temp
drop temp
}