I have the below code and some example data. The resulting sgplot line graph shows only the variable value of the first series statement (in this case the _best), in the legend. I would like the legend to show both best and worst group variable values. Any suggestions?
proc sgplot data=best_worst_stats ;
by cat_name;
title "Trend of median over week for #byval1";
series x=fiscal_wk y=median_best / group=prod_best;
series x=fiscal_wk y=median_worst / group=prod_worst;
run;
some example data
fiscal_wk | cat_name | wpfw | prod_best | median_best | prod_worst | median_worst |
202126 | soap | 202126 | 159023 | 39.82 | 196907 | 32.72 |
202127 | soap | 202127 | 159023 | 19.25 | 196907 | 21.02 |
202128 | soap | 202128 | 159023 | 25.35 | 196907 | 127.36 |
202129 | soap | 202129 | 159023 | 27.78 | 196907 | 80.6 |
202130 | soap | 202130 | 159023 | 23.37 | 196907 | 47.71 |
202131 | soap | 202131 | 159023 | 14.87 | 196907 | 82.72 |
202132 | soap | 202132 | 159023 | 29.08 | 196907 | 144.06 |
202133 | soap | 202133 | 159023 | 22.7 | 196907 | 102.94 |
202126 | shampoo | 202126 | 458372 | 21.9 | 912064 | 23.19 |
202127 | shampoo | 202127 | 458372 | 21.88 | 912064 | 34.98 |
202128 | shampoo | 202128 | 458372 | 26.31 | 912064 | 50.86 |
202129 | shampoo | 202129 | 458372 | 24.2 | 912064 | 28.21 |
202130 | shampoo | 202130 | 458372 | 24.78 | 912064 | 27.66 |
202131 | shampoo | 202131 | 458372 | 24.87 | 912064 | 38.59 |
202132 | shampoo | 202132 | 458372 | 36 | 912064 | 82.19 |
202133 | shampoo | 202133 | 458372 | 18.2 | 912064 | 35.8 |
get
want
In two separate legends:
proc sgplot data=best_worst_stats ;
by cat_name;
title "Trend of median over week for #byval1";
series x=fiscal_wk y=median_best / group=prod_best name="best";
series x=fiscal_wk y=median_worst / group=prod_worst name="worst";
keylegend "best" / position=bottomleft;
keylegend "worst" / position=bottomright;
run;
In the same legend:
proc sgplot data=best_worst_stats ;
by cat_name;
title "Trend of median over week for #byval1";
series x=fiscal_wk y=median_best / group=prod_best name="best";
series x=fiscal_wk y=median_worst / group=prod_worst name="worst";
keylegend "best" "worst";
run;
In two separate legends:
proc sgplot data=best_worst_stats ;
by cat_name;
title "Trend of median over week for #byval1";
series x=fiscal_wk y=median_best / group=prod_best name="best";
series x=fiscal_wk y=median_worst / group=prod_worst name="worst";
keylegend "best" / position=bottomleft;
keylegend "worst" / position=bottomright;
run;
In the same legend:
proc sgplot data=best_worst_stats ;
by cat_name;
title "Trend of median over week for #byval1";
series x=fiscal_wk y=median_best / group=prod_best name="best";
series x=fiscal_wk y=median_worst / group=prod_worst name="worst";
keylegend "best" "worst";
run;
Let me explain the behavior you saw:
When a plot request has a GROUP variable, the automatic legend in SGPLOT and SGPANEL displays only the first grouped plot in the legend. If no group variables are present, all plots are added to the legend by default. However, you can alway override the default legend behavior by naming your plot overlays and adding them to one or more legends.
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.