<?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 by two variables and keep the last record after sort in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Sort-by-two-variables-and-keep-the-last-record-after-sort/m-p/642605#M191725</link>
    <description>&lt;P&gt;If your time_stamp is a SAS datetime, it will be the correct sort order. Just try it.&lt;/P&gt;</description>
    <pubDate>Fri, 24 Apr 2020 14:17:10 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2020-04-24T14:17:10Z</dc:date>
    <item>
      <title>Sort by two variables and keep the last record after sort</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sort-by-two-variables-and-keep-the-last-record-after-sort/m-p/642583#M191718</link>
      <description>&lt;P&gt;I need to sort by two variables to keep the most valid one to the bottom. I then need to output this record to a dataset. How do I go about this? See example which is failing.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;All the best,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;	*Sort dataset by the variable payslip_ref (unique payslip id) and also time_stamp (when record came into the system or changed in system);
	proc sort data=&amp;amp;block;
		by time_stamp payslip_ref ;

	*With the block sorted by payslip_reference and time_stamp we tell SAS to keep the last unique record and also output previous records;
	data latest_data obsolete_data;
		set &amp;amp;block;
		by payslip_ref;

		if last.payslip_ref then
			output latest_data;
		else output obsolete_data;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 24 Apr 2020 13:32:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sort-by-two-variables-and-keep-the-last-record-after-sort/m-p/642583#M191718</guid>
      <dc:creator>Sean_OConnor</dc:creator>
      <dc:date>2020-04-24T13:32:14Z</dc:date>
    </item>
    <item>
      <title>Re: Sort by two variables and keep the last record after sort</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sort-by-two-variables-and-keep-the-last-record-after-sort/m-p/642590#M191719</link>
      <description>&lt;P&gt;Sort&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;by payslip_ref time_stamp;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 24 Apr 2020 13:45:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sort-by-two-variables-and-keep-the-last-record-after-sort/m-p/642590#M191719</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-04-24T13:45:04Z</dc:date>
    </item>
    <item>
      <title>Re: Sort by two variables and keep the last record after sort</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sort-by-two-variables-and-keep-the-last-record-after-sort/m-p/642594#M191720</link>
      <description>&lt;P&gt;Hi Kurt,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would need the newest timestamp at the bottom&amp;nbsp;&lt;/P&gt;&lt;P&gt;i.e 24/4/2020 14:47&lt;/P&gt;&lt;P&gt;24/4/2020 14:48&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So I would need to output 14:48&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 24 Apr 2020 13:48:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sort-by-two-variables-and-keep-the-last-record-after-sort/m-p/642594#M191720</guid>
      <dc:creator>Sean_OConnor</dc:creator>
      <dc:date>2020-04-24T13:48:24Z</dc:date>
    </item>
    <item>
      <title>Re: Sort by two variables and keep the last record after sort</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sort-by-two-variables-and-keep-the-last-record-after-sort/m-p/642605#M191725</link>
      <description>&lt;P&gt;If your time_stamp is a SAS datetime, it will be the correct sort order. Just try it.&lt;/P&gt;</description>
      <pubDate>Fri, 24 Apr 2020 14:17:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sort-by-two-variables-and-keep-the-last-record-after-sort/m-p/642605#M191725</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-04-24T14:17:10Z</dc:date>
    </item>
    <item>
      <title>Re: Sort by two variables and keep the last record after sort</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sort-by-two-variables-and-keep-the-last-record-after-sort/m-p/642609#M191728</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/116786"&gt;@Sean_OConnor&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi Kurt,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would need the newest timestamp at the bottom&amp;nbsp;&lt;/P&gt;
&lt;P&gt;i.e 24/4/2020 14:47&lt;/P&gt;
&lt;P&gt;24/4/2020 14:48&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So I would need to output 14:48&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Time values increase, so the largest value is the latest, i.e. "newest".&lt;/P&gt;
&lt;P&gt;If the sort doesn't work then likely you need to insure that your timestamp is actually a date time value and not character.&lt;/P&gt;</description>
      <pubDate>Fri, 24 Apr 2020 14:33:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sort-by-two-variables-and-keep-the-last-record-after-sort/m-p/642609#M191728</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-04-24T14:33:59Z</dc:date>
    </item>
    <item>
      <title>Re: Sort by two variables and keep the last record after sort</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sort-by-two-variables-and-keep-the-last-record-after-sort/m-p/642637#M191751</link>
      <description>&lt;P&gt;I forgot to mention my timestamp is numeric.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is the variable.&amp;nbsp; How do I increase the length to say 20 as my problem appears that is when it sorts it only takes into account the first 8 digits.&lt;/P&gt;&lt;P&gt;1576505690037&lt;/P&gt;</description>
      <pubDate>Fri, 24 Apr 2020 15:17:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sort-by-two-variables-and-keep-the-last-record-after-sort/m-p/642637#M191751</guid>
      <dc:creator>Sean_OConnor</dc:creator>
      <dc:date>2020-04-24T15:17:38Z</dc:date>
    </item>
    <item>
      <title>Re: Sort by two variables and keep the last record after sort</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sort-by-two-variables-and-keep-the-last-record-after-sort/m-p/642646#M191755</link>
      <description>&lt;P&gt;That number is not a SAS datetime value. Current datetime values have 10 digits (count of seconds from 1960-01-01T00:00:00):&lt;/P&gt;
&lt;PRE&gt; 73         data _null_;
 74         now = datetime();
 75         put now=;
 76         put now= e8601dt20.;
 77         run;
 
 now=1903370562.5
 now=2020-04-24T18:02:43
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please post your data in a data step with datalines. Make sure that the code works and creates a dataset like yours.&lt;/P&gt;</description>
      <pubDate>Fri, 24 Apr 2020 16:03:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sort-by-two-variables-and-keep-the-last-record-after-sort/m-p/642646#M191755</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-04-24T16:03:20Z</dc:date>
    </item>
    <item>
      <title>Re: Sort by two variables and keep the last record after sort</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sort-by-two-variables-and-keep-the-last-record-after-sort/m-p/642649#M191758</link>
      <description>&lt;P&gt;PS even with your values, the sort works:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input payslip_ref $ time_stamp;
format time_stamp 20.;
datalines;
12345 1576505690037
12345 1576505690036
;

title "Pre Sort";

proc print data=have noobs;
run;

proc sort data=have;
by payslip_ref time_stamp;
run;

title "Post Sort";

proc print data=have noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;Pre Sort
payslip_ref	time_stamp
12345	1576505690037
12345	1576505690036
Post Sort
payslip_ref	time_stamp
12345	1576505690036
12345	1576505690037
&lt;/PRE&gt;</description>
      <pubDate>Fri, 24 Apr 2020 16:05:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sort-by-two-variables-and-keep-the-last-record-after-sort/m-p/642649#M191758</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-04-24T16:05:15Z</dc:date>
    </item>
    <item>
      <title>Re: Sort by two variables and keep the last record after sort</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sort-by-two-variables-and-keep-the-last-record-after-sort/m-p/642653#M191759</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/116786"&gt;@Sean_OConnor&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I forgot to mention my timestamp is numeric.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is the variable.&amp;nbsp; How do I increase the length to say 20 as my problem appears that is when it sorts it only takes into account the first 8 digits.&lt;/P&gt;
&lt;P&gt;1576505690037&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If you are sorting a numeric value, regardless of the display format assigned, the entire value is used for determining order.&lt;/P&gt;
&lt;P&gt;However that value you show is NOT a SAS datetime value. Trimming of the last digit the result would be a datetime in the year 6955; 157650569003 =&amp;gt; 01OCT6955:08:43:23. So you really need to very carefully describe just what sort of value that is supposed to be. And perhaps go back to how you brought that data into SAS so the value is a valid date time.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or show us what your actual data set looks like. Instructions here: &lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712&lt;/A&gt; will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the &amp;lt;/&amp;gt; icon or attached as text to show exactly what you have and that we can test code against.&lt;/P&gt;</description>
      <pubDate>Fri, 24 Apr 2020 16:08:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sort-by-two-variables-and-keep-the-last-record-after-sort/m-p/642653#M191759</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-04-24T16:08:04Z</dc:date>
    </item>
  </channel>
</rss>

