<?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: How to change legend label for stacked bar chart in proc sgplot in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-change-legend-label-for-stacked-bar-chart-in-proc-sgplot/m-p/613362#M179109</link>
    <description>&lt;P&gt;Hi again Jag,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Unfortunately the issue is the same: I can get a correct legend, but the color mapping disappears. I was though able to resolve the issue based on another solution in this thread. It is a very manual solution, but . nonetheless solved the issue.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But thank you for your help!&lt;/P&gt;</description>
    <pubDate>Fri, 20 Dec 2019 23:28:00 GMT</pubDate>
    <dc:creator>Birgithj</dc:creator>
    <dc:date>2019-12-20T23:28:00Z</dc:date>
    <item>
      <title>How to change legend label for stacked bar chart in proc sgplot</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-change-legend-label-for-stacked-bar-chart-in-proc-sgplot/m-p/613174#M179050</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am oping you are able to help me with below problem. I have a dataset containing 11 variables. have plotted the variables using a stacked bar chart with the date on the x-axis.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now I want to change the name of the variables in the keylegend box (i.e. Bakk =&amp;nbsp;Bakken (ND &amp;amp; MT), Eagl=Eagle Ford (TX)&amp;nbsp; etc). How can I do this? Below I have pasted my code as well as the figure.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;***Import data***;
PROC IMPORT DATAFILE="/folders/myfolders/Speciale/oil/shale oil by basin.xlsx"
  OUT=shale_prod
  DBMS=XLSX
  REPLACE;
  ***Convert from long to wide format***;
proc sort data=shale_prod out=Wide;
  by date;
run;

proc transpose data=Wide
  out=Long(rename=(Col1=Value))
  name=Source;
  by date;
  var Eagl Spra Bakk Wolf Bone Niob Miss Aust Wood Rest;
run;

***Assign colors to variables***;
data myattrmap;
  length linecolor $ 9 fillcolor $ 9;
  input ID $ value $ linecolor $ fillcolor $;
  datalines;
myid Eagl VLIGB VLIGB
myid Miss brgr brgr
myid Rest vib vib
myid Spra MOGB MOGB
myid Bakk vigb vigb
myid Bone BLUE BLUE
myid Aust BIGB BIGB
myid Niob PKGR PKGR
myid Wood STOY STOY
myid Wolf PAB PAB
;

***Plot stacked bar chart***;
ods graphics on / width=7in height=4.5in maxlegendarea=60;

proc sgplot data=Long dattrmap=myattrmap;
  vbar date / response=Value group=Source groupdisplay=stack grouporder=data attrid=myid;
  xaxis type=time thresholdmin=0 max=21100 display=(nolabel);
  yaxis grid values=(0 to 7) GRIDATTRS=(color=white) label="Million barrels per day";
  keylegend /title="";
run;&lt;/CODE&gt;&lt;/PRE&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="Screen Shot 2019-12-20 at 00.50.01.png" style="width: 596px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/34971iEA91EA5C77D0FD64/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screen Shot 2019-12-20 at 00.50.01.png" alt="Screen Shot 2019-12-20 at 00.50.01.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Dec 2019 13:20:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-change-legend-label-for-stacked-bar-chart-in-proc-sgplot/m-p/613174#M179050</guid>
      <dc:creator>Birgithj</dc:creator>
      <dc:date>2019-12-20T13:20:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to change legend label for stacked bar chart in proc sgplot</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-change-legend-label-for-stacked-bar-chart-in-proc-sgplot/m-p/613182#M179052</link>
      <description>&lt;P&gt;The easiest and best approach would be to use a format on the source variable as below and then apply that format in sgplot to source variable&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format ;
value $src 
'Bakk'='Bakken (ND &amp;amp; MT)'
'Eagl'='Eagle Ford (TX)';
run;

ods graphics on / width=7in height=4.5in maxlegendarea=60;
proc sgplot data=Long dattrmap=myattrmap;
vbar date / response=Value group=Source groupdisplay=stack grouporder=data attrid=myid;
xaxis type=time thresholdmin=0 max=21100 display=(nolabel);
yaxis grid values=(0 to 7) GRIDATTRS=(color=white) label="Million barrels per day";
keylegend /title="";
&lt;STRONG&gt;format source $src.;&lt;/STRONG&gt;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 20 Dec 2019 02:30:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-change-legend-label-for-stacked-bar-chart-in-proc-sgplot/m-p/613182#M179052</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2019-12-20T02:30:25Z</dc:date>
    </item>
    <item>
      <title>Re: How to change legend label for stacked bar chart in proc sgplot</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-change-legend-label-for-stacked-bar-chart-in-proc-sgplot/m-p/613199#M179060</link>
      <description>&lt;P&gt;Hi Jag,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your reply. While this solves the problem with the key legend, unfortunately this solution disables the custom colouring of the stacked bar chart using the 'myattrmap' code, which is vital for the figure. Is there a way to avoid this?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Dec 2019 08:49:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-change-legend-label-for-stacked-bar-chart-in-proc-sgplot/m-p/613199#M179060</guid>
      <dc:creator>Birgithj</dc:creator>
      <dc:date>2019-12-20T08:49:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to change legend label for stacked bar chart in proc sgplot</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-change-legend-label-for-stacked-bar-chart-in-proc-sgplot/m-p/613224#M179074</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dattrmap;
input id $ value $ fillcolor $;
cards;
id F blue
id M green
;

proc sgplot data=sashelp.class dattrmap=dattrmap;
vbar age/group=sex groupdisplay=stack attrid=id;
legenditem type=FILL name="F" /label='Female' fillattrs=(color=blue);
legenditem type=FILL name="M" /label='Male' fillattrs=(color=green);
keylegend "F" "M";
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 20 Dec 2019 12:01:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-change-legend-label-for-stacked-bar-chart-in-proc-sgplot/m-p/613224#M179074</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-12-20T12:01:41Z</dc:date>
    </item>
    <item>
      <title>Re: How to change legend label for stacked bar chart in proc sgplot</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-change-legend-label-for-stacked-bar-chart-in-proc-sgplot/m-p/613238#M179077</link>
      <description>&lt;P&gt;Alternatively you can try the below code where you derive a new variable source2 and use this variable in place of source&lt;/P&gt;
&lt;P&gt;could you please try the let me know if this resolves the issue&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format ;
value $src 
'Bakk'='Bakken (ND &amp;amp; MT)'
'Eagl'='Eagle Ford (TX)';
run;

data Long ;
set Long ;
source2=put(source,$src.);
run;

ods graphics on / width=7in height=4.5in maxlegendarea=60;
proc sgplot data=Long dattrmap=myattrmap;
vbar date / response=Value group=Source2 groupdisplay=stack grouporder=data attrid=myid;
xaxis type=time thresholdmin=0 max=21100 display=(nolabel);
yaxis grid values=(0 to 7) GRIDATTRS=(color=white) label="Million barrels per day";
keylegend /title="";
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 20 Dec 2019 14:32:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-change-legend-label-for-stacked-bar-chart-in-proc-sgplot/m-p/613238#M179077</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2019-12-20T14:32:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to change legend label for stacked bar chart in proc sgplot</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-change-legend-label-for-stacked-bar-chart-in-proc-sgplot/m-p/613272#M179092</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/290079"&gt;@Birgithj&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi Jag,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for your reply. While this solves the problem with the key legend, unfortunately this solution disables the custom colouring of the stacked bar chart using the 'myattrmap' code, which is vital for the figure. Is there a way to avoid this?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Modify the attrmap data set to use the formatted values, or likley better, create a second attribute list with the formatted values but a different attrid. Then you can switch back and forth between behaviors as needed by using format (or not) and the matching attrid. There really isn't much of a limit on how many different attrids can be in the attribute set just follow the rules:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;DIV class="xis-paraLeadIn" id="n0ll9w6ccmw85bn0zqkybzmw61b4"&gt;When a discrete &lt;FONT style="background-color: rgb(252, 222, 192);"&gt;attribute&lt;/FONT&gt; &lt;FONT style="background-color: rgb(252, 222, 192);"&gt;map&lt;/FONT&gt; data set contains multiple &lt;FONT style="background-color: rgb(252, 222, 192);"&gt;attribute&lt;/FONT&gt; maps:&lt;/DIV&gt;
&lt;DIV class="xis-listUnordered"&gt;
&lt;UL&gt;
&lt;LI&gt;
&lt;DIV class="xis-item" id="n1qyy5w2kb5dtfn1dvbe3ddrp8mk"&gt;
&lt;DIV class="xis-paraSimpleFirst" id="p1ks2qbi0v9pr5n13sr4gyhvqq6e"&gt;The ID variable has more than one value.&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV class="xis-item" id="p1dzdgr6atkee9n1hre74jiecixp"&gt;
&lt;DIV class="xis-paraSimpleFirst" id="n0zhahqp6kmu5fn15brt6z7xpn4e"&gt;The VALUE variable has different values that correspond to different data groups.&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV class="xis-item" id="p0ftk59m6iu5lxn13s6d82zb3zxz"&gt;
&lt;DIV class="xis-paraSimpleFirst" id="p0wh0331hb54w3n1bq8ox33nvihn"&gt;The ID values in the &lt;FONT style="background-color: rgb(252, 222, 192);"&gt;attribute&lt;/FONT&gt; &lt;FONT style="background-color: rgb(252, 222, 192);"&gt;map&lt;/FONT&gt; data set must be continuous (in a sorted order). If they are not, use the SORT procedure to sort the data set by ID, in ascending or descending order.&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/LI&gt;
&lt;/UL&gt;
&lt;/DIV&gt;
&lt;DIV class="xis-paragraph" id="p1aoyll67z5tv9n1fw0tg714thyw"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have some variables with as many as 6 value sets because sometimes we want longer text, sometimes shorter and sometimes just to have the difference between Show="Attrmap" and Show="Data". There are even times when the same variable is used in mutiple plots that you can use a different attrid for the same variable in the different plots (though legends may get entertaining)&lt;/P&gt;</description>
      <pubDate>Fri, 20 Dec 2019 16:21:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-change-legend-label-for-stacked-bar-chart-in-proc-sgplot/m-p/613272#M179092</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-12-20T16:21:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to change legend label for stacked bar chart in proc sgplot</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-change-legend-label-for-stacked-bar-chart-in-proc-sgplot/m-p/613362#M179109</link>
      <description>&lt;P&gt;Hi again Jag,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Unfortunately the issue is the same: I can get a correct legend, but the color mapping disappears. I was though able to resolve the issue based on another solution in this thread. It is a very manual solution, but . nonetheless solved the issue.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But thank you for your help!&lt;/P&gt;</description>
      <pubDate>Fri, 20 Dec 2019 23:28:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-change-legend-label-for-stacked-bar-chart-in-proc-sgplot/m-p/613362#M179109</guid>
      <dc:creator>Birgithj</dc:creator>
      <dc:date>2019-12-20T23:28:00Z</dc:date>
    </item>
  </channel>
</rss>

