Building models with SAS Enterprise Miner, SAS Factory Miner, SAS Visual Data Mining and Machine Learning or just with programming

Help to calculate weekly standard deviation from daily price panel data--URGENT HELP REQUIRED

Accepted Solution Solved
Reply
Contributor
Posts: 43
Accepted Solution

Help to calculate weekly standard deviation from daily price panel data--URGENT HELP REQUIRED

Hi i have a daily stock price panel data over a period of years, i want to calculate weekly standard deviation for each stock and then use it as a regressor in a regression equation, how can i do it, please help.

i have data like this

data have;

input stockname $ price date year;

cards;

abc 26  20071205 2007

abc 46  20071206 2007

abc 36  20071207 2007

abc 26  20071208 2007

abc 26  20071210 2007

abc 35 20071212 2007

abc 75 20080123 2008

abc 45 20080224 2008

abc 55 20090612 2009

abc 51 20090613 2009

abc 57 20090614 2009

xyz 26  20071205 2007

xyz 35 20071206 2007

xyz 75 20080123 2008

xyz  45 20080612 2008

;

run;

frequency and number of stock is much higher though, i have tried to use proc means but it is not working for the panel data for me, i am really stuck up right no and cant figure out how to do it

data ghf;

set have;

format date date9.;

yrwk=year(date)*100+week(date,"U");

run;

proc sort data = ghf;

by yrwk stockname;

run;

proc means data=ghf std;

  var _numeric_;

  by yrwk stockname;

  output out=dsd;

run;

but it gives me a messy output i.e. for every stock for each date it gives me STD MEAN and other stats too, is there any more efficient way to do this?? PLEASE HELP


Accepted Solutions
Solution
‎11-26-2012 09:43 AM
PROC Star
Posts: 7,467

Re: Help to calculate weekly standard deviation from daily price panel data--URGENT HELP REQUIRED

Why not just limit the output to what you need?  e.g.:

data need;

  set sashelp.pricedata;

  yrwk=year(date)*100+week(date,"U");

run;

proc summary data=need nway;

  var _numeric_;

  class regionName yrwk;

  output out=want (drop=_Smiley Happy mean= std= /autoname;

run;

View solution in original post


All Replies
Solution
‎11-26-2012 09:43 AM
PROC Star
Posts: 7,467

Re: Help to calculate weekly standard deviation from daily price panel data--URGENT HELP REQUIRED

Why not just limit the output to what you need?  e.g.:

data need;

  set sashelp.pricedata;

  yrwk=year(date)*100+week(date,"U");

run;

proc summary data=need nway;

  var _numeric_;

  class regionName yrwk;

  output out=want (drop=_Smiley Happy mean= std= /autoname;

run;

Contributor
Posts: 43

Re: Help to calculate weekly standard deviation from daily price panel data--URGENT HELP REQUIRED

hello , i have a question, when i creak an year week variable as you have mentioned above, why doe sas graph does not treat it as a date variable ??? when i make graphs it gives me discontinuity although there are none in the data

Respected Advisor
Posts: 4,918

Re: Help to calculate weekly standard deviation from daily price panel data--URGENT HELP REQUIRED

For graphing you would get better results with a genuine date variable such as :

yrwk = intnx("WEEK",date,0);

format yrwk yyweeku.;

and then, if you use SGPLOT graphing proc :

series x=yrwk y=ystd / group=regionName;

xaxis type=time tickvalueformat=yyweeku. fitpolicy=thin;

PG

PG
Contributor
Posts: 43

Re: Help to calculate weekly standard deviation from daily price panel data--URGENT HELP REQUIRED

but now as i have wee,ly data, based on the yrwk variable i created , and now i dont have date variable.

now my yrwk variable has the following format 200601 ....200652;;;;;;;;;200701.......200752

how can i convert this variable in a  year week format which sas treats like a date variable??*

thanks

Respected Advisor
Posts: 4,918

Re: Help to calculate weekly standard deviation from daily price panel data--URGENT HELP REQUIRED

Then you have to do a bit of back flipping to recover the week dates (untested)...


data wantGraph;
set want;

/* transform number 200601 into string "2006W0101" and read with WEEKU9. informat */
yrwkDate = input(cats(floor(yrwk/100),"W",put(mod(yrwk,100),z2.),"01"),weeku9.);
format yrwkDate yyweeku.;
run;

proc sgplot data=wantGraph;
series x=yrwkDate y=price_std / group=regionName;
xaxis type=time tickvalueformat=yyweeku. fitpolicy=thin;
run;

PG

PG
Super Contributor
Super Contributor
Posts: 444

Re: Help to calculate weekly standard deviation from daily price panel data--URGENT HELP REQUIRED

can anyone please tell me what week(date,"U") does?

Super Contributor
Posts: 1,636

Re: Help to calculate weekly standard deviation from daily price panel data--URGENT HELP REQUIRED

PROC Star
Posts: 7,467

Re: Help to calculate weekly standard deviation from daily price panel data--URGENT HELP REQUIRED

: Have you looked up and read the page in the documentation?

SAS(R) 9.2 Language Reference: Dictionary, Fourth Edition

Super Contributor
Super Contributor
Posts: 444

Re: Help to calculate weekly standard deviation from daily price panel data--URGENT HELP REQUIRED

thanks guys

🔒 This topic is solved and locked.

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

Discussion stats
  • 9 replies
  • 1814 views
  • 2 likes
  • 5 in conversation