Published on :
Reporting SASHELP

Advanced Customization of Graphic Legends with PROC SGPLOT

This code is also available in: Deutsch Español Français
This script illustrates how to modify the appearance of legends in PROC SGPLOT using the KEYLEGEND statement. It covers several use cases: adjusting line length, modifying symbol scale, controlling the aspect ratio of fill areas, and using custom formats containing Unicode characters ('greater than or equal to' symbol).
Data Analysis

Type : SASHELP


The script exclusively uses the standard SASHELP.CARS and SASHELP.HEART example tables.

1 Code Block
PROC FORMAT
Explanation :
Configuration of the graphical output environment (ODS) and creation of a custom 'agegroupUnicode' format that uses an escape sequence to display the 'greater than or equal to' mathematical symbol (Unicode 2265).
Copied!
1%let gpath='.';
2%let dpi=200;
3 
4ods html close;
5ods listing gpath=&gpath image_dpi=&dpi;
6 
7/*--Unicode Format--*/
8PROC FORMAT;
9 value agegroupUnicode
10 0 -< 40 = '< 40'
11 40 -< 50 = '40 < 50'
12 50 -< 60 = '50 < 60'
13 60 - high = "(*ESC*){unicode '2265'x} 60"
14 ;
15RUN;
2 Code Block
PROC SGPLOT
Explanation :
Generation of a basic polynomial regression graph to establish a visual reference for the default legend.
Copied!
1/*--Legend default--*/
2ods graphics / reset width=5in height=3in imagename='LegendDefault';
3title 'MSRP by Horsepower';
4PROC SGPLOT DATA=sashelp.cars(where=(type eq 'Sedan'));
5 styleattrs axisextent=DATA;
6 reg x=horsepower y=msrp / cli clm degree=2;
7RUN;
3 Code Block
PROC SGPLOT
Explanation :
Modification of the representative line length in the legend using the 'linelength=32' option in the KEYLEGEND statement.
Copied!
1/*--Legend Line Length--*/
2ods graphics / reset width=5in height=3in imagename='LegendLine';
3title 'MSRP by Horsepower';
4PROC SGPLOT DATA=sashelp.cars(where=(type eq 'Sedan'));
5 styleattrs axisextent=DATA;
6 reg x=horsepower y=msrp / cli clm degree=2;
7 keylegend / linelength=32;
8RUN;
4 Code Block
PROC SGPLOT
Explanation :
Combination of line length and the 'scale=1.2' option to globally enlarge legend elements.
Copied!
1/*--Legend Line Length and Swatch--*/
2ods graphics / reset width=5in height=3in imagename='LegendLineScale';
3title 'MSRP by Horsepower';
4PROC SGPLOT DATA=sashelp.cars(where=(type eq 'Sedan'));
5 styleattrs axisextent=DATA;
6 reg x=horsepower y=msrp / cli clm degree=2;
7 keylegend / linelength=32 scale=1.2;
8RUN;
5 Code Block
PROC SGPLOT
Explanation :
Precise control of legend swatch dimensions with 'fillheight' (height) and 'fillaspect' (aspect ratio, here the golden ratio).
Copied!
1/*--Legend Line Length and Swatch--*/
2ods graphics / reset width=5in height=3in imagename='LegendLineAspect';
3title 'MSRP by Horsepower';
4PROC SGPLOT DATA=sashelp.cars(where=(type eq 'Sedan'));
5 styleattrs axisextent=DATA;
6 reg x=horsepower y=msrp / cli clm degree=2;
7 keylegend / linelength=32 fillheight=2.5pct fillaspect=golden;
8RUN;
6 Code Block
PROC SGPLOT
Explanation :
Complex bar chart using the previously defined Unicode format. The legend is placed inside the graph ('location=inside'), rendered opaque, and its dimensions are customized to fit the 'dataskin=pressed' style.
Copied!
1/*--Legend Swatch--*/
2ods graphics / reset width=5in height=3in imagename='DeathsUnicode';
3title 'Counts by Death Cause and Age Group';
4PROC SGPLOT DATA=sashelp.heart(where=(deathcause ne 'Unknown')) nocycleattrs noborder;
5 FORMAT ageatdeath agegroupUnicode.;
6 vbar ageatdeath / group=deathcause groupdisplay=cluster fillattrs=(color=white);
7 vbar ageatdeath / group=deathcause groupdisplay=cluster nooutline
8 baselineattrs=(thickness=0) dataskin=pressed filltype=gradient name='a';
9 keylegend 'a' / location=inside across=1 title='' fillheight=2.5pct fillaspect=2.5 opaque;
10 xaxis display=(nolabel noline);
11 yaxis label='Count' grid display=(noline noticks);
12RUN;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.