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-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 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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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