proc sgplot data=mydata;
by CD;
styleattrs datacontrastcolors=(red orange green blue purple black);
series x=var1 y=var2 / group=prof;
keylegend / location=outside position=right across=1 title="Profile";
I am making a series of graphs via BY option. If there are multiple groups per graph, the legend stacks up nicely with it's title on top (picture on the left). If there is only one group, legend title is moved to the left of the single key (picture on the right). This makes the legend box wider and re-scales the graph box. How to keep the legend title above the key?
Note, that "across=1" option doesn't help.
Hello @Vic3,
Here is another workaround: Add a dummy observation to each BY group with only one PROF value and define the line colors via an attribute map (rather than the STYLEATTRS statement) including "white" for the dummy value prof=.. As a result, the modified legends will contain a bit of blank space for the invisible second item, but always with the title ("Profile") at the top.
Example:
/* Add an observation with missing PROF into each CD BY-group with only one PROF value */ data want(drop=_:); do until(last.cd); set mydata; by cd; output; if first.cd then _p=prof; else if _p ne prof then _f=1; end; if not _f; call missing(prof,var1,var2); output; run; /* Define line colors via an attribute map */ data myattrmap; input ID $ value linecolor $; datalines; myid . white myid 1 red myid 2 orange myid 3 green myid 4 blue myid 5 purple myid 6 black ; /* Create graph with invisible second legend item where needed */ options missing=' '; proc sgplot data=want dattrmap=myattrmap; by CD; series x=var1 y=var2 / group=prof attrid=myid; /* ... rest unchanged ... */ run; options missing='.';
See
https://blogs.sas.com/content/iml/2021/02/22/byvar-byval-keywords-sas-titles.html
and
https://blogs.sas.com/content/iml/2021/02/17/data-driven-titles-sgpanel.html
Hopefully you'll get some tips there.
/*
Next time if you want ask a question,
Better post some sample data to test your code .
Your question is very interesting.
I think you should talk to SAS support ,like @DanH_sas
Or start a vote/ballot to let SAS change this behavior.
Here is a workaround way,but you need plot these graphics individually.
*/
data mydata(index=(sex));
set sashelp.class(where=(age in (12 14)));
if sex='F' and age ne 14 then delete;
run;
%sganno
data sganno;
%SGTEXT(LABEL="Profile", DRAWSPACE="LAYOUTPERCENT" ,JUSTIFY="LEFT" , X1=92, Y1=58)
run;
proc sgplot data=mydata sganno=sganno;
title 'Female';
where sex='F';
styleattrs datacontrastcolors=(red orange green blue purple black);
series x=weight y=height / group=age ;
keylegend /location=outside position=right across=1 noborder title='' ;
run;
data sganno;
%SGTEXT(LABEL="Profile", DRAWSPACE="LAYOUTPERCENT" ,JUSTIFY="LEFT" , X1=92, Y1=60)
run;
proc sgplot data=mydata sganno=sganno;
title 'Male';
where sex='M';
styleattrs datacontrastcolors=(red orange green blue purple black);
series x=weight y=height / group=age ;
keylegend /location=outside position=right across=1 noborder title='' ;
run;
Hello @Vic3,
Here is another workaround: Add a dummy observation to each BY group with only one PROF value and define the line colors via an attribute map (rather than the STYLEATTRS statement) including "white" for the dummy value prof=.. As a result, the modified legends will contain a bit of blank space for the invisible second item, but always with the title ("Profile") at the top.
Example:
/* Add an observation with missing PROF into each CD BY-group with only one PROF value */ data want(drop=_:); do until(last.cd); set mydata; by cd; output; if first.cd then _p=prof; else if _p ne prof then _f=1; end; if not _f; call missing(prof,var1,var2); output; run; /* Define line colors via an attribute map */ data myattrmap; input ID $ value linecolor $; datalines; myid . white myid 1 red myid 2 orange myid 3 green myid 4 blue myid 5 purple myid 6 black ; /* Create graph with invisible second legend item where needed */ options missing=' '; proc sgplot data=want dattrmap=myattrmap; by CD; series x=var1 y=var2 / group=prof attrid=myid; /* ... rest unchanged ... */ run; options missing='.';
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!
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.