title: Pairwise correlations over rolling periods ignoring double calculations
body: <p>I am trying to compute pairwise correlations over rolling windows for n= 40 variables where all rolled pairwise correlations for 2 given variables are saved in a new variable.</p>
  
  <p>My dataset has the following structure: </p>
  
  <pre><code>Date        V1   V2   V3  . . .
  01/01/2009  0.3  0.6  0.5
  02/01/2009  0.1  0.5  0.2 
  03/01/2009  0.7  0.1  0.1
  .
  .
  .
  </code></pre>
  
  <p>The following code does the trick. <strong>However, it fails to ignore the lower diagonal of the correlation matrix</strong> (it estimates twice the correlation between vars + once the 1s on the diagonal):</p>
  
  <pre><code>ds (Date), not
  gl vars `r(varlist)'
  
  local i = 0
  local j = 0
  
  foreach current_variable1 in $vars {
  local i = `i'+1 
   foreach current_variable2 in $vars {
  local j = `j'+1 
      if (`j' &gt; `i') {
       mvcorr `current_variable1' `current_variable2', ///
       generate(corr_`current_variable1'_`current_variable2') window(60) force
  
  }
  }
  } 
  </code></pre>
  
  <p>In particular the <code>if</code> condition on (<code>j &gt; i</code>) intended to capture the doubled calculation of each pairwise correlation fails to do so. Including the <code>if</code> condition in the <code>mvcorr</code> yields only the error message <code>'no observations'</code>.</p>
  
  <p>What could be the solution for this issue?</p>