Hi, Me again!
Correct me if I missed an option.
Create a line break in legend items is not possible.
The solution with escape character working in an axis label won't work in legend items.
There is no fitpolicy option when it comes to the keylegend statement or legenditem statement.
ods escapechar='^' ;
proc format;
value age 11-13='Group^Young'
14-16='Group^Old';
run;
proc sort data=sashelp.class out=class;
by sex age;
run;
proc sgplot data=class;
vbarbasic sex / response=height stat=mean group=age;
format age age.;
keylegend / position=right;
run;
proc sgplot data=class;
vbarbasic sex / response=height stat=mean group=age;
format age age.;
legenditem type=fill name='1'
/ label='Group^Young';
legenditem type=fill name='2'
/ label='Group^Old';
keylegend '1' '2'/ position=right;
run;
Maybe a bit of a kludge
proc sgplot data=class ; vbarbasic sex / response=height stat=mean group=age; format age age.; legenditem type=fill name='1' / label='Group' fillattrs=graphdata1; legenditem type=text name='a' / label='Young'; legenditem type=Fill name='2' / label='Group' fillattrs=graphdata2; legenditem type=text name='b' / label='Old'; keylegend '1' 'a' '2' 'b'/ position=right; run;
You don't have the split in PROC FORMAT correct.
You want
ods escapechar='^' ;
proc format;
value age 11-13="Group^{newline}Young"
14-16="Group^{newline}Old";
run;
so this works in PROC PRINT and PROC REPORT and similar types of output. Stealing from @Ksharp here, I can even get escape characters to work in SGPLOT in axis labels and titles and footnotes using the (*ESC*) delimiter, but I can't get it to work in SGPLOT legends.
proc sgplot data=class;
vbarbasic sex / response=height stat=mean group=age;
format age age.;
keylegend / position=right;
yaxis label="Height(*ESC*){unicode '000A'x}Inches";
run;
Note: I can get other unicode characters to work where the line break is supposed to be. For example, if instead of the line break, I want a "greater than or equal to sign" (Unicode 2265) even though it makes no sense in this context, this code works inside of legends. It just doesn't seem to work for line breaks.
proc format;
value age 11-13="Group(*ESC*){unicode '2265'x}Young"
14-16="Group(*ESC*){unicode '2265'x}Old";
run;
Maybe a bit of a kludge
proc sgplot data=class ; vbarbasic sex / response=height stat=mean group=age; format age age.; legenditem type=fill name='1' / label='Group' fillattrs=graphdata1; legenditem type=text name='a' / label='Young'; legenditem type=Fill name='2' / label='Group' fillattrs=graphdata2; legenditem type=text name='b' / label='Old'; keylegend '1' 'a' '2' 'b'/ position=right; run;
Correct -- there is not currently not an option to split legend labels. That being said, there are a couple of things to clear up for you regarding ODS ESCAPECHAR usage in ODS GRAPHICS.
Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.
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.
Ready to level-up your skills? Choose your own adventure.