code to calculate standard deviation

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 96
Accepted Solution

code to calculate standard deviation

Dear All,

I have a dataset with company name, Date, and stock prices. I want to create a new variable- stock return volatility. To create this new variable, I first created stock returns calculated as (Stock price in year t minus stock return in year t-1)/ stock price in year t-1. For stock return volatility, I want to take standard deviation of monthly stock return for the 36 month period ending in the last month of the fiscal year.

The program I know to get standard deviation is as follows:

Stock_Vol = STD(lag1(stock return), lag2(stock return), lag3(stock return)........lag36(stock return);

Does anyone know any other code to get standard deviation? Also if a company has less than 36 months of stock return information, I want to delete the company. Can someone please tell me how I can do so?

The subset of the dataset looks as follows:

TICKERdatepricereturns
IBM20010131112.
IBM2001022899.9-0.10804
IBM2001033096.18-0.03724
IBM20010430115.140.19713
IBM20010531111.8-0.02901
IBM200106291130.010733
IBM20010731105.21-0.06894
IBM2001083199.95-0.05
IBM2001092891.72-0.08234
IBM20011031108.070.17826
IBM20011130115.590.069585
IBM20011231120.960.046457
IBM20020131107.89-0.10805
IBM2002022898.12-0.09056
IBM200203281040.059927
IBM2002043083.76-0.19462
IBM2002053180.45-0.03952
IBM2002062872-0.10503
IBM2002073170.4-0.02222
IBM2002083075.380.070739
IBM2002093058.31-0.22645
IBM2002103178.940.353799
IBM2002112986.920.101089
IBM2002123177.5-0.10838
IBM20030131105.21-0.06894
IBM2003022899.95-0.05
IBM2003032891.72-0.08234
IBM20030430108.070.17826
IBM20030531115.590.069585
IBM20030628120.960.046457
IBM20030731107.89-0.10805
IBM2003083098.12-0.09056
IBM200309301040.059927
IBM2003103183.76-0.19462
IBM2003112980.45-0.03952
IBM2003123172-0.10503

How can I calculate stock volatility of IBM for 2003?

Thank you for the time.


Accepted Solutions
Solution
‎02-23-2013 10:00 AM
PROC Star
Posts: 7,356

Re: code to calculate standard deviation

I think you might find it quite a bit easier using something like:

data have;

  informat date date9.;

  format date date9.;

  input company $ date stock_price;

  cards;

IBM 31DEC2011 100

IBM 31JAN2012 200

IBM 29FEB2012 300

IBM 31MAR2012 400

HP  29FEB2012 300

HP  31MAR2012 400

MBI 31JAN2012 400

MBI 29FEB2012 600

MBI 31MAR2012 800

;

proc summary data=have (where=('31JAN2012'D LE DATE LE '31MAR2012'D)) nway;

  var stock_price;

  class company;

  output out=want (drop=_: where=(n eq 3)) n=n stddev=sd;

run;

View solution in original post


All Replies
Solution
‎02-23-2013 10:00 AM
PROC Star
Posts: 7,356

Re: code to calculate standard deviation

I think you might find it quite a bit easier using something like:

data have;

  informat date date9.;

  format date date9.;

  input company $ date stock_price;

  cards;

IBM 31DEC2011 100

IBM 31JAN2012 200

IBM 29FEB2012 300

IBM 31MAR2012 400

HP  29FEB2012 300

HP  31MAR2012 400

MBI 31JAN2012 400

MBI 29FEB2012 600

MBI 31MAR2012 800

;

proc summary data=have (where=('31JAN2012'D LE DATE LE '31MAR2012'D)) nway;

  var stock_price;

  class company;

  output out=want (drop=_: where=(n eq 3)) n=n stddev=sd;

run;

Frequent Contributor
Posts: 96

Re: code to calculate standard deviation

Thanks Arthur for the code.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 1794 views
  • 0 likes
  • 2 in conversation