BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Vic3
Fluorite | Level 6
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.

 

SGplot_legend.jpg

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

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:

legend_w_dummy_item.png

/* 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='.';

View solution in original post

3 REPLIES 3
Ksharp
Super User
/*
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;

Ksharp_0-1675431342903.pngKsharp_1-1675431359096.png

 

FreelanceReinh
Jade | Level 19

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:

legend_w_dummy_item.png

/* 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='.';

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 676 views
  • 2 likes
  • 4 in conversation