BookmarkSubscribeRSS Feed
costasRO
Fluorite | Level 6

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

 

3 REPLIES 3
PaigeMiller
Diamond | Level 26

A search using your favorite Internet search engine should turn up many answers, some using macros, some not using macros.

--
Paige Miller
costasRO
Fluorite | Level 6

Yes, I have tried that, and I have a code that runs but it does the regression for every date, which is slow and unnecessary as I only want the calculations done at each month end. That is the part I am struggling with.

PaigeMiller
Diamond | Level 26

Change the code so the Regression is only calculated on the last day of the month. It should be a relatively simply %IF statement.

 

--
Paige Miller

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Autotuning Deep Learning Models Using SAS

Follow along as SAS’ Robert Blanchard explains three aspects of autotuning in a deep learning context: globalized search, localized search and an in parallel method using SAS.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 1205 views
  • 0 likes
  • 2 in conversation