When using PROC SGPLOT, it is simple enough to get unicode (or other special characters) in the labels for the legends. This can be accomplished in at least two ways; one by defining a style in PROC TEMPLATE (e.g. this example), or by specifying the labelattrs=GraphUnicodeText option (e.g. this example).
However, so far as I can tell, there is no similar method for getting these special characters to appear in the legend for PROC SGPLOT. Specifying the GraphUnicodeText option for the valueattrs in the KEYLEGEND statement has no apparent effect. Similarly, using the same PROC TEMPLATE approach as in the link I provided does not seem to impact the legend. The text still prints out the full "{unicode '2081'x}", etc. (And, no, the problem isn't with a misspecifed unicode or the wrong ODS ESCAPECHAR, because I can use the same exact unicode on the axis labels and have it appear as desired).
Does anybody have any idea how to force PROC SGPLOT to recognize unicode characters in the legend? What is it about the way SAS is rendering the legend that makes it different from the axes labels, such that it ignores the unicode commands? The traditional approaches don't seem to work, and I can't find any description of how to do this by searching around.
Here is a quick arbitrary example to demonstrate the problem:
proc format;
value uni 1="log~{unicode '2081'x}~{unicode '2080'x}x"
2="log~{unicode '2081'x}~{unicode '2080'x}y";
run;
data _example;
do x=1 to 10;
y=int(ranuni(0)*100);
if y>50 then g=2; else g=1;
output;
end;
format g uni.;
run;
ods escapechar='~';
proc sgplot data=_example;
xaxis minor label="log~{unicode '2081'x}~{unicode '2080'x}x"
labelattrs=GraphUnicodeText;
yaxis minor label="log~{unicode '2081'x}~{unicode '2080'x}y"
labelattrs=GraphUnicodeText;
scatter x=x y=y / group=g;
run;
Hi,
As with anything Graph related, Sanjay and his blog have the answer:
http://blogs.sas.com/content/graphicallyspeaking/?s=unicode+legend
Hi,
As with anything Graph related, Sanjay and his blog have the answer:
http://blogs.sas.com/content/graphicallyspeaking/?s=unicode+legend
Well, first of all let me say I am suprirsed that this article didn't appear when I Googled "PROC SGPLOT UNICODE LEGEND". My Google-fu is weak.
Thanks!
I guess I posted too soon. I can't replicate his examples. Apparently, this functionality only works with "SAS 9.4 Maintenance release 3". Otherwise, these options won't work at all. Any idea if there is a workaround?
Yes, quite a bit of functionality came in for th 9.4 release with graphs. Personally, I do not know of a way to do this. You could post a comment on that post, but I think the best suggestion is to move to 9.4, it has plenty of new functionality.
I actually DO have 9.4, the problem is I only have 9.4 maintenance release 2, whereas apparently for this very specific functionality I need release 3. So close, yet so far! I'll have to talk to my department about getting upgraded. Thanks!
Hi Ryan,
One way that Unicode can be adding in to the legend with the current version of SAS you have is to use GTL and to use the legendlabel in Scatterplot, and also to use the option valueattrs = GraphUnicodeText in the discretelegend statement. With legendlabel you can include the unicode similarly to how you would do in in a label within the titles. The only thing is that if you have grouped data you need to unstack it first to add the separate unicode value in the legend, which has effectively been done with the expressions below.
proc template;
define statgraph unicodelegend;
begingraph;
layout overlay / yaxisopts = (label = "log~{unicode '2081'x}~{unicode '2080'x}y" labelattrs=GraphUnicodeText) xaxisopts = (label="log~{unicode '2081'x}~{unicode '2080'x}x" labelattrs=GraphUnicodeText);
scatterplot y = eval(ifn(g = 1, y, .)) x = eval(ifn(g = 1, x, .)) / name = "leg" legendlabel = "log~{unicode '2081'x}~{unicode '2080'x}x" markerattrs = (color = GRAPHDATA1:contrastcolor symbol = GRAPHDATA1:markersymbol);
scatterplot y = eval(ifn(g = 2, y, .)) x = eval(ifn(g = 2, x, .)) / name = "leg2" legendlabel = "log~{unicode '2081'x}~{unicode '2080'x}y" markerattrs = (color = GRAPHDATA2:contrastcolor symbol = GRAPHDATA2:markersymbol);
discretelegend "leg" "leg2" / valueattrs = GraphUnicodeText;
endlayout;
endgraph;
end;
run;
proc sgrender data = _example template = unicodelegend;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.