<?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: latest 2 observation from each group using only PROC SQL in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/latest-2-observation-from-each-group-using-only-PROC-SQL/m-p/306684#M61041</link>
    <description>&lt;P&gt;I don't believe it's supported.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://support.sas.com/techsup/notes/v8/15/138.html" target="_blank"&gt;http://support.sas.com/techsup/notes/v8/15/138.html&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/7643"&gt;@Damien_Mather﻿&lt;/a&gt;&amp;nbsp;&amp;nbsp;Do you have something that indicates otherwise?&lt;/P&gt;</description>
    <pubDate>Mon, 24 Oct 2016 03:56:10 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2016-10-24T03:56:10Z</dc:date>
    <item>
      <title>latest 2 observation from each group using only PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/latest-2-observation-from-each-group-using-only-PROC-SQL/m-p/306317#M60999</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hi All,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to find latest 2 observation/transaction &amp;nbsp;from each group using only PROC SQL.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have tried to make my code, Kindly suggest if any improvization available:&lt;/P&gt;&lt;P&gt;Problem in my code is if i require latest 3 obs then i will need increase steps to get result.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Code below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;proc sql;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;create table new1 as&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;select distinct * from test group by product having transaction_date =max(transaction_date)&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;union &lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;select distinct * from &lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;(select * from test except &lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;(select distinct * from test group by product having transaction_date =max(transaction_date))) &lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;group by product having transaction_date =max(transaction_date) ;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;quit;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;: Xia&lt;/EM&gt;&lt;SPAN&gt; Keshan need help.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Oct 2016 15:05:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/latest-2-observation-from-each-group-using-only-PROC-SQL/m-p/306317#M60999</guid>
      <dc:creator>Aman4SAS</dc:creator>
      <dc:date>2016-10-21T15:05:48Z</dc:date>
    </item>
    <item>
      <title>Re: latest 2 observation from each group using only PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/latest-2-observation-from-each-group-using-only-PROC-SQL/m-p/306323#M61000</link>
      <description>&lt;P&gt;That will prove very complicated. &amp;nbsp;SQL works based on logical selection of data, not by random position in table. &amp;nbsp;To do this kind of thing you would first need to add an equivalent of _N_ to the data, so that SQL van logically take the max(_N_) and max(_N_)-1 obs from each group. &amp;nbsp;The question really is why bother, its far easier with a datastep, use the right tool for the right job - programming 101.&lt;/P&gt;</description>
      <pubDate>Fri, 21 Oct 2016 15:11:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/latest-2-observation-from-each-group-using-only-PROC-SQL/m-p/306323#M61000</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-10-21T15:11:55Z</dc:date>
    </item>
    <item>
      <title>Re: latest 2 observation from each group using only PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/latest-2-observation-from-each-group-using-only-PROC-SQL/m-p/306397#M61009</link>
      <description>&lt;P&gt;Why only SQL?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Oct 2016 17:44:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/latest-2-observation-from-each-group-using-only-PROC-SQL/m-p/306397#M61009</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-10-21T17:44:51Z</dc:date>
    </item>
    <item>
      <title>Re: latest 2 observation from each group using only PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/latest-2-observation-from-each-group-using-only-PROC-SQL/m-p/306496#M61023</link>
      <description>&lt;P&gt;HoHo. That is really not easy for SAS SQL.&lt;/P&gt;
&lt;P&gt;But easy for Data step.&lt;/P&gt;
&lt;P&gt;Or if you have index variable like:&lt;/P&gt;
&lt;P&gt;n sex&lt;/P&gt;
&lt;P&gt;1 F&lt;/P&gt;
&lt;P&gt;2 F&lt;/P&gt;
&lt;P&gt;1 M&lt;/P&gt;
&lt;P&gt;2 M&lt;/P&gt;
&lt;P&gt;3 M&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;that maybe would be easy as&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;select distinct * from test group by product having n &amp;nbsp;between max(n)-2 and max(n)&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 22 Oct 2016 03:11:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/latest-2-observation-from-each-group-using-only-PROC-SQL/m-p/306496#M61023</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-10-22T03:11:21Z</dc:date>
    </item>
    <item>
      <title>Re: latest 2 observation from each group using only PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/latest-2-observation-from-each-group-using-only-PROC-SQL/m-p/306519#M61026</link>
      <description>&lt;P&gt;Thanks for all reply.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As suggested by RW , i tried to delete this post but i was not able to delete.&lt;/P&gt;&lt;P&gt;I required only in SQL bcaz i know all other option to get result i.e. data step, merge but i have been asked to do it in SQL and my answer was same as RW Sir, and result, i am failed. so to be confirmed i have asked this to you legend people.&lt;/P&gt;&lt;P&gt;Thanks Xia Keshan for your attention on my question.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please suggest me how can i delete this post or do i need to keep it as it is.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 22 Oct 2016 04:55:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/latest-2-observation-from-each-group-using-only-PROC-SQL/m-p/306519#M61026</guid>
      <dc:creator>Aman4SAS</dc:creator>
      <dc:date>2016-10-22T04:55:23Z</dc:date>
    </item>
    <item>
      <title>Re: latest 2 observation from each group using only PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/latest-2-observation-from-each-group-using-only-PROC-SQL/m-p/306526#M61027</link>
      <description>&lt;P&gt;I don't see any need to delete your post. Also "don't do it this way" answers are valuable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;IF your data is stored in a database then the answer could be very well to use SQL; but you need to tell us which database and which volumes (like: you've got 100 million transactions but the expected result set will only be 100'000 thousand).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Depending on the database there can be native SQL language extensions which will make it much easier to write the required logic in SQL (like analytic functions).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 22 Oct 2016 05:35:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/latest-2-observation-from-each-group-using-only-PROC-SQL/m-p/306526#M61027</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2016-10-22T05:35:43Z</dc:date>
    </item>
    <item>
      <title>Re: latest 2 observation from each group using only PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/latest-2-observation-from-each-group-using-only-PROC-SQL/m-p/306527#M61028</link>
      <description>&lt;P&gt;Ha,&lt;/P&gt;
&lt;P&gt;Hope it is not too last. I figure out one way.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id date : date9. x;
format date date9.;
cards;
1 1jan2010 1
1 2jan2010 2
1 2jan2010 32
1 4feb2010 45
1 4feb2010 34
1 4dec2010 45
1 4dec2010 34
2 1jan2010 1
2 2jan2010 2
2 3jan2010 32
2 14feb2010 45
2 24feb2010 34
;
run;
proc sql;
select *,
(select count(*) from (select distinct id,date from have) as b
 where b.id=a.id and
  b.date between a.date and 
  (select max(date) from have where id=a.id)
) as count
from have as a
where calculated count in (1 2 3);
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 22 Oct 2016 05:48:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/latest-2-observation-from-each-group-using-only-PROC-SQL/m-p/306527#M61028</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-10-22T05:48:21Z</dc:date>
    </item>
    <item>
      <title>Re: latest 2 observation from each group using only PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/latest-2-observation-from-each-group-using-only-PROC-SQL/m-p/306544#M61031</link>
      <description>&lt;P&gt;Thanks a lot.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It works.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Connecting with you always return me learn something new. Its Great . Thanks once again.&lt;/P&gt;</description>
      <pubDate>Sat, 22 Oct 2016 10:01:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/latest-2-observation-from-each-group-using-only-PROC-SQL/m-p/306544#M61031</guid>
      <dc:creator>Aman4SAS</dc:creator>
      <dc:date>2016-10-22T10:01:26Z</dc:date>
    </item>
    <item>
      <title>Re: latest 2 observation from each group using only PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/latest-2-observation-from-each-group-using-only-PROC-SQL/m-p/306682#M61040</link>
      <description>the sql monotone() function is supported. select .,.,., monotone(date) as sequence, .,.,. would create an _n_ - like field.</description>
      <pubDate>Mon, 24 Oct 2016 03:45:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/latest-2-observation-from-each-group-using-only-PROC-SQL/m-p/306682#M61040</guid>
      <dc:creator>Damien_Mather</dc:creator>
      <dc:date>2016-10-24T03:45:41Z</dc:date>
    </item>
    <item>
      <title>Re: latest 2 observation from each group using only PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/latest-2-observation-from-each-group-using-only-PROC-SQL/m-p/306684#M61041</link>
      <description>&lt;P&gt;I don't believe it's supported.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://support.sas.com/techsup/notes/v8/15/138.html" target="_blank"&gt;http://support.sas.com/techsup/notes/v8/15/138.html&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/7643"&gt;@Damien_Mather﻿&lt;/a&gt;&amp;nbsp;&amp;nbsp;Do you have something that indicates otherwise?&lt;/P&gt;</description>
      <pubDate>Mon, 24 Oct 2016 03:56:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/latest-2-observation-from-each-group-using-only-PROC-SQL/m-p/306684#M61041</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-10-24T03:56:10Z</dc:date>
    </item>
    <item>
      <title>Re: latest 2 observation from each group using only PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/latest-2-observation-from-each-group-using-only-PROC-SQL/m-p/306724#M61051</link>
      <description>&lt;P&gt;You would also need to be careful using that function. &amp;nbsp;As I stated above, SQL has no real concept of record position, it can only pull out data as a logicl argument. &amp;nbsp;Monotonic() could return two different results depending on system setup, SQL options used, and various sortings and groupings.&lt;/P&gt;</description>
      <pubDate>Mon, 24 Oct 2016 08:20:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/latest-2-observation-from-each-group-using-only-PROC-SQL/m-p/306724#M61051</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-10-24T08:20:27Z</dc:date>
    </item>
    <item>
      <title>Re: latest 2 observation from each group using only PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/latest-2-observation-from-each-group-using-only-PROC-SQL/m-p/308228#M61158</link>
      <description>&lt;P&gt;Maybe you're right. Mainly I do follow the rules, but sometimes when I'm feeling a bit naughty I'll go crazy and try some tempting function I've seen in someone else's code examples in my own environments to see if they work regardless of what the documentation says. Don't tell Dr. Jim G., though, I think he thinks I'm an OK guy.&lt;/P&gt;</description>
      <pubDate>Mon, 31 Oct 2016 10:01:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/latest-2-observation-from-each-group-using-only-PROC-SQL/m-p/308228#M61158</guid>
      <dc:creator>Damien_Mather</dc:creator>
      <dc:date>2016-10-31T10:01:45Z</dc:date>
    </item>
  </channel>
</rss>

