<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: SGPLOT legend title, keep on top in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SGPLOT-legend-title-keep-on-top/m-p/857056#M338604</link>
    <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/296354"&gt;@Vic3&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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 &lt;FONT face="courier new,courier"&gt;prof=.&lt;/FONT&gt;. 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.&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="legend_w_dummy_item.png" style="width: 69px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/80084i81F85215587E877B/image-size/large?v=v2&amp;amp;px=999" role="button" title="legend_w_dummy_item.png" alt="legend_w_dummy_item.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;PRE&gt;/* 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;
&lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;myid . white&lt;/STRONG&gt;&lt;/FONT&gt;
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 */

&lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;options missing=' ';&lt;/STRONG&gt;&lt;/FONT&gt;

proc sgplot data=&lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;want&lt;/STRONG&gt;&lt;/FONT&gt; &lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;dattrmap=myattrmap&lt;/STRONG&gt;&lt;/FONT&gt;;
  by CD;
  series x=var1 y=var2 / group=prof &lt;STRONG&gt;&lt;FONT color="#3366FF"&gt;attrid=myid&lt;/FONT&gt;&lt;/STRONG&gt;;
  /* ... rest unchanged ... */
run;

options missing='.';&lt;/PRE&gt;</description>
    <pubDate>Fri, 03 Feb 2023 14:25:18 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2023-02-03T14:25:18Z</dc:date>
    <item>
      <title>SGPLOT legend title, keep on top</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SGPLOT-legend-title-keep-on-top/m-p/856976#M338583</link>
      <description>&lt;PRE&gt;&lt;CODE class=""&gt;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";&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;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?&lt;/P&gt;&lt;P&gt;Note, that "across=1" option doesn't help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SGplot_legend.jpg" style="width: 976px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/80066i0802BA2FE906FAC3/image-size/large?v=v2&amp;amp;px=999" role="button" title="SGplot_legend.jpg" alt="SGplot_legend.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Feb 2023 23:28:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SGPLOT-legend-title-keep-on-top/m-p/856976#M338583</guid>
      <dc:creator>Vic3</dc:creator>
      <dc:date>2023-02-02T23:28:31Z</dc:date>
    </item>
    <item>
      <title>Re: SGPLOT legend title, keep on top</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SGPLOT-legend-title-keep-on-top/m-p/857007#M338595</link>
      <description>&lt;P&gt;See&lt;/P&gt;
&lt;P&gt;&lt;A href="https://blogs.sas.com/content/iml/2021/02/22/byvar-byval-keywords-sas-titles.html" target="_blank"&gt;https://blogs.sas.com/content/iml/2021/02/22/byvar-byval-keywords-sas-titles.html&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;and&lt;/P&gt;
&lt;P&gt;&lt;A href="https://blogs.sas.com/content/iml/2021/02/17/data-driven-titles-sgpanel.html" target="_blank"&gt;https://blogs.sas.com/content/iml/2021/02/17/data-driven-titles-sgpanel.html&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Hopefully you'll get some tips there.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Feb 2023 10:48:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SGPLOT-legend-title-keep-on-top/m-p/857007#M338595</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2023-02-03T10:48:42Z</dc:date>
    </item>
    <item>
      <title>Re: SGPLOT legend title, keep on top</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SGPLOT-legend-title-keep-on-top/m-p/857031#M338600</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*
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;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ksharp_0-1675431342903.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/80073i682EA4973E478A8D/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ksharp_0-1675431342903.png" alt="Ksharp_0-1675431342903.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ksharp_1-1675431359096.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/80074iFBB4D7606A794F6C/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ksharp_1-1675431359096.png" alt="Ksharp_1-1675431359096.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Feb 2023 13:36:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SGPLOT-legend-title-keep-on-top/m-p/857031#M338600</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-02-03T13:36:14Z</dc:date>
    </item>
    <item>
      <title>Re: SGPLOT legend title, keep on top</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SGPLOT-legend-title-keep-on-top/m-p/857056#M338604</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/296354"&gt;@Vic3&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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 &lt;FONT face="courier new,courier"&gt;prof=.&lt;/FONT&gt;. 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.&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="legend_w_dummy_item.png" style="width: 69px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/80084i81F85215587E877B/image-size/large?v=v2&amp;amp;px=999" role="button" title="legend_w_dummy_item.png" alt="legend_w_dummy_item.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;PRE&gt;/* 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;
&lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;myid . white&lt;/STRONG&gt;&lt;/FONT&gt;
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 */

&lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;options missing=' ';&lt;/STRONG&gt;&lt;/FONT&gt;

proc sgplot data=&lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;want&lt;/STRONG&gt;&lt;/FONT&gt; &lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;dattrmap=myattrmap&lt;/STRONG&gt;&lt;/FONT&gt;;
  by CD;
  series x=var1 y=var2 / group=prof &lt;STRONG&gt;&lt;FONT color="#3366FF"&gt;attrid=myid&lt;/FONT&gt;&lt;/STRONG&gt;;
  /* ... rest unchanged ... */
run;

options missing='.';&lt;/PRE&gt;</description>
      <pubDate>Fri, 03 Feb 2023 14:25:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SGPLOT-legend-title-keep-on-top/m-p/857056#M338604</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2023-02-03T14:25:18Z</dc:date>
    </item>
  </channel>
</rss>

