<?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 observation based on the value provided in the column in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/order-observation-based-on-the-value-provided-in-the-column/m-p/558697#M155940</link>
    <description>It almost looks like you want to pull out the DESC column by TYPE and sort on that but that messes up the data because it's not aligned. So far this doesn't make sense to me. You could do that pretty easily and remerge it, just not sure how that could add any value to an analysis.</description>
    <pubDate>Tue, 14 May 2019 16:53:52 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2019-05-14T16:53:52Z</dc:date>
    <item>
      <title>order observation based on the value provided in the column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/order-observation-based-on-the-value-provided-in-the-column/m-p/558692#M155937</link>
      <description>&lt;P&gt;Deal SAS Users,&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;I would like to change the order of rows based on the values provided in the "order" column. for example. The order variable only contains the letter the first letter. however, the numbers will be sorted in descending order.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example:&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input type $ desc $ mo order $10.;
	datalines;
A D2 0 B,D
A D1 1 B,D
A B1 2 B,D
B D1 0 D,B
B B0 1 D,B
B D2 2 D,B
B B1 3 D,B
C D2 0 B,D
C D1 1 B,D
C B1 2 B,D
D X2 0 Z,Y,X
D Y1 1 Z,Y,X
D Z1 2 Z,Y,X
;
run;

data want;
	input type $ desc $ mo order $ 7-12 new_desc $ 13 - 15;
	datalines;
A D2 2 B,D   B1
A D1 0 B,D   D2
A B1 1 B,D   D1
B D1 2 D,B   D2
B B0 0 D,B   D1
B D2 3 D,B   B1
B B1 1 D,B   B0
C D2 2 B,D   B1
C D1 0 B,D   D2
C B1 1 B,D   D1
D X2 2 Z,Y,X Z1
D Y1 1 Z,Y,X Y1
D Z1 0 Z,Y,X X2
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 May 2019 16:42:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/order-observation-based-on-the-value-provided-in-the-column/m-p/558692#M155937</guid>
      <dc:creator>zqkal</dc:creator>
      <dc:date>2019-05-14T16:42:19Z</dc:date>
    </item>
    <item>
      <title>Re: order observation based on the value provided in the column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/order-observation-based-on-the-value-provided-in-the-column/m-p/558693#M155938</link>
      <description>&lt;P&gt;In your desired output data set, the rows are in the same order as they are in the input data set. So, I don't understand the question.&lt;/P&gt;</description>
      <pubDate>Tue, 14 May 2019 16:48:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/order-observation-based-on-the-value-provided-in-the-column/m-p/558693#M155938</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-05-14T16:48:45Z</dc:date>
    </item>
    <item>
      <title>Re: order observation based on the value provided in the column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/order-observation-based-on-the-value-provided-in-the-column/m-p/558697#M155940</link>
      <description>It almost looks like you want to pull out the DESC column by TYPE and sort on that but that messes up the data because it's not aligned. So far this doesn't make sense to me. You could do that pretty easily and remerge it, just not sure how that could add any value to an analysis.</description>
      <pubDate>Tue, 14 May 2019 16:53:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/order-observation-based-on-the-value-provided-in-the-column/m-p/558697#M155940</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-05-14T16:53:52Z</dc:date>
    </item>
    <item>
      <title>Re: order observation based on the value provided in the column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/order-observation-based-on-the-value-provided-in-the-column/m-p/558702#M155945</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45150"&gt;@zqkal&lt;/a&gt;&amp;nbsp; &amp;nbsp;The problem isn't hard and is rather simple with Hashes. Would you be comfortable maintaining it? Also,&amp;nbsp; is this Ad-hoc or for production ?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I know I should have posted the solution and then left those comments, but I would like to know beforehand as I am lazy.&lt;/P&gt;</description>
      <pubDate>Tue, 14 May 2019 17:12:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/order-observation-based-on-the-value-provided-in-the-column/m-p/558702#M155945</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-05-14T17:12:07Z</dc:date>
    </item>
    <item>
      <title>Re: order observation based on the value provided in the column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/order-observation-based-on-the-value-provided-in-the-column/m-p/558705#M155948</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt; it's better to ask now and then develop the solutions otherwise you waste your time developing solutions that may not be used. IMO asking for clarification first is always fine &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
      <pubDate>Tue, 14 May 2019 17:23:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/order-observation-based-on-the-value-provided-in-the-column/m-p/558705#M155948</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-05-14T17:23:02Z</dc:date>
    </item>
    <item>
      <title>Re: order observation based on the value provided in the column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/order-observation-based-on-the-value-provided-in-the-column/m-p/558707#M155950</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp; &amp;nbsp;Absolutely!&amp;nbsp; &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; and can't agree more at lunch time&lt;/P&gt;</description>
      <pubDate>Tue, 14 May 2019 17:24:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/order-observation-based-on-the-value-provided-in-the-column/m-p/558707#M155950</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-05-14T17:24:38Z</dc:date>
    </item>
    <item>
      <title>Re: order observation based on the value provided in the column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/order-observation-based-on-the-value-provided-in-the-column/m-p/558729#M155959</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45150"&gt;@zqkal&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
	input type $ desc $ mo order $10.;
	datalines;
A D2 0 B,D
A D1 1 B,D
A B1 2 B,D
B D1 0 D,B
B B0 1 D,B
B D2 2 D,B
B B1 3 D,B
C D2 0 B,D
C D1 1 B,D
C B1 2 B,D
D X2 0 Z,Y,X
D Y1 1 Z,Y,X
D Z1 2 Z,Y,X
;
run;


data temp ;
if _n_=1 then do;
if 0 then set have;
   dcl hash H (dataset:'have(drop=order)',multidata:'y') ;
   h.definekey  ("type") ;
   h.definedata (all:'y') ;
   h.definedone () ;
   end;
  set have(keep=type order);
  by type;
  if first.type;
  do _i=1 to countw(order,',');
  _k=scan(order,_i,',');
  	do rc=h.find() by 0 while (rc=0);
	if first(desc)=_k then output;
	rc=h.find_next();
	end;
end;
rename desc=newdesc;
drop _: rc;
run;

data want;
if 0 then set have;
merge have(keep=type desc) temp;
by type;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Of course, the TEMP step and the WANT step can quite comfortably be combined into one, but it easier to follow and maintain when as two passes.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 May 2019 18:38:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/order-observation-based-on-the-value-provided-in-the-column/m-p/558729#M155959</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-05-14T18:38:39Z</dc:date>
    </item>
    <item>
      <title>Re: order observation based on the value provided in the column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/order-observation-based-on-the-value-provided-in-the-column/m-p/558751#M155966</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45150"&gt;@zqkal&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Just in case that you "&lt;SPAN&gt;would like to change the order of rows based on the values provided in the 'order' column ...," i.e., not create a new variable (&lt;FONT face="courier new,courier"&gt;new_desc&lt;/FONT&gt;), but just sort the observations in a certain way, you can possibly use an ORDER BY clause in PROC SQL.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Example:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select * from have
order by type, indexc(order, first(desc)), input(substr(desc,2),16.) desc;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;This assumes that dataset HAVE is sorted by TYPE.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 14 May 2019 19:58:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/order-observation-based-on-the-value-provided-in-the-column/m-p/558751#M155966</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-05-14T19:58:59Z</dc:date>
    </item>
  </channel>
</rss>

