<?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: How to  sort a data set OBS by  different sorting order in same step in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-sort-a-data-set-OBS-by-different-sorting-order-in-same/m-p/370182#M88418</link>
    <description>&lt;P&gt;Here is one way:&lt;/P&gt;
&lt;PRE&gt;data need;
  set one;
  sortorder=total;
  if Total eq 0 then sortorder+(-.1*sortorder+.01*placebo);
run;

proc sort data=need out=want;
  by descending sortorder id;
run;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 23 Jun 2017 22:14:59 GMT</pubDate>
    <dc:creator>art297</dc:creator>
    <dc:date>2017-06-23T22:14:59Z</dc:date>
    <item>
      <title>How to  sort a data set OBS by  different sorting order in same step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-sort-a-data-set-OBS-by-different-sorting-order-in-same/m-p/370176#M88417</link>
      <description>&lt;P&gt;Dear,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I need to sort the data by different sorting order based on &amp;nbsp;Total value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if Total is not equal to 0 then sort order should be "descending total id".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if total is equal =0 then sort order should be "descending total descending placebo id"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The &amp;nbsp;OBS (id=d and id=e) has total values equal=0. As per alphabetical order the id=d should come first. But I need id=e should come before id=d as its placebo value is greater than id=d.&lt;/P&gt;
&lt;P&gt;Please help. Thank you&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Output needed;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;b 4 8 12 3&lt;BR /&gt;i 6 6 12 1&lt;BR /&gt;c 7 4 11 0&lt;BR /&gt;j 7 3 10 0&lt;BR /&gt;a 2 4 6 1&lt;BR /&gt;f 1 0 1 2&lt;BR /&gt;g 1 0 1 3&lt;BR /&gt;e 0 0 0 3&lt;BR /&gt;d 0 0 0 2&lt;/P&gt;
&lt;PRE&gt;data one;
input id$ _10mg _20mg Total placebo;
datalines;
a 2 4 6 1
c 7 4 11 0
b 4 8 12 3
d 0 0 0 2
e 0 0 0 3
f 1 0 1 2
g 1 0 1 3&lt;BR /&gt;i 6 6 12 1&lt;BR /&gt;j 7 3 10 0
;&lt;/PRE&gt;</description>
      <pubDate>Fri, 23 Jun 2017 21:41:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-sort-a-data-set-OBS-by-different-sorting-order-in-same/m-p/370176#M88417</guid>
      <dc:creator>knveraraju91</dc:creator>
      <dc:date>2017-06-23T21:41:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to  sort a data set OBS by  different sorting order in same step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-sort-a-data-set-OBS-by-different-sorting-order-in-same/m-p/370182#M88418</link>
      <description>&lt;P&gt;Here is one way:&lt;/P&gt;
&lt;PRE&gt;data need;
  set one;
  sortorder=total;
  if Total eq 0 then sortorder+(-.1*sortorder+.01*placebo);
run;

proc sort data=need out=want;
  by descending sortorder id;
run;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Jun 2017 22:14:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-sort-a-data-set-OBS-by-different-sorting-order-in-same/m-p/370182#M88418</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-06-23T22:14:59Z</dc:date>
    </item>
    <item>
      <title>Re: How to  sort a data set OBS by  different sorting order in same step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-sort-a-data-set-OBS-by-different-sorting-order-in-same/m-p/370222#M88425</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/68272"&gt;@knveraraju91&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;More out of curiosity I've tried the following SQL which to my surprise actually worked.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql feedback;
  create table want1 as
  select *
  from one as o
  order by ifn(Total eq 0,Total+(-.1*Total+.01*placebo),Total) DESC
  ;
quit;

proc sql feedback;
  create table want as
  select *
  from one as o
  order by 
    case
      when total ne 0 then Total+(-.1*Total+.01*placebo)
      else total
      end
    DESC
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm not proposing to use this code&amp;nbsp;as it's too much of "smart arse". Just wanted to share as it amused me.&lt;/P&gt;</description>
      <pubDate>Sat, 24 Jun 2017 05:03:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-sort-a-data-set-OBS-by-different-sorting-order-in-same/m-p/370222#M88425</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-06-24T05:03:56Z</dc:date>
    </item>
  </channel>
</rss>

