BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
yelkenli
Calcite | Level 5

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_wkcat_namewpfwprod_bestmedian_bestprod_worstmedian_worst
202126soap20212615902339.8219690732.72
202127soap20212715902319.2519690721.02
202128soap20212815902325.35196907127.36
202129soap20212915902327.7819690780.6
202130soap20213015902323.3719690747.71
202131soap20213115902314.8719690782.72
202132soap20213215902329.08196907144.06
202133soap20213315902322.7196907102.94
202126shampoo20212645837221.991206423.19
202127shampoo20212745837221.8891206434.98
202128shampoo20212845837226.3191206450.86
202129shampoo20212945837224.291206428.21
202130shampoo20213045837224.7891206427.66
202131shampoo20213145837224.8791206438.59
202132shampoo2021324583723691206482.19
202133shampoo20213345837218.291206435.8

 

get

yelkenli_0-1659112161250.png

 

 

want

yelkenli_1-1659112224028.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
DanH_sas
SAS Super FREQ

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;

View solution in original post

2 REPLIES 2
DanH_sas
SAS Super FREQ

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;
DanH_sas
SAS Super FREQ

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.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1945 views
  • 2 likes
  • 2 in conversation