<?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: Sorting and grouping across more columns with missing data in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Sorting-and-grouping-across-more-columns-with-missing-data/m-p/520048#M73385</link>
    <description>&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;, I see your point (sorry for not replying to you directly, I wrote it on the phone and there is no "reply to" option).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 11 Dec 2018 06:49:37 GMT</pubDate>
    <dc:creator>PavelD</dc:creator>
    <dc:date>2018-12-11T06:49:37Z</dc:date>
    <item>
      <title>Sorting and grouping across more columns with missing data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Sorting-and-grouping-across-more-columns-with-missing-data/m-p/519984#M73376</link>
      <description>&lt;P&gt;hi!&lt;/P&gt;&lt;P&gt;I am struggling with a seemingly simple problem.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Have:&lt;/STRONG&gt; see below&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Want: &lt;/STRONG&gt;grouped by entity, these groups then sorted by per. So the highest per at the top.&amp;nbsp;Per is missing when period=2.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I consulted &lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/Custom-Sorting-with-Proc-SQL/ta-p/373460" target="_self"&gt;"Custom Sorting with the Case Statement"&lt;/A&gt;&amp;nbsp;but didn't manage to connect the dots.&lt;/P&gt;&lt;P&gt;Any ideas? Thank you&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;have	entity	period	per	
	A	1	5	
	B	1	9	
	C	1	7	
	A	2	.	
	C	2	.	
	B	2	.	
				
want	entity	period	per	&amp;lt;&amp;lt; grouped by entity, sorted by per
	B	1	9	
	B	2	.	
	C	1	7	
	C	2	.	
	A	1	5	
	A	2	.	&lt;/PRE&gt;&lt;P&gt;&lt;STRONG&gt;edit 2018-12-10 16:26&lt;/STRONG&gt;: clarification (entity order in "want" is not important, but they need to be grouped)&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;edit 2018-12-11 08:13&lt;/STRONG&gt;: following &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;'s suggestion, below is my tentative (but working) implementation:&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;edit 2018-12-11 14:13&lt;/STRONG&gt;: you may shorten the code by using the SAS rank procedure instead of data step / proc sort combination.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input entity $ period per;
  datalines;
A 1 5 
B 1 9 
C 1 7 
A 2 . 
C 2 . 
B 2 .
;
run;
/* sorted have dataset
entity	period	per
B	1	9
C	1	7
A	1	5
A	2	.
C	2	.
B	2	.
*/
proc sort data = have; by descending per period; run;
/* have1 = have with a custom order added (period=1 only)
entity	period	per	custom_order
B	1	9	1
C	1	7	2
A	1	5	3
A	2	.	.
C	2	.	.
B	2	.	.
*/
data have1;
  set have;
  if period = 1 then custom_order + 1  ;
  else custom_order = .;
run;
/* have1, sorted 
entity	period	per	custom_order
A	1	5	3
A	2	.	.
B	1	9	1
B	2	.	.
C	1	7	2
C	2	.	.
*/
proc sort data = have1; by entity period; run;

/* want, unsorted yet 
entity	period	per	dummy
A	1	5	3
A	2	.	3
B	1	9	1
B	2	.	1
C	1	7	2
C	2	.	2
*/
data want (drop = custom_order);
  set have1;
  retain dummy 0;
  by entity period;
  
  if first.entity then dummy = custom_order;

run;
/* final, sorted want:
entity	period	per	dummy
B	1	9	1
B	2	.	1
C	1	7	2
C	2	.	2
A	1	5	3
A	2	.	3
*/
proc sort data = want; by dummy period ; run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Dec 2018 13:15:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Sorting-and-grouping-across-more-columns-with-missing-data/m-p/519984#M73376</guid>
      <dc:creator>PavelD</dc:creator>
      <dc:date>2018-12-11T13:15:17Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting and grouping across more columns with missing data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Sorting-and-grouping-across-more-columns-with-missing-data/m-p/519986#M73377</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have;
    by descending entity period;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 10 Dec 2018 15:24:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Sorting-and-grouping-across-more-columns-with-missing-data/m-p/519986#M73377</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-12-10T15:24:12Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting and grouping across more columns with missing data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Sorting-and-grouping-across-more-columns-with-missing-data/m-p/519990#M73378</link>
      <description>Hi PaigeMiller, sorry I have given maybe a wrong example. It is a coincidence that entity is nicely sorted in the "want" dataset. Let me edit.</description>
      <pubDate>Mon, 10 Dec 2018 15:25:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Sorting-and-grouping-across-more-columns-with-missing-data/m-p/519990#M73378</guid>
      <dc:creator>PavelD</dc:creator>
      <dc:date>2018-12-10T15:25:52Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting and grouping across more columns with missing data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Sorting-and-grouping-across-more-columns-with-missing-data/m-p/520026#M73381</link>
      <description>Since you want a custom order, map B to 1, C to 2 and A to 3 and then sort by that column. You can either keep it as a variable or drop if you don't need it.</description>
      <pubDate>Mon, 10 Dec 2018 16:09:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Sorting-and-grouping-across-more-columns-with-missing-data/m-p/520026#M73381</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-12-10T16:09:12Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting and grouping across more columns with missing data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Sorting-and-grouping-across-more-columns-with-missing-data/m-p/520034#M73382</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/215262"&gt;@PavelD&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;STRONG&gt;Want: &lt;/STRONG&gt;grouped by entity, these groups then sorted by per. So the highest per at the top.&amp;nbsp;Per is missing when period=2.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;edit 2018-12-10 16:26: clarification (entity order in "want" is not important, but they need to be grouped)&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have;
by entity descending per;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 10 Dec 2018 16:25:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Sorting-and-grouping-across-more-columns-with-missing-data/m-p/520034#M73382</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2018-12-10T16:25:26Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting and grouping across more columns with missing data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Sorting-and-grouping-across-more-columns-with-missing-data/m-p/520048#M73385</link>
      <description>&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;, I see your point (sorry for not replying to you directly, I wrote it on the phone and there is no "reply to" option).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Dec 2018 06:49:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Sorting-and-grouping-across-more-columns-with-missing-data/m-p/520048#M73385</guid>
      <dc:creator>PavelD</dc:creator>
      <dc:date>2018-12-11T06:49:37Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting and grouping across more columns with missing data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Sorting-and-grouping-across-more-columns-with-missing-data/m-p/520050#M73386</link>
      <description>You replied to your own message so not sure who you're replying to here or what you're trying to say.</description>
      <pubDate>Mon, 10 Dec 2018 16:58:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Sorting-and-grouping-across-more-columns-with-missing-data/m-p/520050#M73386</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-12-10T16:58:16Z</dc:date>
    </item>
  </channel>
</rss>

