title: Stata: Counting number of consecutive occurrences of a pre-defined length
body: <p>Observations in my data set contain the history of moves for each player. I would like to count the number of consecutive series of moves of some pre-defined length (2, 3 and more than 3 moves) in the first and the second halves of the game.  The sequences cannot overlap, i.e. the sequence  1111 should be considered as a sequence of the length 4, not 2 sequences of length 2. That is, for an observation like this:</p>
  
  <pre><code>+-------+-------+-------+-------+-------+-------+-------+-------+
  | Move1 | Move2 | Move3 | Move4 | Move5 | Move6 | Move7 | Move8 |
  +-------+-------+-------+-------+-------+-------+-------+-------+
  |     1 |     1 |     1 |     1 | .     | .     |     1 |     1 |
  +-------+-------+-------+-------+-------+-------+-------+-------+
  </code></pre>
  
  <p>…the following variables should be generated:</p>
  
  <pre><code>Number of sequences of 2 in the first half =0 
  Number of sequences of 2 in the second half =1
  Number of sequences of 3 in the first half =0
  Number of sequences of 3 in the second half =0
  Number of sequences of &gt;3 in the first half =1 
  Number of sequences of &gt;3 in the second half = 0
  </code></pre>
  
  <p>I have two potential options of how to proceed with this task but neither of those leads to the final solution:</p>
  
  <p>Option 1: Elaborating on Nick’s tactical suggestion to use strings (<a href="https://stackoverflow.com/questions/18576167/stata-maximum-number-of-consecutive-occurrences-of-the-same-value-across-variab">Stata: Maximum number of consecutive occurrences of the same value across variables</a>), I have concatenated all “move*” variables and tried to identify the starting position of a substring:</p>
  
  <blockquote>
  <pre><code>egen test1 = concat(move*)
  gen test2 = subinstr(test1,"11","X",.) // find all consecutive series of length 2
  </code></pre>
  </blockquote>
  
  <p>There are several problems with Option 1:
  (1) it does not account for cases with overlapping sequences (“1111” is recognized as 2 sequences of 2)
  (2) it shortens the resulting string test2 so that positions of X no longer correspond to the starting positions in test1
  (3) it does not account for variable length of substring if I need to check for sequences of the length greater than 3.</p>
  
  <p>Option 2: Create an auxiliary set of variables to identify the starting positions of the consecutive set (sets) of the 1s of some fixed predefined length. Building on the earlier example, in order to count sequences of length 2, what I am trying to get is an auxiliary set of variables that will be equal to 1 if the sequence of started at a given move, and zero otherwise:</p>
  
  <pre><code>+-------+-------+-------+-------+-------+-------+-------+-------+
  | Move1 | Move2 | Move3 | Move4 | Move5 | Move6 | Move7 | Move8 |
  +-------+-------+-------+-------+-------+-------+-------+-------+
  |     0 |     0 |     0 |     0 |     0 |     0 |     1 |     0 |
  +-------+-------+-------+-------+-------+-------+-------+-------+
  </code></pre>
  
  <p>My code looks as follows but it breaks when I am trying to restart counting consecutive occurrences:</p>
  
  <pre><code>quietly forval i = 1/42 {
  gen temprow`i' =.
  egen rowsum = rownonmiss(seq1-seq`i') //count number of occurrences
  replace temprow`i'=rowsum 
  mvdecode seq1-seq`i',mv(1) if rowsum==2 
  drop rowsum
  }
  </code></pre>
  
  <p>Does anyone know a way of solving the task? </p>