<?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: Order column in data set by value in last row in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Order-column-in-data-set-by-value-in-last-row/m-p/615865#M180190</link>
    <description>&lt;P&gt;Hi Ronein,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Perhaps there is a more sophisticated solution but what you could do is:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;- transpose the values of 'Total' row (only)&lt;/P&gt;&lt;P&gt;- sort this by the values of total (from COL1 by default)&lt;/P&gt;&lt;P&gt;- transpose back what you get from previous step&lt;/P&gt;&lt;P&gt;- concatenate the values from previous step to one variable (e.g with CATX(' ', of MODEL: ) )&lt;/P&gt;&lt;P&gt;- assign the new concatenated value to a macro var (e.g. with CALL SYMPUT)&lt;/P&gt;&lt;P&gt;- use RETAIN statement to define the order of the variables based on the macro var from previous strep (Note: the RETAIN should be before the SET statement)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It should give you what you want.&lt;/P&gt;</description>
    <pubDate>Wed, 08 Jan 2020 08:12:59 GMT</pubDate>
    <dc:creator>geoskiad</dc:creator>
    <dc:date>2020-01-08T08:12:59Z</dc:date>
    <item>
      <title>Order column in data set by value in last row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Order-column-in-data-set-by-value-in-last-row/m-p/615858#M180184</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;I have a summary table .&lt;/P&gt;
&lt;P&gt;The task is to order the columns by the value of the total Row.&lt;/P&gt;
&lt;P&gt;The expected order of columns will be :&lt;/P&gt;
&lt;P&gt;Team&lt;/P&gt;
&lt;P&gt;Model5 (because Total is the lowest)&lt;/P&gt;
&lt;P&gt;Model1&lt;/P&gt;
&lt;P&gt;Model3&lt;/P&gt;
&lt;P&gt;Model2&lt;/P&gt;
&lt;P&gt;Model4 (because Total is the highest)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What is the way to do it please?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data Summary_tbl;
INFILE DATALINES DLM=',';
INPUT Team $  Model1  Model2  Model3  Model4  Model5;
cards;
b,10,18,14,16,4
d,8,24,16,20,6
e,6,18,12,15,8
c,1,7,3,14,2
a,2,1,10,14,1 
Total,27,68,55,79,21
;
Run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Jan 2020 06:42:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Order-column-in-data-set-by-value-in-last-row/m-p/615858#M180184</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2020-01-08T06:42:47Z</dc:date>
    </item>
    <item>
      <title>Re: Order column in data set by value in last row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Order-column-in-data-set-by-value-in-last-row/m-p/615865#M180190</link>
      <description>&lt;P&gt;Hi Ronein,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Perhaps there is a more sophisticated solution but what you could do is:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;- transpose the values of 'Total' row (only)&lt;/P&gt;&lt;P&gt;- sort this by the values of total (from COL1 by default)&lt;/P&gt;&lt;P&gt;- transpose back what you get from previous step&lt;/P&gt;&lt;P&gt;- concatenate the values from previous step to one variable (e.g with CATX(' ', of MODEL: ) )&lt;/P&gt;&lt;P&gt;- assign the new concatenated value to a macro var (e.g. with CALL SYMPUT)&lt;/P&gt;&lt;P&gt;- use RETAIN statement to define the order of the variables based on the macro var from previous strep (Note: the RETAIN should be before the SET statement)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It should give you what you want.&lt;/P&gt;</description>
      <pubDate>Wed, 08 Jan 2020 08:12:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Order-column-in-data-set-by-value-in-last-row/m-p/615865#M180190</guid>
      <dc:creator>geoskiad</dc:creator>
      <dc:date>2020-01-08T08:12:59Z</dc:date>
    </item>
    <item>
      <title>Re: Order column in data set by value in last row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Order-column-in-data-set-by-value-in-last-row/m-p/615869#M180192</link>
      <description>&lt;P&gt;Inspired by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/304272"&gt;@geoskiad&lt;/a&gt; :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=work.Summary_tbl(where=(Team='Total')) out=work.totals(rename=(_name_=model col1=value));
run;

/* if inside of a macro:
%local VarList;
*/

proc sql noprint;
   select model
      into :VarList separated by ' '
      from work.Totals
      order by value;
quit;

%put &amp;amp;=Varlist;


proc print data=work.summary_tbl;
   var &amp;amp;VarList.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 08 Jan 2020 08:48:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Order-column-in-data-set-by-value-in-last-row/m-p/615869#M180192</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2020-01-08T08:48:22Z</dc:date>
    </item>
    <item>
      <title>Re: Order column in data set by value in last row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Order-column-in-data-set-by-value-in-last-row/m-p/615917#M180216</link>
      <description>&lt;P&gt;My share of fun &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data Summary_tbl;
INFILE DATALINES DLM=',';
INPUT Team $  Model1  Model2  Model3  Model4  Model5;
cards;
b,10,18,14,16,4
d,8,24,16,20,6
e,6,18,12,15,8
c,1,7,3,14,2
a,2,1,10,14,1 
Total,27,68,55,79,21
;
Run;
 
data _null_ ;
 if _n_=1 then do;
  set Summary_tbl(where=(Team='Total'));
  array m(*) model:;
  array t(5);
  call pokelong(peekclong(addrlong(m(1)),40),addrlong(t1),40);
  call sortn(of t(*));
  dcl hash H () ;
  h.definekey("_n_");
  do _n_=1 to dim(t);
   _iorc_=whichn(t(_n_),of m(*));
    h.definedata (vname(m(_iorc_))) ;
  end;
  h.definedone();&lt;BR /&gt;  _n_=1;
 end;
 set Summary_tbl end=z;
   h.replace();
 if z;
 h.output(dataset:'want');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Jan 2020 12:50:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Order-column-in-data-set-by-value-in-last-row/m-p/615917#M180216</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-01-09T12:50:52Z</dc:date>
    </item>
    <item>
      <title>Re: Order column in data set by value in last row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Order-column-in-data-set-by-value-in-last-row/m-p/615918#M180217</link>
      <description>&lt;P&gt;I knew my code was not sophisticated &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data = summary_tbl out = x;
  where team = 'Total';
run;

proc sort data = x;
  by col1;
run;

proc transpose data = x out = x_t;
  var _name_;
run;

data _null_;
set x_t;
  call symput('xx', catx(' ', of Model: )); 
run;

data Summary_tbl2;
 retain Team &amp;amp;xx.;
set Summary_tbl;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 08 Jan 2020 13:28:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Order-column-in-data-set-by-value-in-last-row/m-p/615918#M180217</guid>
      <dc:creator>geoskiad</dc:creator>
      <dc:date>2020-01-08T13:28:05Z</dc:date>
    </item>
    <item>
      <title>Re: Order column in data set by value in last row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Order-column-in-data-set-by-value-in-last-row/m-p/615919#M180218</link>
      <description>&lt;P&gt;I don't claim mine is either. SAS is a great video game is all I know. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Jan 2020 13:30:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Order-column-in-data-set-by-value-in-last-row/m-p/615919#M180218</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-01-08T13:30:02Z</dc:date>
    </item>
    <item>
      <title>Re: Order column in data set by value in last row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Order-column-in-data-set-by-value-in-last-row/m-p/615985#M180254</link>
      <description>&lt;P&gt;This is a problem where the ability to dynamically build hash tables (in particular dynamically add data components to the hash definedata method helps.&amp;nbsp; The program below (1) reads in the total row, (2) works from the min to the max of the total row, adding data components to the hash definition in corresponding order, (3) then reads in all the data set, adding each obs to the hash object, and (4) at the end of input ouput the hash object to data set WANT:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data Summary_tbl;
  INFILE DATALINES DLM=',';
  INPUT Team $  Model1  Model2  Model3  Model4  Model5;
cards;
b,10,18,14,16,4
d,8,24,16,20,6
e,6,18,12,15,8
c,1,7,3,14,2
a,2,1,10,14,1 
Total,27,68,55,79,21
;
Run;

data _null_;
  set summary_tbl (where=(team='Total'))  summary_tbl end=end_of_data;
  array _tot {*} model: ;
  if _n_=1 then do;
    declare hash h (ordered:'a');
    h.definekey('seq');
    h.definedata('team');
    do until (n(of _tot{*})=0 );
      i=whichn(min(of _tot{*}),of _tot{*});
      h.definedata(vname(_tot{i}));
      call missing(_tot{i});
    end;
    h.definedone();
  end;
  seq=_n_;
  if _n_&amp;gt;1 then h.add();
  if end_of_data;
  h.output (dataset:'want');
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;OL&gt;
&lt;LI&gt;In the SET statement, the total row is read, followed by all the rows (including the total row at the end of the data set).&lt;/LI&gt;
&lt;LI&gt;You see the h.definedata method statement embedded in a loop over changing minimum totals as per my strategy above.&lt;/LI&gt;
&lt;LI&gt;To preserve the original &lt;EM&gt;&lt;STRONG&gt;row&lt;/STRONG&gt;&lt;/EM&gt; order, there is a variable SEQ used as the h object key, and the hash object is declared as sorted.&lt;/LI&gt;
&lt;LI&gt;The "if _n_&amp;gt;1 then h.add()" avoids adding the total row at the beginning of the hash object.&lt;/LI&gt;
&lt;/OL&gt;</description>
      <pubDate>Wed, 08 Jan 2020 16:58:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Order-column-in-data-set-by-value-in-last-row/m-p/615985#M180254</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-01-08T16:58:07Z</dc:date>
    </item>
    <item>
      <title>Re: Order column in data set by value in last row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Order-column-in-data-set-by-value-in-last-row/m-p/616169#M180349</link>
      <description>&lt;P&gt;Thank you.&lt;/P&gt;
&lt;P&gt;Total row is not in your output and I want it too...&lt;/P&gt;</description>
      <pubDate>Thu, 09 Jan 2020 12:25:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Order-column-in-data-set-by-value-in-last-row/m-p/616169#M180349</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2020-01-09T12:25:28Z</dc:date>
    </item>
    <item>
      <title>Re: Order column in data set by value in last row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Order-column-in-data-set-by-value-in-last-row/m-p/616171#M180351</link>
      <description>&lt;P&gt;HI&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp; Good catch and thank you pointing me lack of attention to detail as to have missed to include&amp;nbsp;&lt;EM&gt; h.definedata("Team");&amp;nbsp;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;So, I have added that below in the modified one &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; Thanks again&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data Summary_tbl;
INFILE DATALINES DLM=',';
INPUT Team $  Model1  Model2  Model3  Model4  Model5;
cards;
b,10,18,14,16,4
d,8,24,16,20,6
e,6,18,12,15,8
c,1,7,3,14,2
a,2,1,10,14,1 
Total,27,68,55,79,21
;
Run;

data _null_ ;
 if _n_=1 then do;
  set Summary_tbl(where=(Team='Total'));
  array m(*) model:;
  array t(5);
  call pokelong(peekclong(addrlong(m(1)),40),addrlong(t1),40);
  call sortn(of t(*));
  dcl hash H (ordered:'a') ;
  h.definekey("_n_");
  h.definedata("Team");
  do _n_=1 to dim(t);
   _iorc_=whichn(t(_n_),of m(*));
    h.definedata (vname(m(_iorc_))) ;
  end;
  h.definedone();
  _n_=1;
 end;
 set Summary_tbl end=z;
  h.replace();
 if z;
 h.output(dataset:'want');
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 09 Jan 2020 12:49:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Order-column-in-data-set-by-value-in-last-row/m-p/616171#M180351</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-01-09T12:49:14Z</dc:date>
    </item>
  </channel>
</rss>

