title: Row-wise count/sum of values in Stata
body: <p>I have a dataset where each person (row) has values <code>0</code>, <code>1</code> or <code>.</code> in a number of variables (columns).</p>
  
  <p>I would like to create two variables. One that includes the count of all the <code>0</code> and one that has the count of all the <code>1</code> for each person (row).
  In my case, there is no pattern in the variable names. For this reason I create a varlist of all the existing variables excluding the ones that need not to be counted.</p>
  
  <pre><code> +--------+--------+------+------+------+------+------+----------+--------+
   |   ID   | region |  Qa  |  Qb  |  C3  |  C4  |  Wa  |  count 0 | count 1|
   +--------+--------+------+------+------+------+------+----------+--------+
   |      1 |      A |    1 |    1 |    1 |    1 |    . |        0 |      4 |
   |      2 |      B |    0 |    0 |    0 |    1 |    1 |        3 |      2 |
   |      3 |      C |    0 |    0 |    . |    0 |    0 |        4 |      0 |
   |      4 |      D |    1 |    1 |    1 |    1 |    0 |        0 |      4 |
   +--------+--------+------+------+------+------+------+----------+--------+
  </code></pre>
  
  <p>The following works, however, I cannot add an <code>if</code> statement</p>
  
  <pre><code>ds ID region, not // all variables in the dataset apart from ID region
  return list  
  local varlist = r(varlist)
  egen count_of_1s = rowtotal(`varlist') 
  </code></pre>
  
  <p>If I change the last line with the one below, I get an error of invalid syntax.</p>
  
  <pre><code>egen count_of_1s = rowtotal(`varlist') if `v' == 1 
  </code></pre>
  
  <p>I turned from count to summing because I thought this is a sneaky way out of the problem. I could change the values from 0,1 to 1, 2, then sum all the two values separately in two different variables and then divide accordingly in order to get the actual count of 1 or 2 per row.</p>
  
  <p>I found this <a href="https://stackoverflow.com/questions/18543912/stata-using-egen-anycount-when-values-vary-for-each-observation"></a><a href="https://stackoverflow.com/questions/18543912/stata-using-egen-anycount-when-values-vary-for-each-observation">Stata: Using egen, anycount() when values vary for each observation</a> however Stata freezes as my dataset is quite large (100.000 rows and 3000 columns).</p>
  
  <p>Any help will be very appreciated :-)</p>
  
  <h2>Solution based on the response of William</h2>
  
  <pre><code>* number of total valid responses (0s and 1s, excluding . )
  ds ID region, not // all variables in the dataset apart from ID region
  return list  
  local varlist = r(varlist)
  egen count_of_nonmiss = rownonmiss(`varlist') // this counts all the 0s and 1s (namely, the non missing values)
  
  * total numbers of 1s per row
  ds ID region count_of_nonmiss, not // CAUTION: count_of_nonmiss needs not to be taken into account for this!
  return list  
  local varlist = r(varlist)    
  generate count_of_1s = rowtotal(`varlist')
  </code></pre>