How can I display the name of a series in the legend instead of the grouped variable values in PROC SGPLOT?

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

How can I display the name of a series in the legend instead of the grouped variable values in PROC SGPLOT?

I am plotting the values for two series, peer salaries and UA average salaries by year and rank. I am using PROC SGPLOT and two series statements with the year as the x value and the salaries as the y value, then the rank is the group variable. I have SAS 9.2. I want the legend to simply show that the first series is the peer salary and the second is the UA average salary, not show the group (rank) values. Even using the legendlabel option, I can't seem to make this happen. My code and results are below. Suggestions are greatly appreciated!

proc sgplot data=faculty noautolegend;

  where uasrank in ('1','2','3');

  series x=uasyear y=uassug50/group=uasrank

                        markers          

  markerattrs=(symbol=circlefilled)

  lineattrs=(color=black pattern=solid)

  legendlabel="SUG 50th Percentile"

  name="sug50" datalabel=label;

  series x=uasyear y=uasuaavg/group=uasrank

  markers

  markerattrs=(symbol=squarefilled)

  lineattrs=(color=CX990000 pattern=solid)

  legendlabel="UA Average"

  name="uaavg";

  keylegend "sug50" "uaavg";

  xaxis label='';

  yaxis label='';

  format uasrank $fac_rank. uasyear $year. uassug50 dollar12. uasuaavg dollar12.;

run;

SGPlot4.png


Accepted Solutions
Solution
‎10-22-2012 10:22 AM
New Contributor
Posts: 2

Re: How can I display the name of a series in the legend instead of the grouped variable values in PROC SGPLOT?

Several people from SAS helped me with this, and I appreciate all their work. It looks like, as I always suspect, that you can do most anything in SAS even if not in a totally direct way. Here is the answer that worked for me, given by Sanjay Matange:

data faculty;

  set faculty;

  a=.;

  if _n_ = 1 then a=0;

  run;

proc sgplot data=faculty noautolegend;

      where uasrank in ('1','2','3');

      series x=uasyear y=uassug50/group=uasrank

                                                markers

                                                markerattrs=(symbol=circlefilled)

                                                lineattrs=(pattern=solid color=black)

                                                curvelabel

   curvelabelpos=start;         

      series x=uasyear y=uasuaavg/group=uasrank

                                                markers

                                                markerattrs=(symbol=squarefilled)

                                                lineattrs=(pattern=solid color=CX990000)

  curvelabel

  curvelabelpos=end;

   series x=uasyear y=a / lineattrs=graphdata1(color=black pattern=solid)

             name='a' legendlabel="SUG 50th Percentile";

   series x=uasyear y=a / lineattrs=graphdata2(color=CX990000 pattern=solid)

             name='b' legendlabel="UA Average";

      keylegend "a" "b";

      xaxis label=' ';

      yaxis label=' ' min=50000;;

      format  uasyear $year. uasrank $fac_rank. uassug50 dollar12. uasuaavg dollar12.;

run;

SGPlot4.png

View solution in original post


All Replies
Respected Advisor
Posts: 4,927

Re: How can I display the name of a series in the legend instead of the grouped variable values in PROC SGPLOT?

You could use two KEYLEGEND statements with different titles. If you specify the same location and position for both legends they will be correctly stacked. (hope this is not new to 9.3)

PG

PG
Solution
‎10-22-2012 10:22 AM
New Contributor
Posts: 2

Re: How can I display the name of a series in the legend instead of the grouped variable values in PROC SGPLOT?

Several people from SAS helped me with this, and I appreciate all their work. It looks like, as I always suspect, that you can do most anything in SAS even if not in a totally direct way. Here is the answer that worked for me, given by Sanjay Matange:

data faculty;

  set faculty;

  a=.;

  if _n_ = 1 then a=0;

  run;

proc sgplot data=faculty noautolegend;

      where uasrank in ('1','2','3');

      series x=uasyear y=uassug50/group=uasrank

                                                markers

                                                markerattrs=(symbol=circlefilled)

                                                lineattrs=(pattern=solid color=black)

                                                curvelabel

   curvelabelpos=start;         

      series x=uasyear y=uasuaavg/group=uasrank

                                                markers

                                                markerattrs=(symbol=squarefilled)

                                                lineattrs=(pattern=solid color=CX990000)

  curvelabel

  curvelabelpos=end;

   series x=uasyear y=a / lineattrs=graphdata1(color=black pattern=solid)

             name='a' legendlabel="SUG 50th Percentile";

   series x=uasyear y=a / lineattrs=graphdata2(color=CX990000 pattern=solid)

             name='b' legendlabel="UA Average";

      keylegend "a" "b";

      xaxis label=' ';

      yaxis label=' ' min=50000;;

      format  uasyear $year. uasrank $fac_rank. uassug50 dollar12. uasuaavg dollar12.;

run;

SGPlot4.png

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 1083 views
  • 0 likes
  • 2 in conversation