title: Stata generate by groups
body: <p>I have a very weird thing happening with my code. I have panel data set  with the panel id being <em>p_id</em> and I am trying to create a another variable by using panel_id. My code is this, where <em>p_id</em> is the panel id, <em>marital_status</em> of person observed in each time period and <em>x</em> is the variable I would want to create. </p>
  
  <pre><code>bys p_id: gen count =_N
  bys p_id: gen count1 =_n
  bys p_id: gen x= marital_status if count1 ==1
  </code></pre>
  
  <p>However when I do </p>
  
  <pre><code>tab x
  </code></pre>
  
  <p>I get different numbers for rows (row total does not change) each time I run this code. The numbers are pretty closely clustered, but I need to understand why this is happening. </p>