<?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: Sort rows in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Sort-rows/m-p/612938#M178944</link>
    <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;In the real report that I need to produce the order of the rows is important.&lt;/P&gt;
&lt;P&gt;Lt's say that the "important groups" should be in the first rows of the report.&lt;/P&gt;
&lt;P&gt;I have tried to create another data set that showing the order of rows that I want&amp;nbsp; but unfortunately the required data set still has wrong order of rows.&lt;/P&gt;
&lt;P&gt;I want to ask also why in the left join the sort of the rows is not in same order as the table on the left?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data  summary_tbl;
input group $ mean_Y  SUM_Y ;
cards;
847ABC 10.1 1200 
958THF 19.1 1580
833RET  17.2 1620
860QWE 14.8 1490
855DBC 15.1 1810
;
run;


Data ssort;
input group $;
cards;
833RET
847ABC
958THF
855DBC
860QWE
;
run;

PROC SQL;
	create table required  as
	select a.*,b.mean_Y,b.SUM_Y 	   
	from  ssort as a
	left join summary_tbl as b
	on a.group=b.group
;
QUIT;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 19 Dec 2019 07:04:11 GMT</pubDate>
    <dc:creator>Ronein</dc:creator>
    <dc:date>2019-12-19T07:04:11Z</dc:date>
    <item>
      <title>Sort rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sort-rows/m-p/612928#M178937</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;Please find summary table.&lt;/P&gt;
&lt;P&gt;I need to do 2 things and I would like to ask please how to do them:&lt;/P&gt;
&lt;P&gt;1-I want to sort the data in following order:&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;833RET&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;847ABC&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;958THF&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;855DBC&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;860QWE&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2-I want to add summary row for following groups:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;833RET+847ABC&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;958THF+&lt;/CODE&gt;&lt;CODE class=" language-sas"&gt;855DBC+&lt;/CODE&gt;&lt;CODE class=" language-sas"&gt;860QWE&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;All groups together&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;(Note: In summary row there will be only value of SUM_Y and mean_Y value will be missing) &lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;So there will be&amp;nbsp; 8 rows in the required data set (5 original rows plus 3 of sum&amp;nbsp; )&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;row1-&lt;CODE class=" language-sas"&gt;833RET&lt;/CODE&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Row2-&lt;CODE class=" language-sas"&gt;847ABC&lt;/CODE&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;CODE class=" language-sas"&gt;Row3-833RET+847ABC&lt;/CODE&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Row4-&lt;CODE class=" language-sas"&gt;958THF&lt;/CODE&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Row5-&lt;CODE class=" language-sas"&gt;855DBC&lt;/CODE&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;Row6-958THF+855DBC&amp;nbsp;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;Row7-860QWE&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;Row8-Grand Total for all groups&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data  summary_tb;
input group $ mean_Y  SUM_Y ;
cards;
847ABC 10.1 1200 
958THF 19.1 1580
833RET  17.2 1620
860QWE 14.8 1490
855DBC 15.1 1810
;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Dec 2019 06:15:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sort-rows/m-p/612928#M178937</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2019-12-19T06:15:14Z</dc:date>
    </item>
    <item>
      <title>Re: Sort rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sort-rows/m-p/612932#M178940</link>
      <description>&lt;P&gt;What is the logical rule for sorting, and what is the logical rule for building the groups? Without such, no code is possible.&lt;/P&gt;</description>
      <pubDate>Thu, 19 Dec 2019 06:16:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sort-rows/m-p/612932#M178940</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-12-19T06:16:33Z</dc:date>
    </item>
    <item>
      <title>Re: Sort rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sort-rows/m-p/612938#M178944</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;In the real report that I need to produce the order of the rows is important.&lt;/P&gt;
&lt;P&gt;Lt's say that the "important groups" should be in the first rows of the report.&lt;/P&gt;
&lt;P&gt;I have tried to create another data set that showing the order of rows that I want&amp;nbsp; but unfortunately the required data set still has wrong order of rows.&lt;/P&gt;
&lt;P&gt;I want to ask also why in the left join the sort of the rows is not in same order as the table on the left?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data  summary_tbl;
input group $ mean_Y  SUM_Y ;
cards;
847ABC 10.1 1200 
958THF 19.1 1580
833RET  17.2 1620
860QWE 14.8 1490
855DBC 15.1 1810
;
run;


Data ssort;
input group $;
cards;
833RET
847ABC
958THF
855DBC
860QWE
;
run;

PROC SQL;
	create table required  as
	select a.*,b.mean_Y,b.SUM_Y 	   
	from  ssort as a
	left join summary_tbl as b
	on a.group=b.group
;
QUIT;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Dec 2019 07:04:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sort-rows/m-p/612938#M178944</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2019-12-19T07:04:11Z</dc:date>
    </item>
    <item>
      <title>Re: Sort rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sort-rows/m-p/612943#M178946</link>
      <description>&lt;P&gt;The SQL join does an implicit sort on the "key" column(s) used in the "on" clause.&lt;/P&gt;
&lt;P&gt;If you need to preserve the original order, you need to store that in an additional column:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data  summary_tbl;
input group $ mean_Y  SUM_Y ;
cards;
847ABC 10.1 1200 
958THF 19.1 1580
833RET  17.2 1620
860QWE 14.8 1490
855DBC 15.1 1810
;
run;

data ssort;
input group $;
sortkey = _n_;
cards;
833RET
847ABC
958THF
855DBC
860QWE
;
run;

proc sql;
create table required  as
select a.group,b.mean_Y,b.SUM_Y 	   
from  ssort as a
left join summary_tbl as b
on a.group=b.group
order by a.sortkey
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 19 Dec 2019 08:02:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sort-rows/m-p/612943#M178946</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-12-19T08:02:54Z</dc:date>
    </item>
    <item>
      <title>Re: Sort rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sort-rows/m-p/612945#M178948</link>
      <description>&lt;P&gt;Fixing the sorting-problem:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data SortFormat;
   set ssort(rename=(group=Start));
   length FmtName $ 32 Label $ 3 _number 8;
   retain FmtName "$RequiredOrder" _number 0;

   _number = _number + 1;
   Label = put(_number, z3.);

   drop _number;
run;

proc format cntlin=SortFormat;
run;

proc sort data=summary_tbl out=sorted;
   by group;
   format group $RequiredOrder.;
run;

proc datasets nolist;
   modify sorted;
      format group;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 19 Dec 2019 08:12:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sort-rows/m-p/612945#M178948</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2019-12-19T08:12:41Z</dc:date>
    </item>
  </channel>
</rss>

