BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
shalmali
Calcite | Level 5

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.

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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

2 REPLIES 2
art297
Opal | Level 21

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;

shalmali
Calcite | Level 5

Thanks Arthur for the code.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 3554 views
  • 1 like
  • 2 in conversation