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

I need a way to produce only a legend which can be used on a panel in a template I have created.  It's nothing fancy, just a title, 4 colored squares (with Hex or color name specified) with some text next to each.  I've seen some SAS programming use gplot to produce only a legend, but that isn't working for my purposes.  I came across Lex Jansen's page on it, but wasn't able to use that either.  Anyone have some canned SAS code that will do this?  I just need a way to call it in PROC GREPLAY so that I can add it to a template.

1 ACCEPTED SOLUTION

Accepted Solutions
GraphGuy
Meteorite | Level 14

If you don't want to go with creating a custom legend with annotate, here's a way you might use a gplot legend... This example creates a gmap and suppresses the gmap legend, then creates a gplot using the same data (keeping the gplot legend, basically suppressing the gplot graph). You'll probably have some variable other than statecode to color the graphs by.

 

I only played around with this for a few minutes - you can probably make some improvements! 🙂

 

data foo; set sashelp.us_data (where=(statecode in ('NC' 'SC' 'GA')));
run;

 

goptions xpixels=500 ypixels=500;
proc gmap data=foo map=maps.us;
id statecode;
choro statecode / discrete nolegend;
run;

 

symbol1 font='albany amt/unicode' value='25a0'x interpol=none h=.00001;
legend1 label=('My Legend') shape=symbol(.25in,.25in) repeat=1;
title; footnote;
goptions xpixels=600 ypixels=50;
proc gplot data=foo;
plot population_2010*population_2010=statecode / legend=legend1
noaxes noframe;
run;

 

map_legend.pngplot_legend.png

View solution in original post

10 REPLIES 10
ballardw
Super User

It may help to show the "template" you have created so we have some idea what you are working with.

 

It would likely be a good idea to show what a completed graph with this might look like and the bits that you are asking for highlighted.

Reeza
Super User
Are you using GTL, SGPLOT or GPLOT? Or something else? That will matter....
RandoDando
Pyrite | Level 9
I am using GMAP (nolegend option) and GSLIDE outputs on the template.
Legend would apply to the GMAP outputs universally.
Reeza
Super User
Ok. I think I don't know enough to answer your question. I would have assumed that you could just use GMAP and then get the legend within there customized. I would use formats to get the hex code shown as desired instead of the labels.
https://robslink.com/SAS/democd66/aaaindex.htm

Either way, I've moved this post to the graphics forum so hopefully one of the graphics guru's can assist you soon 🙂

FYI - SGMAP makes much better maps these days.
Jay54
Meteorite | Level 14

This should be doable using SGPLOT procedure with a "dummy" scatter plot statement and sashelp.class.  The dummy plot is needed to allow the plot output to be created.  Transparency=1 can be used to suppress any actual plot display.  Other options can be used to suppress axes and borders.

 

Then, use one or more LEGENDITEM statements to define named legend entries you need, including color, type of item and the associated text.  Then, add the names of the legend items to the KEYLEGEND statement.

 

You can do the same with GTL too. 

 

See use of LegendItem in this example:

https://blogs.sas.com/content/graphicallyspeaking/2017/10/27/legend-items/

 

Also see documentation:

https://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=9.4_3.5&docsetId=grstatproc&docsetTarget=p...

 

https://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=9.4_3.5&docsetId=grstatproc&docsetTarget=p...

 

 

Jay54
Meteorite | Level 14

Here is an example:

ods graphics / noborder;
proc sgplot data=sashelp.class noborder;
scatter x=height y=weight / group=sex transparency=1;
xaxis display=none;
yaxis display=none;
legenditem type=fill name='Item1' / fillattrs=graphdata2 label='Custom 1';
legenditem type=line name='Item2' / lineattrs=graphdata3 label='Custom 2';
keylegend 'Item1' 'Item2' / scale=2;
run;
RandoDando
Pyrite | Level 9

Oh no.

Was the LEGENDITEM option added recently? When I copy this code and run it those options appear in red and I get an invalid statement error for those.

 

Looks like we may have to call upon the macro gurus...

GraphGuy
Meteorite | Level 14

Since you're using SAS/Graph (gmap, gslide, greplay), you will not be able to easily use ODS Graphics (sgplot) with it.

 

One solution I've used to get a second legend on a separate page in SAS/Graph is to suppress the legend in the real graph (nolegend), and then use 'annotate' on a blank gslide to fabricate my own custom legend on a separate page. Here's an example - the outtput, and then the code:

 

http://robslink.com/SAS/democd16/topper.htm

http://robslink.com/SAS/democd16/topper.sas

 

GraphGuy
Meteorite | Level 14

If you don't want to go with creating a custom legend with annotate, here's a way you might use a gplot legend... This example creates a gmap and suppresses the gmap legend, then creates a gplot using the same data (keeping the gplot legend, basically suppressing the gplot graph). You'll probably have some variable other than statecode to color the graphs by.

 

I only played around with this for a few minutes - you can probably make some improvements! 🙂

 

data foo; set sashelp.us_data (where=(statecode in ('NC' 'SC' 'GA')));
run;

 

goptions xpixels=500 ypixels=500;
proc gmap data=foo map=maps.us;
id statecode;
choro statecode / discrete nolegend;
run;

 

symbol1 font='albany amt/unicode' value='25a0'x interpol=none h=.00001;
legend1 label=('My Legend') shape=symbol(.25in,.25in) repeat=1;
title; footnote;
goptions xpixels=600 ypixels=50;
proc gplot data=foo;
plot population_2010*population_2010=statecode / legend=legend1
noaxes noframe;
run;

 

map_legend.pngplot_legend.png

RandoDando
Pyrite | Level 9
Thanks, Rob! That did the trick/

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

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

View all other training opportunities.

Discussion stats
  • 10 replies
  • 2421 views
  • 9 likes
  • 5 in conversation