Hello everyone;
I have a file with daily stock returns that looks like this
Company Date var1 var2 var3
1 1/1/2000
1 1/2/2000
. .
1 31/12/2010
2 1/1/2007
2 1/2/2007
.
2 31/12/2020
etc
Not all companies have the same date range
I want to run a rolling regression for each company using var1, var2 and var3, using past data from a window of length n, where n is one month, 2 months up to 24, months. So n should be part of a macro that I can control.
The regression should first regress var1=a+ var2 + var3 + u, save the residuals, and then do some further calculation of the residuals to aggregate them and calculate a new variable var4. So there are some calculations to be done for each company and each time period that is selected based on n.
I would like the calculations to start at the end of each month for each company. For example, on the 31/1/2000 for company 1 the macro should run the regression for this company and do the following calculations using past data. if n=1 month then the calculations should use data from 1/1/2000 until 31/1/2000, etc. If n=2 months, for the 31/1/2000 for company 1, the macro should do the calculations for this company using data from 1/12/1999 until 31/1/2000, etc.
The resulting file should look like
Company id date v ar4_n1 var4_n2 var4_n3 etc (until var4_n24)
1 31/1/2000 x . . .
1 31/2/2000 x y . .
1 31/12/2010 x y z .
2 31/1/2007 x . . .
.
2 31/12/2020 x y z .
var4_n are the variables that will be constructed during the calculation for each rolling window. they get populated sequentially deepening on data availability., for example, during the first month with data for company 1, only var4_n1 has a number and all other var4_n do not, etc.
So basically the macro should use the initial file to select the data that satisfy some date conditions based on n and using the last day of each month, run the calculations, save the resulting file, loop over n and at the end append all the resulting datasets.
Thank you all very much.
Costas
... View more