New Contributor
Posts: 3

# Rolling beta estimation

[ Edited ]

Dear all,

I'm working on a regression with rolling beta.

My regression starts from 2004/01/31 to 2017/12/31.

Based on 60 monthly returns, estimated betas for each of the two stocks at the end of each year for the recent 10 years.

For example, 2004/01/31 to 2008/12/31, 2005/01/31 to 2009/12/31, 2006/01/31 to 2010/12/31, ...., 2013/01/31- 2017/12/31.

It is to obtain period beta for stock A and B by 10 periods.

Here are my expected results:

beta (A)   beta (B)

period 1 (2004/01/31 ~ 2008/12/31)           x.xx          x.xx

period 2 (2005/01/31 ~ 2009/12/31)           x.xx          x.xx

:                                          :               :

period10 (2013/01/31 ~ 2017/12/31)          x.xx          x.xx

SAS code in my textbook is following. How should I fix it?

data aaa ;

set beta.beta ;

keep cusip date ret sprtrn ;

format date yymmdd6. ;

rename cusip=firm ret=r sprtrn=rm ;

run ;

proc sort data=aaa ;

by firm date ;

run ;

data begin (keep=firm bgndate) end (keep=firm enddate) ;

set aaa ;

by firm ;

if first.firm then do ;

bgndate=date ;

format bgndate yymmdd6. ;

output begin ;

end ;

if last.firm then do ;

enddate=date ;

format enddate yymmdd6. ;

output end ;

end ;

run ;

data length ;

merge begin end ;

by firm ;

* delete firms with too few return days ;

if bgndate > mdy (01,31,13) then delete ;

if enddate < mdy (12,31,08) then delete ;

keep firm ;

run ;

data gooddata ;

merge aaa length (in=a) ;

by firm ;

if a ;

n + 1 ;

if first.firm then n=1 ;

run ;

%macro estim ;

%do x = 50 %to 66 ;

data temp ;

set gooddata ;

if &x - 49 <= n <= &x ;

per = &x ;

proc reg data = temp outest = results ;

model r = rm ;

by firm per ;

quit ;

proc append base = betas1 data = results ;

%end ;

%mend estim ;

%estim ;

run ;

Here is sample data(data=aaa).

data            firm                   r                  rm

040130     45920010       0.070673       0.017276

040227     45920010      -0.025899       0.012209

040331     45920010      -0.048290      -0.016359

:                  :                     :                     :

171229     45920010      -0.003572       0.009832

PROC Star
Posts: 1,400

## Re: Rolling beta estimation

Show us some example data?

Super User
Posts: 13,939

## Re: Rolling beta estimation

Being a sometimes lazy programmer I might be tempted to add 10 variable to you data, one for each period such that period1=1 when date is in the given interval and 0 otherwise.

Something like:

```data aaa ;
set beta.beta ;
keep cusip date ret sprtrn ;
format date yymmdd6. ;
rename cusip=firm ret=r sprtrn=rm ;
period1 = (2004 le year(date) le 2008 );
period2 = (2005 le year(date) le 2009 );
/* continue the obvious pattern*/
run ;```

Then your regression could look like:

proc reg data = temp outest = results ;

where period1=1;

model r = rm ;

by firm  ;

quit ;

which would lend itself to a do 1 to 10 macro loop with : where period&i = 1;

I would also be tempted not to prefilter the "too few return days" but capture the n actually used by the model by adding:

ods output Nobs = ObsUsed;

to the Proc reg (and accumulating that as well)

You might want to add a label to the model statement such as:

Period&i: model ....

to have some additional information about which regression things are coming from as that label will be in the OUTEST or other model related output sets.

I have to say this bit of code is not obvious as to purpose

```%do x = 50 %to 66 ;

data temp ;

set gooddata ;

if &x - 49 <= n <= &x ;

per = &x ;
```

lots of magic numbers.

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