I have created a simple vbar chart from data like the attached (I can't figure out how to paste into this browser).
And I have used sgplot to make a simple bar chart, with the colors of the bars different for each week depending on the "code" column. I also got the weeks to appear in the data order! This is nice, since the data is epi data, and they do things by year and week.
Attached is a sample of the output.
The code was this, roughly.......
proc sgplot data= d300 dattrmap=attrmap;
vbar week /response=percent group=code attrid=code nooutline;
xaxis label='Epi Week' type=discrete discreteorder=data;
keylegend / title='';
So right now it all looks like I want. However, I see a need in the future to have both the week and the year as the labels on the xaxis. So, in other words, the week would still be displayed as it is now, with year as a second level label under the weeks (year only appearing once, centered under the weeks it covers).
I have not been able to find an example of how to do this. Is it possible?
In the next release of SAS, we will have support for interval bar charts. This means you'll be able to plot bar charts along a continuous number line, including time lines. Our time axis already has support for the two-level labeling you describe, so the code will be very simple:
proc sgplot data=sashelp.air;
where date >= '01jan1959'd;
vbar date / response=air;
The interval bar chart will be in the next release; but, the only axis with two-level support is the time axis. If you are willing to use GTL, you might be able to accomplish what you want using block plots in conjunction with a bar chart. Basically, you would turn off the X-axis and use two axis-aligned block plots. Try the following example and see if it works for you:
define statgraph bar_sample;
entrytitle "Regional Sales Figures";
layout lattice / rowweights=preferred;
layout overlay / xaxisopts=(display=(line ticks));
barchartparm x=year_month y=actual / group=region name="sales";
blockplot x=year_month block=month / valuehalign=start display=(label values)
blockplot x=year_month block=year / valuehalign=start display=(label values)
sidebar / align=bottom spacefill=false;
/* Pre-summarize the data so that the block plots will render correctly */
proc summary data=sashelp.prdsale nway;
class year month region;
output out=temp sum=;
/* Build the X-axis variable to create unique category values */
year_month = year||month;
proc sgrender data=temp2 template=bar_sample; run;
Sanjay, thank you for the response. The link did not work for me--did you mean this one?
I have referenced many of your articles and your book (graphics by example) as I have learned sgplot. Thank you for breaking things down for the average user. I learned to apply the attribute map from that blog article.
I did read that article on epi curves, although I did not need to go to the full extent of using the data labels to label the x axis--we had a variable for epiweek in the data, and I just made sure there was an observation for each week in the data, so there are no gaps. Then ordering the xaxis by the data was enough.
However, to go the next step, once the epiweeks start "wrapping," (i.e. we have epi week 30 for both 2014 and 2015 in the data), I foresee a need to be able to put the year under the week in the xaxis, sort of as a second level category.
I will give the GTL example above from Dan a try at some point, although I am trying to keep the code as simple as possible, because I am trying to convince some folks who love excel that SAS can do it just as well, and that it is not too difficult.
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
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.