<?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 Reordering data by date, but grouping ids in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Reordering-data-by-date-but-grouping-ids/m-p/531299#M73740</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I have a data set as;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Id&amp;nbsp; &amp;nbsp;Date&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; 2005&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; 2008&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; 2004&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; 2012&lt;/P&gt;&lt;P&gt;4&amp;nbsp; &amp;nbsp; 2001&lt;/P&gt;&lt;P&gt;5&amp;nbsp; &amp;nbsp; 2016&lt;/P&gt;&lt;P&gt;5&amp;nbsp; &amp;nbsp; 2007&lt;/P&gt;&lt;P&gt;5&amp;nbsp; &amp;nbsp; 2010&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to order the data so that it is sorted by date order, but keep the same id's together, so it would look like this;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Id&amp;nbsp; &amp;nbsp;Date&lt;/P&gt;&lt;P&gt;4&amp;nbsp; &amp;nbsp; &lt;STRONG&gt;2001&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &lt;STRONG&gt;2004&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; 2012&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &lt;STRONG&gt;2005&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;5&amp;nbsp; &amp;nbsp; &lt;STRONG&gt;2007&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;5&amp;nbsp; &amp;nbsp; 2010&lt;/P&gt;&lt;P&gt;5&amp;nbsp; &amp;nbsp; 2016&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &lt;STRONG&gt;2008&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The only way i have found to do his so far is using proc sort, but this seems to either order by date which muddles the ids, or I can sort by dates within id but this doesn't reorder the id's so that the earliest is first.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help is really appreciated, thanks in advance &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 30 Jan 2019 09:36:26 GMT</pubDate>
    <dc:creator>Cydney</dc:creator>
    <dc:date>2019-01-30T09:36:26Z</dc:date>
    <item>
      <title>Reordering data by date, but grouping ids</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Reordering-data-by-date-but-grouping-ids/m-p/531299#M73740</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I have a data set as;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Id&amp;nbsp; &amp;nbsp;Date&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; 2005&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; 2008&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; 2004&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; 2012&lt;/P&gt;&lt;P&gt;4&amp;nbsp; &amp;nbsp; 2001&lt;/P&gt;&lt;P&gt;5&amp;nbsp; &amp;nbsp; 2016&lt;/P&gt;&lt;P&gt;5&amp;nbsp; &amp;nbsp; 2007&lt;/P&gt;&lt;P&gt;5&amp;nbsp; &amp;nbsp; 2010&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to order the data so that it is sorted by date order, but keep the same id's together, so it would look like this;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Id&amp;nbsp; &amp;nbsp;Date&lt;/P&gt;&lt;P&gt;4&amp;nbsp; &amp;nbsp; &lt;STRONG&gt;2001&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &lt;STRONG&gt;2004&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; 2012&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &lt;STRONG&gt;2005&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;5&amp;nbsp; &amp;nbsp; &lt;STRONG&gt;2007&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;5&amp;nbsp; &amp;nbsp; 2010&lt;/P&gt;&lt;P&gt;5&amp;nbsp; &amp;nbsp; 2016&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &lt;STRONG&gt;2008&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The only way i have found to do his so far is using proc sort, but this seems to either order by date which muddles the ids, or I can sort by dates within id but this doesn't reorder the id's so that the earliest is first.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help is really appreciated, thanks in advance &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 30 Jan 2019 09:36:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Reordering-data-by-date-but-grouping-ids/m-p/531299#M73740</guid>
      <dc:creator>Cydney</dc:creator>
      <dc:date>2019-01-30T09:36:26Z</dc:date>
    </item>
    <item>
      <title>Re: Reordering data by date, but grouping ids</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Reordering-data-by-date-but-grouping-ids/m-p/531304#M73741</link>
      <description>&lt;P&gt;Well, probably the reason why you can't get that sort, and why i can see no order, is because there doesn't appear to be any logical ordering.&amp;nbsp; Please can you explain exactly&amp;nbsp; - giving the specific examples therin, what the order here is?&amp;nbsp; From what I can see ID is not in order, nor is date, therefore the data is not ordered.&amp;nbsp; You say grouping by ids, but again the data is not grouped by ids, its not grouped by anything.&lt;/P&gt;</description>
      <pubDate>Wed, 30 Jan 2019 10:01:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Reordering-data-by-date-but-grouping-ids/m-p/531304#M73741</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2019-01-30T10:01:05Z</dc:date>
    </item>
    <item>
      <title>Re: Reordering data by date, but grouping ids</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Reordering-data-by-date-but-grouping-ids/m-p/531321#M73742</link>
      <description>&lt;P&gt;Yes sorry, this list will be used to chase up data so needs to be in date order to make the task easier, but whilst looking up a specific id with multiple records it would make more sense to check them all in one go.&lt;/P&gt;&lt;P&gt;If you look at just the first instance of each id they are all in date order i.e&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Id&amp;nbsp; &amp;nbsp;Date&lt;/P&gt;&lt;P&gt;4&amp;nbsp; &amp;nbsp;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;2001&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;2004&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;2005&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;5&amp;nbsp; &amp;nbsp;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;2007&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;2008&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But rather than have the extra records for each case in date order i want to keep them with the first record&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 30 Jan 2019 10:39:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Reordering-data-by-date-but-grouping-ids/m-p/531321#M73742</guid>
      <dc:creator>Cydney</dc:creator>
      <dc:date>2019-01-30T10:39:28Z</dc:date>
    </item>
    <item>
      <title>Re: Reordering data by date, but grouping ids</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Reordering-data-by-date-but-grouping-ids/m-p/531333#M73743</link>
      <description>&lt;P&gt;Create a format for the chronological order, and then use the resulting variable:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id date;
cards;
1 2005
2 2008
3 2004
3 2012
4 2001
5 2016
5 2007
5 2010
;
run;

/* create a format that holds a sequential number for id's, based on the first year for each */
proc sort
  data=have
  out=cntlin (rename=(id=start))
;
by id date;
run;

data cntlin;
set cntlin;
by start;
if first.start;
run;

proc sort data=cntlin;
by date;
run;

data cntlin;
set cntlin;
label = _n_;
fmtname = 'myfmt';
type = 'N';
run;

proc format cntlin=cntlin;
run;

/* use that format */
data want;
set have;
number = put(id,myfmt.);
run;

/* create final order */
proc sort data=want;
by number date;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 30 Jan 2019 11:28:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Reordering-data-by-date-but-grouping-ids/m-p/531333#M73743</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-01-30T11:28:05Z</dc:date>
    </item>
    <item>
      <title>Re: Reordering data by date, but grouping ids</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Reordering-data-by-date-but-grouping-ids/m-p/531335#M73744</link>
      <description>&lt;P&gt;Glad you understand what was needed&amp;nbsp;&lt;img id="smileyfrustrated" class="emoticon emoticon-smileyfrustrated" src="https://communities.sas.com/i/smilies/16x16_smiley-frustrated.png" alt="Smiley Frustrated" title="Smiley Frustrated" /&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 30 Jan 2019 11:30:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Reordering-data-by-date-but-grouping-ids/m-p/531335#M73744</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2019-01-30T11:30:10Z</dc:date>
    </item>
    <item>
      <title>Re: Reordering data by date, but grouping ids</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Reordering-data-by-date-but-grouping-ids/m-p/531338#M73745</link>
      <description>&lt;P&gt;Hi Kurt,&lt;/P&gt;&lt;P&gt;Thank you very much this worked brilliantly that is the exact order i was looking for!&lt;/P&gt;</description>
      <pubDate>Wed, 30 Jan 2019 11:48:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Reordering-data-by-date-but-grouping-ids/m-p/531338#M73745</guid>
      <dc:creator>Cydney</dc:creator>
      <dc:date>2019-01-30T11:48:45Z</dc:date>
    </item>
    <item>
      <title>Re: Reordering data by date, but grouping ids</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Reordering-data-by-date-but-grouping-ids/m-p/531422#M73746</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input id date;
cards;
1 2005
2 2008
3 2004
3 2012
4 2001
5 2016
5 2007
5 2010
;
run;


proc sort data=have out=_h;
by date id;
run;

data t;
if _n_=1 then do;
  declare hash H (multidata:'y',ordered:'y') ;
   h.definekey  ("id") ;
   h.definedata ("seq") ;
    h.definedone () ;
end;
set _h end=l;
if h.find() ne 0 then seq+1;
h.replace();
run;

proc sort data=t out=want(drop=seq);
by seq date;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 30 Jan 2019 16:12:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Reordering-data-by-date-but-grouping-ids/m-p/531422#M73746</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-01-30T16:12:57Z</dc:date>
    </item>
  </channel>
</rss>

