## plotting weekly variance

Frequent Contributor
Posts: 91

# plotting weekly variance

Hi,

could some one please let me know how to adjust the following code to calculate WEEKLY VARIANCE ? the code below does it for monthly variance.

Data set is attached.

thanks.

data test;

infile "F:\data\residuals.csv"  DSD MISSOVER;

input date :MMDDYY9. lnprices residual;

format date mmddyy10.;

run;

/* calculate monthly variance for all variables;*/

proc summary data=test nway;

class date;

var residual;

output out=month_var (drop=_ var= ;

format date monyy.;

run;

/* rearrange data for by-group processing in GPLOT;*/

proc transpose data=month_var out=tmonth_var (rename=(col1=var));

by date;

format date monyy.;

run;

/*Graphing*/

proc sort data=tmonth_var;

by _name_ date;

run;

options validvarname=upcase;

goptions ftext='Arial' htext=2 gunit=pct;

symbol1 v=dot i=join;

axis1 label=(angle=90 "Monthly Variance");

title h=4 "Variance by Month for:1997-2005";

footnote j=right "Source: Henry Hub";

proc gplot data=tmonth_var;

plot var*date/vaxis=axis1;

by _name_ ;

run;

quit;

Posts: 2,161

## Re: plotting weekly variance

Instead of this in PROC SUMMARY

format date monyy.;

you want to add a week variable in data test;

You can use the week function and the year function to create a variable like this

week=cats(year(date),'W',week(date));

--
Paige Miller
Super User
Posts: 21,497

## Re: plotting weekly variance

Change the format applied to your data to be a weekly format rather than monyy., I aligned mine to the beginning of the week but something similar can be done for end of the week if required.

%macro get_weekdate_fmt;

proc format;

value weekdate_fmt

%do k=0 %to 3000;

%eval(2+&k*7) - %eval(2+&k*7+6)=%sysfunc(putn(%eval(2+&k*7), date.))
%end;;*need two semicolons to end properly;
run;
%mend get_weekdate_fmt;

I had some help with creating one here, see code above.

http://listserv.uga.edu/cgi-bin/wa?A2=ind1211b&L=sas-l&F=&S=&P=4762

Super User
Posts: 12,148

## Re: plotting weekly variance

Or a custom format using the date directives such as:

proc format library=work;

picture MyWeekFmt (min=8 max=8)

low-high = '%Y %0U' (datatype=date);

run;

Note that the directives ARE CASE SENSITIVE. The U could be W or V for different definitions of start week.

data _null_;

x= '23Feb2011'd;

put x= MyWeekFmt.;

run;

Discussion stats
• 3 replies
• 234 views
• 6 likes
• 4 in conversation