I have been graphing the number of Product A manufactured and the percent prevalence of defects by week. The data begins in early June and by this point the x axis is getting awfully congested. I would like to change the labeling to show the start date of every other week. I am not sure how much longer these graphs will be needed, so hard coding the tick marks seems like a bit of a pain. I tried including values=(date1 to date2 by 14), however, this combines the two weeks of data into one bar and I need all of the bars. I then tried using mod(date, 2)=0 to select every other date, but can't figure out how to only apply this to the labels. I also tried using formats, but since the graph has data from both 2020 to 2021, it creates a mess. Below is how I created the week variables, my original SGPlot code for the original graph I have, and for the one which creates the combined bars. I have attached a pdf of the graph outputs and a tab delimited file with some sample observations for one of the warehouses.
/*Create week variables*/
data demo; set foo;
week1=week(date,"u");
nweek=week1-22;
run;
data demo2; set demo;
if first.nweek then displayweek=date; /*show first day of each week*/
displayweek+0;
if last.nweek then graphweek=displayweek;
run;
/*Graphs*/
title "Original";
proc sgplot data=demo2;
xaxis label="Week Beginning" labelattrs=(size=12pt weight=bold)
valueattrs=(size=12pt);
yaxis min=0 label="Count" INTEGER labelattrs=(size=12pt weight=bold)
valueattrs=(size=13pt) fitpolicy=thin offsetmin=0;
y2axis min=0 label="%" labelattrs=(size=12pt weight=bold)
valueattrs=(size=13pt) fitpolicy=thin offsetmin=0;
vbar graphweek/response=count legendlabel="Number";
vline graphweek/response=pct y2axis legendlabel="Percent"
lineattrs=(thickness=2)
markers MARKERATTRS=(symbol=TriangleFilled);
run;
title "Fix Attempt";
proc sgplot data=demo2;
xaxis label="Week Beginning" interval=auto
values=('07jun20'd to &lastday by 14) valuesformat=date.
labelattrs=(size=12pt weight=bold) valueattrs=(size=12pt);
yaxis min=0 label="Count" INTEGER labelattrs=(size=12pt weight=bold)
valueattrs=(size=13pt) fitpolicy=thin offsetmin=0;
y2axis min=0 label="Percent" labelattrs=(size=12pt weight=bold)
valueattrs=(size=13pt) fitpolicy=thin offsetmin=0;
vbar graphweek/response=count legendlabel="Number";
vline graphweek/response=pct y2axis legendlabel="Percent"
lineattrs=(thickness=2)
markers MARKERATTRS=(symbol=TriangleFilled);
run;
With a large and apparently growing number of categories I would suggest moving from VBAR to NEEDLE plot.
Two required parameters x=graphweek and y=count and set a needle width large enough to look "bar" like with the LINEATTRS=( Thickness= n ) option. You even get to specify a unit type like CM MM or IN for the thickness. 5mm would make the "bars" 5mm wide.
Then your xaxis can use date ranges like Values = ('01JAN2021'd to '01OCT2021'd by month) to create one tick mark per month. If you end up with long enough series then you can use "by quarter" or even "by year".
Apologies for the delayed response. This was so close to working! I had to adjust the weeks because any week with less than 7 days, caused an overlapping bar. Unfortunately it's highly likely that I will need to send out this graph at various points during the week (e.g. not always at the end of a week), so the last week displayed is off (see attached picture). I tried further adjusting the bar widths, but it didn't help.
Any suggestions?
Try adding option FITPOLICY=THIN or FITPOLICY=STAGGERTHIN to your XAXIS statement.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.