Hi Experts,
I am going to calculate sigma following the paper Campbell et al. (2008), where he defines sigma as
Where r^2 is the firm-level daily return and N is the number of trading days in three months period. K is the index of trading days in months t-1, t-2, t-3.
They require a firm have at least 5 nonzero daily observations. In this case, they report sigma as missing and replace it with the annual cross-sectional mean.
I have the attached code (I do not have much idea of such hash function), can you look into this and tell me whether the 3 months rolling volatility/standard deviation calculated perfectly or not and how can I annualize it? I am attaching the code and data for you. If you think there is any other efficient way or the code need modification please post it. Thanks in advance.
Link of the paper:
https://scholar.harvard.edu/files/campbell/files/campbellhilscherszilagyi_jf2008.pdf
Portion of code:
data want;
if _n_=1 then do;
if 0 then set have(rename=(ret=_ret));
declare hash h(multidata:'y');
h.definekey('Date');
h.definedata('_ret');
h.definedone();
/**********/
declare hash m();
m.definekey('_monyy');
m.definedata('_ret');
m.definedone();
/**********/
end;
array x{100} _temporary_;
do until(last.PERMNO);
set have;
by PERMNO;
_ret=ret;h.add();
end;
do until(last.PERMNO);
set have;
by PERMNO;
n=0;call missing(of x{*});
m.clear(); /**<---***/
do i=intnx('month',monyy,-3) to intnx('month',monyy,-1,'e');
rc=h.find(key:i);
do while(rc=0);
if not missing(_RET) then do;n+1;x{n}=_ret; _monyy=intnx('month',i,0); m.replace(); end; /**<---***/
rc=h.find_next(key:i);
end;
end;
std=std(of x{*});
if n<5 then std=.; /**<-----****/
if m.num_items<3 then std=.; /**<----EDIT HERE-****/
output;
end;
Have you checked with a few dozen test records that the calculations are performed as you expect?
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!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.