<?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: PROC SGPLOT ordering by category and group in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/PROC-SGPLOT-ordering-by-category-and-group/m-p/898682#M39992</link>
    <description>&lt;P&gt;With your very tiny example data set (which I have expanded), I cannot reproduce this problem.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input subject gender $ state $;
datalines;
1 M TX
2 F TX
3 M TX
4 F VA
5 F VA
1 M TX
2 F TX
3 M TX
4 F VA
5 F VA
1 M OR
2 F TX
3 M OR
4 M OR
5 M OR
1 M NY
2 F NY
3 M NY
4 F NJ
5 F NJ
;
 
proc freq data=have;
tables gender*state / out=counts outpct;
run;

proc sort data=counts ;
by gender  ;
run;
 
proc sgplot data=counts;
vbar state  / response=count group=gender groupdisplay=stack ;
xaxis display = none;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sun, 15 Oct 2023 18:48:29 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2023-10-15T18:48:29Z</dc:date>
    <item>
      <title>PROC SGPLOT ordering by category and group</title>
      <link>https://communities.sas.com/t5/New-SAS-User/PROC-SGPLOT-ordering-by-category-and-group/m-p/898677#M39989</link>
      <description>&lt;P&gt;I have a simple dataset structured as follows:&lt;/P&gt;&lt;P&gt;data have;&lt;/P&gt;&lt;P&gt;input subject gender $ state $;&lt;/P&gt;&lt;P&gt;datalines&lt;/P&gt;&lt;P&gt;1 'M' 'TX'&lt;/P&gt;&lt;P&gt;2 'F' 'TX'&lt;/P&gt;&lt;P&gt;3 'M' 'TX'&lt;/P&gt;&lt;P&gt;4 'F' 'VA'&lt;/P&gt;&lt;P&gt;5 'F' 'VA'&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have generated some summary data using PROC FREQ and wish to plot it.&lt;/P&gt;&lt;DIV&gt;proc freq data=have;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;tables gender*state / out=counts outpct;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;run;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;proc sort data=counts ;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;by gender&amp;nbsp; ;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;run;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;proc sgplot data=counts;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;vbar state&amp;nbsp; / response=count group=gender order=ascending categoryorder=respasc groupdisplay=stack ;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;xaxis display = none;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;run;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;This generates the following plot;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.JPG" style="width: 632px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/88874i36EFDDEECF90CD50/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.JPG" alt="Capture.JPG" /&gt;&lt;/span&gt;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;I am happy with all parts of this plot&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;EXCEPT the displayed order of the groups in the bars (gender), which I want to be uniform (e.g. red always under blue). &lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;Changing the sorting variable in proc sort and grouporder in proc sgplot do not seem to change the these displays.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;What else should I try?&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;Thank you.&lt;/SPAN&gt;&lt;/DIV&gt;</description>
      <pubDate>Sun, 15 Oct 2023 18:29:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/PROC-SGPLOT-ordering-by-category-and-group/m-p/898677#M39989</guid>
      <dc:creator>EmilyAV</dc:creator>
      <dc:date>2023-10-15T18:29:22Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SGPLOT ordering by category and group</title>
      <link>https://communities.sas.com/t5/New-SAS-User/PROC-SGPLOT-ordering-by-category-and-group/m-p/898678#M39990</link>
      <description>&lt;P&gt;Try the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.4/grstatproc/n0p7vdd69sgf3wn1479qxqxuryrt.htm#n0iz9rcg3x496dn1sfn1g6ernd9ol" target="_self"&gt;GROUPORDER= option&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 15 Oct 2023 18:34:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/PROC-SGPLOT-ordering-by-category-and-group/m-p/898678#M39990</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-10-15T18:34:51Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SGPLOT ordering by category and group</title>
      <link>https://communities.sas.com/t5/New-SAS-User/PROC-SGPLOT-ordering-by-category-and-group/m-p/898680#M39991</link>
      <description>&lt;P&gt;Thanks, &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;.&lt;/P&gt;&lt;P&gt;Adding the GROUPORDER option doesn't generate an error message but also doesn't change the display.&lt;/P&gt;</description>
      <pubDate>Sun, 15 Oct 2023 18:39:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/PROC-SGPLOT-ordering-by-category-and-group/m-p/898680#M39991</guid>
      <dc:creator>EmilyAV</dc:creator>
      <dc:date>2023-10-15T18:39:29Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SGPLOT ordering by category and group</title>
      <link>https://communities.sas.com/t5/New-SAS-User/PROC-SGPLOT-ordering-by-category-and-group/m-p/898682#M39992</link>
      <description>&lt;P&gt;With your very tiny example data set (which I have expanded), I cannot reproduce this problem.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input subject gender $ state $;
datalines;
1 M TX
2 F TX
3 M TX
4 F VA
5 F VA
1 M TX
2 F TX
3 M TX
4 F VA
5 F VA
1 M OR
2 F TX
3 M OR
4 M OR
5 M OR
1 M NY
2 F NY
3 M NY
4 F NJ
5 F NJ
;
 
proc freq data=have;
tables gender*state / out=counts outpct;
run;

proc sort data=counts ;
by gender  ;
run;
 
proc sgplot data=counts;
vbar state  / response=count group=gender groupdisplay=stack ;
xaxis display = none;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 15 Oct 2023 18:48:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/PROC-SGPLOT-ordering-by-category-and-group/m-p/898682#M39992</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-10-15T18:48:29Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SGPLOT ordering by category and group</title>
      <link>https://communities.sas.com/t5/New-SAS-User/PROC-SGPLOT-ordering-by-category-and-group/m-p/898684#M39993</link>
      <description>&lt;P&gt;Even when I produce a much larger fake data set, I cannot reproduce this problem.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data fake;
    do state='AL','AK','AZ','AR','CA','CO','CT''DE','FL','GA','HI','ID','IL','IN','IA','KS',
    	'KY','LA','ME','MA','MD','MI','MN';
        n=rand('integer',10,100);
        do repl=1 to n;
            if rand('uniform')&amp;lt;0.2 then gender='F'; else gender='M';
            output;
        end;
    end;
    drop n repl;
run;
 
proc freq data=fake;
tables gender*state / out=counts outpct;
run;

proc sort data=counts ;
by gender  ;
run;
 
proc sgplot data=counts;
vbar state  / response=count group=gender groupdisplay=stack ;
xaxis display = none;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 15 Oct 2023 19:06:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/PROC-SGPLOT-ordering-by-category-and-group/m-p/898684#M39993</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-10-15T19:06:46Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SGPLOT ordering by category and group</title>
      <link>https://communities.sas.com/t5/New-SAS-User/PROC-SGPLOT-ordering-by-category-and-group/m-p/898694#M39995</link>
      <description>&lt;P&gt;That's strange. Must have to do with the structure of the data?&lt;/P&gt;&lt;P&gt;I eliminated the possibility that the problem was a lack of zero counts in some groups, but still run into the same issue.&lt;/P&gt;&lt;P&gt;Not sure if this makes sense, but when I remove the&amp;nbsp;categoryorder=respasc statement, the plot changes like this:&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="Capture2.JPG" style="width: 626px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/88878iDD54044CC6121FD6/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture2.JPG" alt="Capture2.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 15 Oct 2023 21:47:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/PROC-SGPLOT-ordering-by-category-and-group/m-p/898694#M39995</guid>
      <dc:creator>EmilyAV</dc:creator>
      <dc:date>2023-10-15T21:47:50Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SGPLOT ordering by category and group</title>
      <link>https://communities.sas.com/t5/New-SAS-User/PROC-SGPLOT-ordering-by-category-and-group/m-p/898733#M40003</link>
      <description>&lt;P&gt;&lt;A href="https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/grstatproc/n0p7vdd69sgf3wn1479qxqxuryrt.htm#p14mwpmr7ycybcn10rkqzq0pn8iwc" target="_self"&gt;The documentation for the CATEGORYORDER=RESPASC option&lt;/A&gt;&amp;nbsp;states&lt;/P&gt;
&lt;TABLE class="xisDoc-summary"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="xisDoc-summaryText"&gt;When a group variable is used with the CATEGORYORDER= option, the category order is not affected by the value of the groups. &lt;FONT color="#800000"&gt;The categories are always sorted by the response statistic at a category level&lt;/FONT&gt;. [Emphasis added]&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="xisDoc-summaryText"&gt;When this option and the GROUPORDER= option are both specified, the GROUPORDER= option is ignored.&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think you will need to sort the data by total count, then by gender. Then plot the data and use GROUPORDER=DATA to order the states.&lt;/P&gt;</description>
      <pubDate>Mon, 16 Oct 2023 11:03:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/PROC-SGPLOT-ordering-by-category-and-group/m-p/898733#M40003</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2023-10-16T11:03:09Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SGPLOT ordering by category and group</title>
      <link>https://communities.sas.com/t5/New-SAS-User/PROC-SGPLOT-ordering-by-category-and-group/m-p/898740#M40006</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*
order your freq in dataset before PROC SGPLOT 
and switch it into VBARPARM .
*/

data fake;
call streaminit(123);
    do state='AL','AK','AZ','AR','CA','CO','CT','DE','FL','GA','HI','ID','IL','IN','IA','KS',
    	'KY','LA','ME','MA','MD','MI','MN';
        n=rand('integer',10,100);
        do repl=1 to n;
            if rand('uniform')&amp;lt;0.2 then gender='F'; else gender='M';

				if state in ('ME' 'CO' 'DE') then gender=ifc(gender='F','M','F');

            output;
        end;
    end;
    drop n repl;

run;
 

proc sql;
create table counts as
select * ,sum(count) as total from
(select state,gender,count(*) as count from fake group by state,gender)
group by  state
order by total 
;
quit;
 
proc sgplot data=counts;
vbarparm category=state  response=count/ group=gender  groupdisplay=stack grouporder=ASCENDING ;
/*xaxis display = none;*/
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-1697455819839.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/88885iFC6911F69D03E648/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ksharp_0-1697455819839.png" alt="Ksharp_0-1697455819839.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>Mon, 16 Oct 2023 11:30:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/PROC-SGPLOT-ordering-by-category-and-group/m-p/898740#M40006</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-10-16T11:30:22Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SGPLOT ordering by category and group</title>
      <link>https://communities.sas.com/t5/New-SAS-User/PROC-SGPLOT-ordering-by-category-and-group/m-p/898787#M40014</link>
      <description>&lt;P&gt;Thank you.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 16 Oct 2023 13:37:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/PROC-SGPLOT-ordering-by-category-and-group/m-p/898787#M40014</guid>
      <dc:creator>EmilyAV</dc:creator>
      <dc:date>2023-10-16T13:37:38Z</dc:date>
    </item>
  </channel>
</rss>

