DATA Step, Macro, Functions and more

rolling betas

Reply
New Contributor
Posts: 3

rolling betas

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 to 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 example data(data=aaa).

Date        firm         r        rm

040129   45920010   0.01234   0.00134

040227   45920010   0.00123   -0.00145

:         :           :           :

171227   45920010   0.12398   0.00258

040129   59491810   0.01234   0.00134

040227   59491810   0.00123   -0.00145

:         :           :           :

171227   59491810   0.12398   0.00258

Respected Advisor
Posts: 3,277

Re: rolling betas

SAS code in my textbook is following.

How should I fix it?

 

What is wrong with it? Please be specific.

--
Paige Miller
Super User
Posts: 10,850

Re: rolling betas

1) Make a dataset (Key_Table) like :

id start            end

1 2004/01/31  2008/12/31       

1 2005/01/31  2009/12/31  

1 2013/01/31  2017/12/31 

 

2) Make a macro .

%macro estim (id=,start=,end=);

 

             proc reg data =have(where=(id=&id and date between &start and &end)) outest = results ;

                    model r = rm ;

                           by firm per ;

                           quit ;

             proc append base = betas1 data = results ;

%end ;

%mend estim ;

 

3) CALL EXECUTE() to go through.

 

data _null_;

 set Key_Table;

 call execute(cats('%estim (id=',id,',start=',start,',end=',end,')'));

run;

 

Ask a Question
Discussion stats
  • 2 replies
  • 124 views
  • 0 likes
  • 3 in conversation