BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Rhodochrosite | Level 12

Is there a good way to create an independent legend that you can use for multiple graphs in Enterprise guide 5.1 SAS 9.2?  So that all graphs use the same color schema and the legend is only shown once?


Accepted Solutions
Barite | Level 11

David, I have presented a paper that shows you how you can plot your own legend:

Otherwise, I agree with @ballardw suggestion to create a consistent legend.

View solution in original post

Super User

The ability to assign graphic attributes to values across procedures was one of the major additions to SG graphics in 9.3. In 9.2 careful ordering of the categorical variables, something ensuring they appeared with a missing or 0 value depending on the procedure usually let me do this IF I pre-summarized the data. A sum of one value is that value ...

The legend issue, assuming you can get the color schema to work, might be the option NOAUTOLEGEND on the procedures where you don't want the legend to appear.

Rhodochrosite | Level 12

This article might do the trick still playing with it.

Barite | Level 11

David, I have presented a paper that shows you how you can plot your own legend:

Otherwise, I agree with @ballardw suggestion to create a consistent legend.

Meteorite | Level 14

If you really want the result as in the linked sample, and not just an independent legend, proc SGPANEL is perfectly suited for this.  It would produce a common legend, all in probably 4-5 lines of code.  Use same data set as in the sample program.  Should work with SAS 9.2.

ods graphics / reset width=5in height=3in attrpriority=color';

proc sgpanel data=one;

  panelby grp;

  series x=x y=y / group=z markers;



Rhodochrosite | Level 12

In my particular case I need to charge the scale between the various graphs.  It looks like sgpanel provides one scale for multiple graphs.

Meteorite | Level 14

You can set the UNIFORM option to get  different scaling when the axis is not shared.

Else, you can use proc SGSCATTER with PLOT statement to get fully independent plots cells.

Rhodochrosite | Level 12

I need to include different where clauses to include different subsets of data.  Sgpannel has the group clause however it does not have the option to manually specify different subsets.

E.g. only show summation total, show all possibilities. 

I’m comparing similar graphs using line charts and bar graphs.  Sgscatter is for scatterplots.


Actually. if you use the JOIN option on the PLOT or COMPARE statements in SGSCATTER, you can get line plots as well.

Rhodochrosite | Level 12

Could you post a link to an example of sgscatter using a line graph without the typical scatter plots? I am using a summation clause in my current plots, the sgscatter functions appear to be for plotting data points rather than summations.  The compare feature compares two columns, I am trying to compare two different subsets using a where clause.


Here are some simple examples:

proc means data=sashelp.class;

class age;

var weight height;

output out=temp mean=;


proc sgscatter  data=temp;

plot (weight height)*age / join;


proc sgscatter data=temp;

compare x=age y=(weight height) / join;


Rhodochrosite | Level 12

Can I use the same procedure to do:

Graph 1

line for the weight for age 11

line for the weight for age 12

line for the weight for age 13

Then beside it using the same legend

Graph 2

line for the weight for age 14 only including where mean > 55

line for the weight for age 15 only including where mean > 55

line for the weight for age 16 only including where mean > 55

In other words can I use where clause like functions?

Rhodochrosite | Level 12

I’ve been working off of djrisks’ paper.  I was able to use it to create an independent series plot.  I’m currently working on adding manual sorting and coloring to the legend.  Note that the base part of the code is this:  the additional grouping is not needed for most situations.

data enroll_led;

set enroll_le;

index_final = _n_;


data legend;

set enroll_led;

/* For patient legend */

do i = 6, 10;

xax = i;



drop i;


ods graphics on / reset = all border = off imagefmt = png width = 3in height = 2in imagename

= "grlegend";

goptions reset = all;

proc template;

                define statgraph order;

                 dynamic TITLE1 TITLE2;


                 entrytitle TITLE1;

                 entrytitle TITLE2;

                layout overlay / xaxisopts =(display = NONE) yaxisopts = (display = NONE)

                y2axisopts = ( display = STANDARD reverse = true label = " "

                linearopts= ( tickvaluesequence=(start=1 end=32 increment=1)));

                /* Main plot */

                seriesplot x=xax y= degree_level / yaxis = y2 display = all group=degree_level

                index = index_final lineattrs = (pattern =1 thickness = 1) /*markerattrs = (size = 1pt)*/;





                proc sgrender data= legend template=order;

                     dynamic title1=" ";

                     dynamic title2="Legend";


Barite | Level 11

David, I'm pleased my paper helped you! For clarification, what would you like to do now?

Rhodochrosite | Level 12

My overall goal which now is achievable given your paper, a bit of additional research, and proof of concepts; is to have a legend for a number of small dynamic dashboard graphs.  The legend needs to match the colors displayed on the graphs.  The graphs need to change display sets based on input values affecting their where clauses. The legend needs to have a predefined order that is non-alphabetical.

My plan is create a hardcoded color pattern set.

Assign the color pattern set to the legend. 

Order the legend by a database index key.

Make the legend only show the colors that are currently being used.

Sort the dataset and assign the color pattern to the dataset each time I change data subsets, when input values are changed.  (This is inefficient but appears to be the best way).

Add line charts (sgplot) and barcharts (gcharts)

I’m using SAS 9.2 and EG 5.1



Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg



Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 21 replies
  • 5 in conversation