<?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 Group sort question in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Group-sort-question/m-p/833801#M329642</link>
    <description>&lt;P&gt;Hi, everyone.&lt;/P&gt;&lt;P&gt;I have another sorting question. &amp;nbsp;I would appreciate it if you could help me.This is the code for a simplified example.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data have;&lt;BR /&gt;input ORD FV $ STATE $ SUM;&lt;BR /&gt;datalines;&lt;BR /&gt;1 apple all 21&lt;BR /&gt;2 apple a 7&lt;BR /&gt;3 apple b 14&lt;BR /&gt;1 carrot all 30&lt;BR /&gt;2 carrot a 13&lt;BR /&gt;3 carrot b 17&lt;BR /&gt;1 banana all 19&lt;BR /&gt;2 banana a 4&lt;BR /&gt;3 banana b 15&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;The FV variable shows those fruit or vegetables, STATE variable shows their states. There are only three states, 'a' or 'b'(maybe pieces or juice) or 'all' means both of them. SUM is the variable shows the quantity we have. And I already have the&amp;nbsp;ORD variable for inner sorting.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to sort them using SUM, but group by FV, which means for example, carrot's 'all' is the max SUM and should be the first obs and all of its states('a' 'b') should follow after it as a group settled down on 2 and 3 obs.&lt;/P&gt;&lt;P&gt;The code below is my solution, I need the new order variable SEQ, could it be more easier such as just in one step or do not need split dataset? Really thank you for your reading and thinking.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;proc sql noprint;
	create table all as select * from have where state='all' order by sum desc;
quit;
data all;
	set all;
	seq+1;output;
run;
proc sql noprint;
	create table want as
	select a.*,b.seq
	from have as a left join all as b
	on a.fv=b.fv
	;
quit;
proc sort data= want;
	by seq ord;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Snipaste_2022-09-16_18-02-56.png" style="width: 515px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/75307i73EE99C45B420AE8/image-dimensions/515x185?v=v2" width="515" height="185" role="button" title="Snipaste_2022-09-16_18-02-56.png" alt="Snipaste_2022-09-16_18-02-56.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 16 Sep 2022 10:18:01 GMT</pubDate>
    <dc:creator>Chauncy</dc:creator>
    <dc:date>2022-09-16T10:18:01Z</dc:date>
    <item>
      <title>Group sort question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Group-sort-question/m-p/833801#M329642</link>
      <description>&lt;P&gt;Hi, everyone.&lt;/P&gt;&lt;P&gt;I have another sorting question. &amp;nbsp;I would appreciate it if you could help me.This is the code for a simplified example.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data have;&lt;BR /&gt;input ORD FV $ STATE $ SUM;&lt;BR /&gt;datalines;&lt;BR /&gt;1 apple all 21&lt;BR /&gt;2 apple a 7&lt;BR /&gt;3 apple b 14&lt;BR /&gt;1 carrot all 30&lt;BR /&gt;2 carrot a 13&lt;BR /&gt;3 carrot b 17&lt;BR /&gt;1 banana all 19&lt;BR /&gt;2 banana a 4&lt;BR /&gt;3 banana b 15&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;The FV variable shows those fruit or vegetables, STATE variable shows their states. There are only three states, 'a' or 'b'(maybe pieces or juice) or 'all' means both of them. SUM is the variable shows the quantity we have. And I already have the&amp;nbsp;ORD variable for inner sorting.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to sort them using SUM, but group by FV, which means for example, carrot's 'all' is the max SUM and should be the first obs and all of its states('a' 'b') should follow after it as a group settled down on 2 and 3 obs.&lt;/P&gt;&lt;P&gt;The code below is my solution, I need the new order variable SEQ, could it be more easier such as just in one step or do not need split dataset? Really thank you for your reading and thinking.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;proc sql noprint;
	create table all as select * from have where state='all' order by sum desc;
quit;
data all;
	set all;
	seq+1;output;
run;
proc sql noprint;
	create table want as
	select a.*,b.seq
	from have as a left join all as b
	on a.fv=b.fv
	;
quit;
proc sort data= want;
	by seq ord;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Snipaste_2022-09-16_18-02-56.png" style="width: 515px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/75307i73EE99C45B420AE8/image-dimensions/515x185?v=v2" width="515" height="185" role="button" title="Snipaste_2022-09-16_18-02-56.png" alt="Snipaste_2022-09-16_18-02-56.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Sep 2022 10:18:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Group-sort-question/m-p/833801#M329642</guid>
      <dc:creator>Chauncy</dc:creator>
      <dc:date>2022-09-16T10:18:01Z</dc:date>
    </item>
    <item>
      <title>Re: Group sort question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Group-sort-question/m-p/833805#M329645</link>
      <description>&lt;P&gt;In your output data set, why is carrot the first FV group followed by apple followed by banana? This is not in any sorted order. Please explain this part.&lt;/P&gt;</description>
      <pubDate>Fri, 16 Sep 2022 10:35:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Group-sort-question/m-p/833805#M329645</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-09-16T10:35:17Z</dc:date>
    </item>
    <item>
      <title>Re: Group sort question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Group-sort-question/m-p/833809#M329648</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/432792"&gt;@Chauncy&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks to the power of the ORDER BY clause of PROC SQL you can almost always solve your "sort questions" in one step.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select a.*
from have a join have(where=(state='all')) b
on a.fv=b.fv
order by b.sum desc, fv, ord;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You could even get the desired result if variable ORD wasn't there: You would just replace &lt;FONT face="courier new,courier"&gt;ord&lt;/FONT&gt; by, e.g.,&amp;nbsp;&lt;FONT face="courier new,courier"&gt;whichc(state,'all','a','b')&lt;/FONT&gt;.&lt;/P&gt;</description>
      <pubDate>Fri, 16 Sep 2022 11:37:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Group-sort-question/m-p/833809#M329648</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-09-16T11:37:24Z</dc:date>
    </item>
    <item>
      <title>Re: Group sort question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Group-sort-question/m-p/833893#M329693</link>
      <description>&lt;P&gt;Maybe show some data before you got the sum.&lt;/P&gt;
&lt;P&gt;Proc summary/means with Class variables will create statistics like sum for combinations of the class variables and include a _type_ variable so that the _type_ for the sum of Carrot "all" would be lower than the _type_ of Carrot and State combinations. So use Summary and then sort by FV State _type_.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or if all of this manipulation is to force records into the order needed for a report you may be doing a lot of extra work as I bet I can make a report that displays in that order (though may use Proc Tabulate depending on actual data and needs).&lt;/P&gt;
&lt;P&gt;Beware of oversimplifying examples. An approach that may work for 2 variables may have issues if you are doing multiple "all" levels with more groups.&lt;/P&gt;</description>
      <pubDate>Fri, 16 Sep 2022 16:32:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Group-sort-question/m-p/833893#M329693</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-09-16T16:32:47Z</dc:date>
    </item>
    <item>
      <title>Re: Group sort question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Group-sort-question/m-p/833942#M329705</link>
      <description>I need sort fv through sum which state='all' descending, but all states one fv has must followed after it as a group. In fact, 'all' is the summary row of sum for each fv(state='a' or 'b'). Sorry, it maybe not much clear.</description>
      <pubDate>Sat, 17 Sep 2022 03:26:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Group-sort-question/m-p/833942#M329705</guid>
      <dc:creator>Chauncy</dc:creator>
      <dc:date>2022-09-17T03:26:44Z</dc:date>
    </item>
    <item>
      <title>Re: Group sort question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Group-sort-question/m-p/833943#M329706</link>
      <description>Never used ORDER BY clause while joining tables, there's more to learn. Thanks!</description>
      <pubDate>Sat, 17 Sep 2022 03:37:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Group-sort-question/m-p/833943#M329706</guid>
      <dc:creator>Chauncy</dc:creator>
      <dc:date>2022-09-17T03:37:48Z</dc:date>
    </item>
    <item>
      <title>Re: Group sort question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Group-sort-question/m-p/833945#M329707</link>
      <description>Thank you. I will pay attention for that. This is a simplified model for generating some rtf files during drug clinical trial data submission, such as Summary of Treatment-Emergent Adverse Events by System Organ Class, Preferred Term and Severity, while I need to generate sas datasets with standard specification for easy review. So before using proc report, I also need the datasets already displayed in a certain order with the summary row in it as one observation. For that reason, we can not create any new variables or new statistics during report procedure, and all sorting variables should be prepared before. However, there's more for me to learn.Thanks again.</description>
      <pubDate>Sat, 17 Sep 2022 04:21:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Group-sort-question/m-p/833945#M329707</guid>
      <dc:creator>Chauncy</dc:creator>
      <dc:date>2022-09-17T04:21:54Z</dc:date>
    </item>
  </channel>
</rss>

