Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: st: Rolling window correlations for all possible pairs of variables

From   <>
To   <>
Subject   RE: st: Rolling window correlations for all possible pairs of variables
Date   Mon, 13 Aug 2012 12:30:46 +0100

Dear Maarten and stata list,
Many thanks for the prompt reply!
This is very helpful and works well, however I receive an error message when a pair of variables do not have any overlapping observations. Say var1 has observations in period 1900-1960, whereas var2 has observations in 1970-2000. Thus as it is not possible to calculate the corr, the calculations simply stop. Is there any way to force the do-file keep calculating?
Thank you very much,


From: on behalf of Maarten Buis
Sent: Mon 13/08/2012 11:48
Subject: Re: st: Rolling window correlations for all possible pairs of variables

On Mon, Aug 13, 2012 at 12:08 PM,  <> wrote:
> I have a time series dataset consisting of more than several hundreds of variables, and roughly 1500 time points.
> I want to calculate rolling window correlations for each possible pairs of the variables, and store the results either in a new data set or generate new variables for each possible combination (for instance for var 1 and var2, it is rho1_2, for var1 and var3 it is rho1_3 and so on). Doing this manually is quite inefficient and time consuming.

*---------------------------- begin example -------------------------
// get and prepare some example data
webuse lutkepohl2, clear
tsset qtr

// collect the names of all variables except the time variable
ds qtr, not
local vars `r(varlist)'

// count the number of variables
local k_vars : word count `vars'

// store the first variable name in the local macro 1
// the second variable name in the local macro 2, etc.
tokenize `vars'

// I don't want to clutter my harddrive whith such example datasets
tempfile tofill temp orig
save `orig'

forvalues i = 1/`k_vars' {
        forvalues j = `=`i'+1'/`k_vars' {
                // get some informative info while waiting for the loop to finish
                di as txt "rolling correlation between " _c
                di as result "``i''" as txt " and " as result "``j''"
                quietly {
                        if `i' == 1 & `j' == 2 {
                                // note that the first time round `i' evaluates to 1,
                                // which in turn is evaluated to the first variable name
                                rolling r(rho), window(10) saving(`tofill'): ///
                                                corr ``i'' ``j''
                                use `tofill', clear
                                rename _stat_1 r_``i''_``j''
                                save `tofill', replace
                        else {
                                use `orig', clear
                                rolling r(rho), window(10) saving(`temp', replace) : ///
                                                cor ``i'' ``j''
                                use `temp', clear
                                rename _stat_1 r_``i''_``j''
                                merge 1:1 start using `tofill'
                                assert _merge == 3
                                drop _merge
                                save `tofill', replace

use `tofill'
*----------------------------- end example --------------------------
(For more on examples I sent to the Statalist see: )

Hope this helps,

Maarten L. Buis
Reichpietschufer 50
10785 Berlin
Germany <> 

*   For searches and help try:

Please access the attached hyperlink for an important electronic communications disclaimer:


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index