Observations in my dataset are players, and binary variables temp1
up are equal to 1 if the player made a move, and equal to zero otherwise.
I would like to to calculate the maximum number of consecutive moves per player.
+------------+------------+-------+-------+-------+-------+-------+-------+ | simulation | playerlist | temp1 | temp2 | temp3 | temp4 | temp5 | temp6 | +------------+------------+-------+-------+-------+-------+-------+-------+ | 1 | 1 | 0 | 1 | 1 | 1 | 0 | 0 | | 1 | 2 | 1 | 0 | 0 | 0 | 1 | 1 | +------------+------------+-------+-------+-------+-------+-------+-------+
My idea was to generate auxiliary variables in a loop, which would count consecutive duplicates and then apply egen, rowmax():
+------------+------------+------+------+------+------+------+------+------+ | simulation | playerlist | aux1 | aux2 | aux3 | aux4 | aux5 | aux6 | _max | +------------+------------+------+------+------+------+------+------+------+ | 1 | 1 | 0 | 1 | 2 | 3 | 0 | 0 | 3 | | 1 | 2 | 1 | 0 | 0 | 0 | 1 | 2 | 2 | +------------+------------+------+------+------+------+------+------+------+
I am struggling with introducing a local counter variable that would be incrementally increased by 1 if consecutive move is made, and would be reset to zero otherwise (the code below keeps auxiliary variables fixed..):
quietly forval i = 1/42 { /*42 is max number of variables temp*/
local j = 1
gen aux`i'=.
local j = `j'+1
replace aux`i'= `j' if temp`i'!=0
}