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;
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));
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
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: