SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
Fer_
Fluorite | Level 6

Hello Everyone. 

I want to calculate standard deviation of the firm's daily returns during a given year. I have permno as identifier for each firm and daily data for stock return. Such as below. 

Permno           date                       ret 

10000         10/03/2002          0.005

10000         10/06/2002         0.0051

10002          12/06/2002         0.007

 

and so on. 

Could you please help me to calculate standard deviation of the firm's daily returns during a given year? 

Thanks in advance. 

3 REPLIES 3
mkeintz
PROC Star

If you want std of returns for each permno for the same year, then, assuming data are sorted by PERMNO:

 

proc summary data=have ;
  by permno ;
  where '01jan2022'd <= date <= '31dec2022'd;
  var ret;
  output out=want (drop=_:) std=retstd;
run;

If you want it for each year in a series of multiple years, then, if data are sorted by permno year: 

data need / view=need;
  set have;
  year=year(date);
run;

proc summary data=need ;
  by permno year;
  var ret;
  output out=want (drop=_:) std=retstd;
run;

 

 

 

 

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Fer_
Fluorite | Level 6

Thanks for your reply. 

I want to get yearly std for each firm (permno) which has daily stock return. Which means for each firm in each year, I need to have one std at the end. 

 

mkeintz
PROC Star

@Fer_ wrote:

Thanks for your reply. 

I want to get yearly std for each firm (permno) which has daily stock return. Which means for each firm in each year, I need to have one std at the end. 

 



That is what my second code block provides.  Test it on a small sample of data to see for yourself.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

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
  • 3 replies
  • 684 views
  • 0 likes
  • 2 in conversation