title: Create duplicate rows based on conditions in SAS or R
body: <p>I want to copy rows in data set depending how many rows subject has in original dataset. </p>
  
  <pre><code>id x1 x2 x3 count
  1  a  b  c  1
  1  b  c  f  2
  2  g  h  a  1
  2  a  d  c  2
  2  f  g  a  3
  3  a  g  a  1
  </code></pre>
  
  <p>I have created this count variable to detect number of rows each subject has. 
  (Don't mind about values of x1--x3, they are just here for illustration.) I have also much more subjects in actual dataset than these three. </p>
  
  <p>Dataset, what I want should look like this. </p>
  
  <pre><code>id x1 x2 x3 count
  1  a  b  c  1
  1  b  c  f  2
  1  a  b  c  1
  1  b  c  f  2
  2  g  h  a  1
  2  a  d  c  2
  2  f  g  a  3
  2  g  h  a  1
  2  a  d  c  2
  2  f  g  a  3
  3  a  g  a  1
  </code></pre>
  
  <p>I have tried following code</p>
  
  <pre><code>data want (drop=i);
  set have;
  by id;
  output;
  do i = 1 to count;
  output;
  end;
  run;
  </code></pre>
  
  <p>But obviously it makes too many copies.. </p>
  
  <p>Any help with R or SAS is highly appreciated! </p>