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.
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;
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.
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:
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;
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...
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
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;
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.
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.