Hello!
My shortened sgplot-Syntax is like
vbar week / response=blood_donation group=Product nomissinggroup;
vline week / response=blood_issued group=Product nomissinggroup;
and I have to exclude observations with missing values for the group variable to have a nice legend. Nomissinggroup doesn't seem to work in that way.
Is there a simple way to exclude missing value groups?
Hallo @AntjeWestphal,
How about this legend?
Obtained with this code:
proc sgplot data=Ges;
title height=15pt 'Pool-TK und Apherese-TK';
xaxis labelattrs=(size=15);
yaxis label='TK-Herstellung und -Abgabe an Patienten' values=(0 to 100 by 10) offsetmin=0 valueattrs=(size=13) labelattrs=(size=15);
y2axis label='VB-Spenden' values=(0 to 400 by 40) offsetmin=0 valueattrs=(size=13) labelattrs=(size=15);
vbar KW2 / name='B1' response=Herst group=Produkt transparency=0.2;
vline KW2 / name='LP' response=AusgTK group=Produkt lineattrs=(thickness=2);
vbar KW2 / name='B2' response=SpendVB group=Produkt transparency=0.4 nooutline barwidth=0.4 fillattrs=(color=grey) y2axis;
keylegend 'B1' / noborder valueattrs=(size=12) exclude=('TK' 'VB') position=bottomleft;
keylegend 'LP' / noborder valueattrs=(size=12) exclude=( 'TKP' 'TKZ' 'VB') position=bottom;
keylegend 'B2' / noborder valueattrs=(size=12) exclude=('TK' 'TKP' 'TKZ' ) position=bottomright;
run;
I added the NAME= option to the plots and separate KEYLEGEND statements for each plot, referring to the (arbitrary) names, using the EXCLUDE= option to remove the unwanted items and the POSITION= option for the positioning. Moreover, I omitted the LEGENDLABEL= options because they have "no effect if you also specify the GROUP= option in the same plot statement" (according to the documentation).
Could you add a statement like this after the `VBAR` and `VLINE`?
where product ~= "";
Assuming `product` is a character variable.
Here are the data. It's a mixture out of 3 different sources.
This is the original code:
proc sgplot data=Ges; title height=15pt 'Pool-TK und Apherese-TK'; xaxis labelattrs=(size=15); yaxis label='TK-Herstellung und -Abgabe an Patienten' values=(0 to 100 by 10) offsetmin=0 valueattrs=(size=13) labelattrs=(size=15); y2axis label='VB-Spenden' values=(0 to 400 by 40) offsetmin=0 valueattrs=(size=13) labelattrs=(size=15); vbar KW2 / response=Herst group=Produkt transparency=0.2 legendlabel='Herstellung'; vline KW2 / response=AusgTK group=Produkt lineattrs=(thickness=2) legendlabel='TK-Ausgaben'; vbar KW2 / response=SpendVB group=Produkt transparency=0.4 nooutline legendlabel='VB-Spenden' barwidth=0.4 fillattrs=(color=grey) y2axis; keylegend / noborder valueattrs=(size=12); run;
And here is the result. The graphs are nice but not the legend. (Please disregard the color selection. 😉)
Hallo @AntjeWestphal,
How about this legend?
Obtained with this code:
proc sgplot data=Ges;
title height=15pt 'Pool-TK und Apherese-TK';
xaxis labelattrs=(size=15);
yaxis label='TK-Herstellung und -Abgabe an Patienten' values=(0 to 100 by 10) offsetmin=0 valueattrs=(size=13) labelattrs=(size=15);
y2axis label='VB-Spenden' values=(0 to 400 by 40) offsetmin=0 valueattrs=(size=13) labelattrs=(size=15);
vbar KW2 / name='B1' response=Herst group=Produkt transparency=0.2;
vline KW2 / name='LP' response=AusgTK group=Produkt lineattrs=(thickness=2);
vbar KW2 / name='B2' response=SpendVB group=Produkt transparency=0.4 nooutline barwidth=0.4 fillattrs=(color=grey) y2axis;
keylegend 'B1' / noborder valueattrs=(size=12) exclude=('TK' 'VB') position=bottomleft;
keylegend 'LP' / noborder valueattrs=(size=12) exclude=( 'TKP' 'TKZ' 'VB') position=bottom;
keylegend 'B2' / noborder valueattrs=(size=12) exclude=('TK' 'TKP' 'TKZ' ) position=bottomright;
run;
I added the NAME= option to the plots and separate KEYLEGEND statements for each plot, referring to the (arbitrary) names, using the EXCLUDE= option to remove the unwanted items and the POSITION= option for the positioning. Moreover, I omitted the LEGENDLABEL= options because they have "no effect if you also specify the GROUP= option in the same plot statement" (according to the documentation).
Hi Reinhard, perfect, that works great! Thanks a lot!
Also thanks to the other answerers!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.