BookmarkSubscribeRSS Feed
malakaext
Calcite | Level 5

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;

3 REPLIES 3
PaigeMiller
Diamond | Level 26

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
Reeza
Super User

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

ballardw
Super User

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;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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