BookmarkSubscribeRSS Feed
Ramin1
Obsidian | Level 7

Hi Experts,

 

I am going to calculate sigma following the paper Campbell et al. (2008), where he defines sigma as 

Ruhul_0-1625734220759.png

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;

1 REPLY 1
ChrisNZ
Tourmaline | Level 20

Have you checked with a few dozen test records that the calculations are performed as you expect?

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 1009 views
  • 0 likes
  • 2 in conversation