Hi, I'm trying to get something like this, but not the part of double Y-axis but the part where I get the average for every hour for each variable. I don't know how to make the graph display the mean of each variable. I know I probably need to use PROC MEANS or PROC SUMMARY but I can't find anything online. I have a lot of observations.
My data looks something like this:
Year | Month | Day | Hour | Load | Temperature |
20010 | 1 | 1 | 0 | 70000 | 20.87 |
2009 | 1 | 1 | 1 | 68000 | 20.61 |
2011 | 1 | 1 | 2 | 67000 | 20.27 |
2009 | 1 | 1 | 3 | 66000 | 20.52 |
2009 | 1 | 1 | 4 | 65000 | 21.45 |
2010 | 1 | 1 | 5 | 66000 | 21.69 |
2009 | 1 | 1 | 6 | 67000 | 21.87 |
2009 | 1 | 1 | 7 | 69000 | 22.39 |
2009 | 1 | 1 | 8 | 69000 | 22.72 |
2009 | 1 | 1 | 9 | 70000 | 24.61 |
2011 | 1 | 1 | 10 | 71800 | 26.74 |
2009 | 1 | 1 | 11 | 72000 | 29.72 |
And goes on and on.
I've simulated some sample data so that everyone can work with the same data. Here's how to sort and use PROC MEANS to summarize the data:
proc format ;
value mn_name
1='January' 2='February' 3='March' 4='April'
5='May' 6='June' 7='July' 8='August'
9='September' 10='October' 11='November' 12='December'
other='Invalid';
run;
/* simulate time series data */
data Have;
call streaminit(1);
do year = 2009 to 2011;
do month = 1 to 12;
if month=2 then NDays=28;
else if month in (4,6,9,11) then NDays = 30;
else NDays=31;
do Day = 1 to NDays;
do hour = 0 to 23;
load = 80 + 5*sin(hour/12*constant('pi')) + rand("Normal");
temperature = 30 + 5*sin((hour+1)/12*constant('pi')) + rand("Normal");
output;
end;
end;
end;
end;
run;
proc sort data=Have;
by month hour;
run;
proc means data=Have noprint;
by month hour;
var load temperature;
output out=MeanOut mean=;
run;
proc sgpanel data=MeanOut;
format month mn_name.;
panelby month / layout=panel columns=2;
series x=Hour y=Temperature;
run;
The SGPANEL procedure does not support multiple Y axes, although you can use the GTL to display the Y2 axis.
The solution is to run PROC SUMMARY to create means, and then run PROC SGPLOT using the means computed by PROC SUMMARY.
Use proc means, then plot the output of the proc means. The sgplot will run a lot quicker, and thus the proc means run time is negligible.
do you have any example code? Sorry, that's how I learn to code in SAS. Otherwise, I know what you mean but have to idea how to execute it.
I've simulated some sample data so that everyone can work with the same data. Here's how to sort and use PROC MEANS to summarize the data:
proc format ;
value mn_name
1='January' 2='February' 3='March' 4='April'
5='May' 6='June' 7='July' 8='August'
9='September' 10='October' 11='November' 12='December'
other='Invalid';
run;
/* simulate time series data */
data Have;
call streaminit(1);
do year = 2009 to 2011;
do month = 1 to 12;
if month=2 then NDays=28;
else if month in (4,6,9,11) then NDays = 30;
else NDays=31;
do Day = 1 to NDays;
do hour = 0 to 23;
load = 80 + 5*sin(hour/12*constant('pi')) + rand("Normal");
temperature = 30 + 5*sin((hour+1)/12*constant('pi')) + rand("Normal");
output;
end;
end;
end;
end;
run;
proc sort data=Have;
by month hour;
run;
proc means data=Have noprint;
by month hour;
var load temperature;
output out=MeanOut mean=;
run;
proc sgpanel data=MeanOut;
format month mn_name.;
panelby month / layout=panel columns=2;
series x=Hour y=Temperature;
run;
The SGPANEL procedure does not support multiple Y axes, although you can use the GTL to display the Y2 axis.
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.
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.