Betas (CIZ Format) - Macro

Compute Market-Model Beta for all CRSP securities on a rolling daily or monthly basis.

Macro call with an example

The following example calls the beta_ciz SAS macro and calculates Beta for all CRSP securities between January 1st, 1990 and June 30th, 2001 on a rolling daily basis.

%beta_ciz(S=d,START=01JAN1990,END=30JUN2001,WINDOW=250,MINWIN=60, INDEX=VWRETD);

Top of Section

SAS macro

/* ********************************************************************************* */
/* ******************** W R D S   R E S E A R C H   M A C R O S ******************** */
/* ********************************************************************************* */
/* WRDS Macro: BETA_CIZ                                                              */
/* Summary   : Create Beta Estimation                                                */
/* Author    : Rabih Moussawi, WRDS                                                  */
/* Update    : November 2024 by Freda Drechsler for CRSP CIZ data format             */
/* Variables : - S     : Monthly (m) or Daily (d) frequency                          */
/*             - START : Sample Start Date                     				         */
/* 			   - END   : Sample End Date       										 */
/* 			   - WINDOW: Window of Estimation 										 */
/*      	   - MINWIN: Minimum Window of Estimation for non-missing betas 		 */
/*             - INDEX : Market Return Variable, with default Value-Weighted (VWRETD)*/ 
/* ********************************************************************************* */
%macro beta_ciz (s=m, START=01JAN1990, END  =30JUN2001, WINDOW=36, MINWIN=12, INDEX=VWRETD);

/* Example for Daily Beta Computation */
/* %let S=d; %let WINDOW=250; %let MINWIN=60; */
  
/* START. Computing Betas from &sf Using &WINDOW Estimation Window */
%let sf       = crsp.&s.sf_v2 ; /* CRSP Stock Dataset: Daily vs. Monthly */

%put &sf.;

* As CIZ CRSP data has different naming convention for daily and monthly data;
* First need to change the variable names;

%if %sysfunc(upcase(&s)) = M %then %do;
  data _crsp0;
    set &sf.;
    keep permno mthcaldt mthret;
    rename mthcaldt = date mthret = ret;
  run;

  data _ind0;
    set crsp.wrds_monthlyindexret_query;
    rename mthcaldt = date;
  run;
%end;

%else %do;
  data _crsp0;
    set &sf.;
    keep permno dlycaldt dlyret;
    rename dlycaldt = date dlyret = ret;
  run;

  data _ind0;
    set crsp.wrds_dailyindexret_query;
    rename dlycaldt = date;
  run;
%end;
 

data _crsp1;
  set _crsp0;
  where "&START."D<=date<="&END."D;
run;
  
/* Add Index Return Data */
proc sql;
  create table _crsp2
  as select a.*, b.&index,
  b.&index*(abs(a.ret)>=0) as X, a.ret*b.&index as XY,
  (abs(a.ret*b.&index)>=0) as count
  from _crsp1 as a, _ind0 as b
  where a.date=b.date
  order by a.permno, a.date;
quit;
 
/* Compute Components for Covariances and Variances for Market Model Regression */
proc printto log = junk; run;
proc expand data=_crsp2 out=_crsp3 method=none;
  by permno;
  id date;
  convert X=X2      / transformout= (MOVUSS &WINDOW.);
  convert X=X       / transformout= (MOVSUM &WINDOW.);
  convert XY=XY     / transformout= (MOVSUM &WINDOW.);
  convert ret=Y     / transformout= (MOVSUM &WINDOW.);
  convert ret=Y2    / transformout= (MOVUSS &WINDOW.);
  convert ret=tvol  / transformout= (MOVSTD &WINDOW.);
  convert count=n   / transformout= (MOVSUM &WINDOW.);
quit;
run;
proc printto; run;
  
/* Calculate Betas, R-Squared, and Idiosyncratic Volatility */
data BETA;
  set _crsp3;
  if n>=&MINWIN. and (Y2-(Y**2)/n)>0 then
    do;
      beta     = (XY-X*Y/n) / (X2-(X**2)/n);
      alpha    =  Y/n- beta*X/n;
      R2       = (XY-X*Y/n)**2 / ( (X2-(X**2)/n) * (Y2-(Y**2)/n) );
      Sigma    = sqrt( ((Y2-(Y**2)/n) - beta*(XY-X*Y/n)) / (n-2) );
      Beta_std = sigma/sqrt(X2-(X**2)/n);
   end;
  label alpha = "Stock Alpha";
  label beta = "Stock Beta";
  label R2 = "Market Model R-Squared";
  label Tvol = "Total Stock Volatility";
  label Sigma = "Idiosyncratic Volatility";
  label beta_std = "Beta Standard Deviation";
  label n = "Number of Observations used to compute Beta";
  drop X X2 XY Y Y2 COUNT;
  format n beta beta_std comma8.2 Tvol ret alpha R2 Sigma &index percentn8.2;
run;
  
/* House Cleaning */
proc sql;
  drop table _crsp0, _crsp1, _crsp2, _crsp3, _ind0;
quit;
%mend beta_ciz;
 
/* ********************************************************************************* */
/* *************  Material Copyright Wharton Research Data Services  *************** */
/* ****************************** All Rights Reserved ****************************** */
/* ********************************************************************************* */

Top of Section

Usage Notes

For every month or day, the macro computes the beta of the stock using the trailing returns within the pre-specified window. The efficiency of the code arises from the superior performance of PROC EXPAND in computing rolling statistics. The code can be easily modified to efficiently compute the four loadings for each of Fama and French factors.

Top of Section

Top