Reputation: 25
I need to create a timeseries graph (using PROC SGPLOT at the moment) that plots monthly counts of workplace incidents from 2000 to 2010.
The problem I am having is that I don't know how to organise the data in order to have the xaxis showing YEAR and MONTH.
At the beginning I have the following variables: DATE, YEAR, MONTH, INCIDENT_ID.
I successfully used PROC SQL to create counts of distinct INCIDENT_ID grouped by YEAR and MONTH (called 'COUNT'). I then unsuccessfully created a PROC SGPLOT using YEAR or MONTH for the xaxis, but this does not plot COUNT correctly. I then tried sorting the data set by YEAR, MONTH and created an TIME_ID variable that put them in order, and tried using this as the x-axis. While it plots COUNT correctly, it does not provide an understandable x-axis.
Therefore I have my counts (COUNT) that I want to plot in a PROC SGPLOT series, but what do I use as the x-axis to have it show YEAR and/or MONTH of COUNT? Or is there some obvious other way to do this?
Upvotes: 2
Views: 2664
Reputation: 12465
You need to specify MONYY7. format on your date variable. Then specify a month interval on the xaxis.
like so:
data test;
format date monyy7.;
count = 0;
do year=2010 to 2013;
do month=1 to 12;
count = count + ceil(ranuni(123)*2);
date = mdy(month,1,year);
output;
end;
end;
run;
ods html;
ods graphics on / HEIGHT=10in;
proc sgplot data=test;
scatter x=date y=count;
xaxis interval=month fitpolicy=rotate;
run;
ods html close;
I had to play with the image size to make the axis labels readable. I also rotated them so more could fit.
Upvotes: 2