<?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: Stacked bar chart with percentages in Graphics Programming</title>
    <link>https://communities.sas.com/t5/Graphics-Programming/Stacked-bar-chart-with-percentages/m-p/919356#M24430</link>
    <description>&lt;P&gt;You could also make some small modifications to your proc sql step and then use VBARPARM:&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;proc sql ;&lt;BR /&gt;create table want as&lt;BR /&gt;select a.year,a.state,sum(fail)/n as pct "% failed" FORMAT=PERCENT8.&lt;BR /&gt;from have as a&lt;BR /&gt;,(select year,n(fail) as n from have group by year) as b&lt;BR /&gt;where a.year=b.year&lt;BR /&gt;group by a.year,state&lt;BR /&gt;;&lt;BR /&gt;quit ;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;PROC SGPLOT DATA= WANT NOBORDER ;&lt;BR /&gt;styleattrs datacolors=(darkred darkblue) datacontrastcolors=(black) ;&lt;BR /&gt;vbarparm category= year response= pct / group= state seglabel seglabelattrs=(color=white weight=bold);&lt;BR /&gt;run ;&lt;/P&gt;&lt;P&gt;This gives you a stacked bar chart with each bar segment labelled with the percentage values from PROC SQL.&lt;/P&gt;&lt;P&gt;(Sorry- I'm new to the SAS community and not quite sure how to paste in the resulting graph.)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 07 Mar 2024 19:36:17 GMT</pubDate>
    <dc:creator>jeanleid</dc:creator>
    <dc:date>2024-03-07T19:36:17Z</dc:date>
    <item>
      <title>Stacked bar chart with percentages</title>
      <link>https://communities.sas.com/t5/Graphics-Programming/Stacked-bar-chart-with-percentages/m-p/917630#M24390</link>
      <description>&lt;P&gt;Maybe I'm just having a bad Friday, but I'm struggling to make a stacked bar chart, and it feels like the kind of struggling that maybe I'm doing something dumb...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have data with one record per person, with people from two states, and two years.&amp;nbsp; For each person, I have a boolean&lt;/P&gt;
&lt;P&gt;variable that indicates pass/fail.&amp;nbsp; Data like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
  input year state $2. Fail ;
  cards ;
2023 MA 1
2023 MA 1
2023 MA 0
2023 MA 0
2023 MA 0
2023 MA 0
2023 MA 0
2023 MA 0
2023 RI 1
2023 RI 0
2024 MA 1
2024 MA 1
2024 MA 1
2024 MA 1
2024 MA 0
2024 MA 0
2024 MA 0
2024 MA 0
2024 RI 1
2024 RI 1
;
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So there are 10 people in each year.&amp;nbsp; My goal is to make a bar chart that shows that 30% failed in 2023 and 60% failed in 2024.&amp;nbsp; I want it to be a stacked bar chart, with state as the group.&amp;nbsp; It should show 30% failed in 2023 (20% MA, 10%RI) and 60%failed in 2024 (40% MA, 20%RI).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As I couldn't make the chart I want directly from this data (but would be happy if you can), I first thought I should calculate the percentages I want myself.&amp;nbsp; Even that feels overly complex, because I need to get the sum of the failures for each state-year, and then divide that by the count of rows for the year.&amp;nbsp; I can get what I want with SQL, but it feels like it should be easier...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql ;
  create table want as
  select a.year,a.state,sum(fail)/n as pct
  from have as a
      ,(select year,n(fail) as n from have group by year) as b
  where a.year=b.year
  group by a.year,state
  ;
quit ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That gives:&lt;/P&gt;
&lt;PRE&gt;Obs    year    state    pct

 1     2023     MA      0.2
 2     2023     RI      0.1
 3     2024     MA      0.4
 4     2024     RI      0.2
&lt;/PRE&gt;
&lt;P&gt;Then to get my stacked bar chart:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sgplot data=want ;
  vbar  year/ response=pct group=state;
  format pct percent. ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Which gives me what I want:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Quentin_0-1708713097512.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/94048iEBD7FE958AC58B4F/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Quentin_0-1708713097512.png" alt="Quentin_0-1708713097512.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Is there a better/easier way to do this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The point is not to compare the failure rate for MA vs RI.&amp;nbsp; I'm not calculating that percentage.&amp;nbsp; The goal is to compare the failure rate by year.&amp;nbsp; And in each year, see how many of those failures came from MA vs RI.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Feb 2024 18:35:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Graphics-Programming/Stacked-bar-chart-with-percentages/m-p/917630#M24390</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2024-02-23T18:35:23Z</dc:date>
    </item>
    <item>
      <title>Re: Stacked bar chart with percentages</title>
      <link>https://communities.sas.com/t5/Graphics-Programming/Stacked-bar-chart-with-percentages/m-p/917637#M24391</link>
      <description>&lt;P&gt;I usually stay away from stacked bar charts.&amp;nbsp; Maybe the line chart version is easier to read(and make):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set have ;
  FailMa=(State='MA' and Fail) ;
  FailRI=(State='RI' and Fail) ;
run ;

proc sgplot data=want ;
  vline year /response=fail stat=mean ;
  vline year /response=failMA stat=mean ;
  vline year /response=failRI stat=mean ;
run ;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Quentin_0-1708714769437.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/94051iE4FF770E18875947/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Quentin_0-1708714769437.png" alt="Quentin_0-1708714769437.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Feb 2024 18:59:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Graphics-Programming/Stacked-bar-chart-with-percentages/m-p/917637#M24391</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2024-02-23T18:59:45Z</dc:date>
    </item>
    <item>
      <title>Re: Stacked bar chart with percentages</title>
      <link>https://communities.sas.com/t5/Graphics-Programming/Stacked-bar-chart-with-percentages/m-p/917643#M24393</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
  input year state $2. Fail ;
  cards ;
2023 MA 1
2023 MA 1
2023 MA 0
2023 MA 0
2023 MA 0
2023 MA 0
2023 MA 0
2023 MA 0
2023 RI 1
2023 RI 0
2024 MA 1
2024 MA 1
2024 MA 1
2024 MA 1
2024 MA 0
2024 MA 0
2024 MA 0
2024 MA 0
2024 RI 1
2024 RI 1
;
run ;

data have2;
  set have;
  state=".."; /* "extra" state to get 100% */
  Fail = NOT Fail;
run;

data have3;
  set have have2;
run;

ods graphics / reset attrpriority=color;
proc sgplot data=have3(where=(Fail)) PCTLEVEL=group ;
  styleattrs datacolors=(red blue white); /* last value white to hide in background */
  vbar  year / group=state 
  RESPONSE=fail 
  STAT=PERCENT 
  GROUPORDER=DATA /* to keep "extra" group "on top" */
  NOOUTLINE;
  yaxis max=0.6; /* to scale (optionally)*/
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="yabwon_0-1708717039481.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/94054iD45AB8A41C146919/image-size/medium?v=v2&amp;amp;px=400" role="button" title="yabwon_0-1708717039481.png" alt="yabwon_0-1708717039481.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Feb 2024 19:37:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Graphics-Programming/Stacked-bar-chart-with-percentages/m-p/917643#M24393</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2024-02-23T19:37:26Z</dc:date>
    </item>
    <item>
      <title>Re: Stacked bar chart with percentages</title>
      <link>https://communities.sas.com/t5/Graphics-Programming/Stacked-bar-chart-with-percentages/m-p/917649#M24394</link>
      <description>&lt;P&gt;Small update on "missing class" colour:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have2;
  set have have(in=i drop=state);
  if i then Fail = NOT Fail;
run;

data MyAttrMap;
  ID='grpMiss';
  value=' '; /* missing group */
  fillcolor='Black';
  filltransparency=1;
  output;
run;

proc sgplot 
  data=have2(where=(Fail)) 
  dattrmap=MyAttrMap 
  PCTLEVEL=group 
;
  vbar  year / group=state response=fail 
    STAT=PERCENT 
    GROUPORDER=DATA
    NOOUTLINE 
    MISSING 
    ATTRID=grpMiss
  ;
  yaxis max=0.6;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="yabwon_0-1708718587527.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/94055iE4C9D122CD96A234/image-size/medium?v=v2&amp;amp;px=400" role="button" title="yabwon_0-1708718587527.png" alt="yabwon_0-1708718587527.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Fri, 23 Feb 2024 20:09:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Graphics-Programming/Stacked-bar-chart-with-percentages/m-p/917649#M24394</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2024-02-23T20:09:45Z</dc:date>
    </item>
    <item>
      <title>Re: Stacked bar chart with percentages</title>
      <link>https://communities.sas.com/t5/Graphics-Programming/Stacked-bar-chart-with-percentages/m-p/917665#M24395</link>
      <description>&lt;P&gt;Very creative solution&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/35763"&gt;@yabwon&lt;/a&gt;&amp;nbsp;, I would have never thought of adding fake data to let vbar calculate the percentage I want via percentage of sum.&amp;nbsp; As Paul D. would say, thanks 1E6!&lt;/P&gt;</description>
      <pubDate>Fri, 23 Feb 2024 21:09:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Graphics-Programming/Stacked-bar-chart-with-percentages/m-p/917665#M24395</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2024-02-23T21:09:25Z</dc:date>
    </item>
    <item>
      <title>Re: Stacked bar chart with percentages</title>
      <link>https://communities.sas.com/t5/Graphics-Programming/Stacked-bar-chart-with-percentages/m-p/917667#M24396</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19879"&gt;@Quentin&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;... I should calculate the percentages I want myself.&amp;nbsp; Even that feels overly complex, ...&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The percentages could also be calculated with PROC FREQ:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc freq data=have noprint;
by year;
tables state*fail / out=want1(where=(fail));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But then a format such as&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
picture pctfmt low-high='009%';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;would be needed to get the percent signs into the tick mark labels. Also, the label of variable PERCENT ("Percent of Total Frequency") becomes the new default y-axis label.&lt;/P&gt;</description>
      <pubDate>Fri, 23 Feb 2024 21:12:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Graphics-Programming/Stacked-bar-chart-with-percentages/m-p/917667#M24396</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2024-02-23T21:12:51Z</dc:date>
    </item>
    <item>
      <title>Re: Stacked bar chart with percentages</title>
      <link>https://communities.sas.com/t5/Graphics-Programming/Stacked-bar-chart-with-percentages/m-p/917670#M24397</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;.&amp;nbsp; I had mucked about with PROC TABULATE for a bit, but I'm trash at calculating percentages with PROC TABULATE.&amp;nbsp; I never even thought to try PROC FREQ with a BY statement.&amp;nbsp; Much better than my SQL step.&lt;/P&gt;</description>
      <pubDate>Fri, 23 Feb 2024 21:20:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Graphics-Programming/Stacked-bar-chart-with-percentages/m-p/917670#M24397</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2024-02-23T21:20:40Z</dc:date>
    </item>
    <item>
      <title>Re: Stacked bar chart with percentages</title>
      <link>https://communities.sas.com/t5/Graphics-Programming/Stacked-bar-chart-with-percentages/m-p/919356#M24430</link>
      <description>&lt;P&gt;You could also make some small modifications to your proc sql step and then use VBARPARM:&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;proc sql ;&lt;BR /&gt;create table want as&lt;BR /&gt;select a.year,a.state,sum(fail)/n as pct "% failed" FORMAT=PERCENT8.&lt;BR /&gt;from have as a&lt;BR /&gt;,(select year,n(fail) as n from have group by year) as b&lt;BR /&gt;where a.year=b.year&lt;BR /&gt;group by a.year,state&lt;BR /&gt;;&lt;BR /&gt;quit ;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;PROC SGPLOT DATA= WANT NOBORDER ;&lt;BR /&gt;styleattrs datacolors=(darkred darkblue) datacontrastcolors=(black) ;&lt;BR /&gt;vbarparm category= year response= pct / group= state seglabel seglabelattrs=(color=white weight=bold);&lt;BR /&gt;run ;&lt;/P&gt;&lt;P&gt;This gives you a stacked bar chart with each bar segment labelled with the percentage values from PROC SQL.&lt;/P&gt;&lt;P&gt;(Sorry- I'm new to the SAS community and not quite sure how to paste in the resulting graph.)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Mar 2024 19:36:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Graphics-Programming/Stacked-bar-chart-with-percentages/m-p/919356#M24430</guid>
      <dc:creator>jeanleid</dc:creator>
      <dc:date>2024-03-07T19:36:17Z</dc:date>
    </item>
  </channel>
</rss>

