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

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

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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=_:) mean= std= /autoname;

run;

View solution in original post

9 REPLIES 9
art297
Opal | Level 21

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=_:) mean= std= /autoname;

run;

Ahmad
Calcite | Level 5

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

PGStats
Opal | Level 21

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
Ahmad
Calcite | Level 5

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

PGStats
Opal | Level 21

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
Tal
Pyrite | Level 9 Tal
Pyrite | Level 9

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

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 choose a machine learning algorithm

Use this tutorial as a handy guide to weigh the pros and cons of these commonly used machine learning algorithms.

Find more tutorials on the SAS Users YouTube channel.

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