title: Calculating correlation and covariance for a event window in SAS
body: <p>I have to calculate the correlation and covariance for my daily sales values for an event window. The event window is of 45 day period and my data looks like -</p>
  
  <pre><code>store_id    date       sales
  5927    12-Jan-07    3,714.00
  5927    12-Jan-07    3,259.00
  5927    14-Jan-07    3,787.00
  5927    14-Jan-07    3,480.00
  5927    17-Jan-07    3,646.00
  5927    17-Jan-07    3,316.00
  4978    18-Jan-07    3,530.00
  4978    18-Jan-07    3,103.00
  4978    18-Jan-07    3,026.00
  4978    21-Jan-07    3,448.00
  </code></pre>
  
  <p>Now, for every store_id, date combination, I need to go back 45 days (there is more data for each combination in my original data set)  calculate the correlation between sales and lag(sales) i.e. autocorrelation of degree one. As you can see, the date column is not continuous. So something like (date - 45) is not going to work.</p>
  
  <p>I have gotten till this part -</p>
  
  <pre><code>data ds1;
    set ds;
    by store_id;
    LAG_SALE = lag(sales);
        IF FIRST.store_idTHEN DO;
        LAG_SALE = .;
        END;
  run;
  </code></pre>
  
  <p>For calculating correlation and covariances -</p>
  
  <pre><code>proc corr data=ds1 outp=Corr
  by store_id date;
        cov;   /**  include covariances **/
  var sales lag_sale;
  run;
  </code></pre>
  
  <p>But how do I insert the event window for each date, store_id combination? My final output should look something like this -</p>
  
  <pre><code>id    date     corr cov
  5927 12-Jan-07 ... ...
  5927 14-Jan-07 ... ...
  </code></pre>