Hi, I am creating a horizontal stacked bar chart with percentage on the x-axis and supply item on the y-axis where each segment of the bar shows on-hand supply of that item (length of time before factory runs out of item, 4 groups = 0 days, 1 day, 2 days, 3 days). Reporting of supply data is optional, so different supply items may have different sample sizes. I would like to include the sample size for the number of factories represented in each full bar (sum the number of factories across the 4 possible segments of each bar). Because this is a stacked bar chart with 4 groups included in each bar, the datalabel option displays 4 columns summarizing sample size for each. I tried to force SAS to include sample size for just one of the 4 segments by setting 3 values to missing but missing values are still displayed (see photo). Based on the photo shown, I would like to keep the "0 days" column (and will rename "Number of Factories"). Is there a way to 1) suppress datalabel columns where values are "." or 2) apply a where statement to datalabel (such as, datalabel=num_factories(where supply_length='0 days') or 3) create and display another summary variable that sums supply_count across the 4 segments (supply_count/num_factories=percent). The code used to create the example chart is displayed here:
proc sgplot data=test dattrmap=attrmap;
hbar supply / response=percent group=supply_length groupdisplay=stack attrid=myid datalabel=fac_count datalabelpos=right nooutline;
keylegend /title='On-hand supply availability';
xaxis grid values=(0 to 100 by 10) label="Percentage";
yaxis discreteorder=data label="Supply Item";
run;
Thank you!
Yes. Read the documentation for the KEYLEGEND statement. Use the NAME= option to specify the statements that you want to appear in the legend. You might also need to use the NOAUTOLEGEND option:
proc sgplot NOAUTOLEGEND ...;
hbar supply / name="bar" ...;
keylegend "bar" / ...;p
run;
I suggest you use the YAXISTABLE statement to display the sample size and any other statistics.
Thank you! Just tried the yaxistable statement with nomissingchar option and get the N I wanted (thanks!), however, the legend disappears and I get the error message "DISCRETELEGEND statement with DISPLAYCLIPPED=FALSE is getting clipped. The legend will not be drawn." I tried adding the discretelegend option and now have 2 legends displayed. Do you know how to maintain the original legend on bottom once the yaxistable statement is added?
Yes. Read the documentation for the KEYLEGEND statement. Use the NAME= option to specify the statements that you want to appear in the legend. You might also need to use the NOAUTOLEGEND option:
proc sgplot NOAUTOLEGEND ...;
hbar supply / name="bar" ...;
keylegend "bar" / ...;p
run;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Lock in the best rate now before the price increases on April 1.
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.