<?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: Keep the observations with the most recent date in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Keep-the-observations-with-the-most-recent-date/m-p/695583#M25190</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/335985"&gt;@Barkat&lt;/a&gt;&amp;nbsp; Since you didn't post your expected output for your input sample, please see if the following is what you are after i.e. whether my understanding of your requirement is correct-&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input id	TrainingDate :mmddyy10.	Score	TestDate :mmddyy10.;
format TrainingDate mmddyy10. TestDate mmddyy10.;
cards;
1	11/21/16	3	08/26/10
1	11/21/16	3	06/27/11
1	11/21/16	7	10/20/16
1	11/21/16	3	06/13/17
1	11/21/16	2.5	08/13/17
2	04/23/18	5	11/15/10
2	04/23/18	3.3	10/26/11
2	04/23/18	13.4	10/26/11
2	04/23/18	3.3	11/28/12
2	04/23/18	11.8	11/28/12
2	04/23/18	6	12/27/17
2	04/23/18	3.6	05/09/18
2	04/23/18	30	08/10/18
2	04/23/18	2.6	01/02/19
3	10/08/18	3	06/11/11
3	10/08/18	5	05/14/18
3	10/08/18	4	06/11/18
3	10/08/18	3	08/13/18
4	12/06/16	1.4	07/01/17
4	12/06/16	4.6	11/17/17
4	12/06/16	2	07/19/18
;
proc sql;
 create table want as
 select *
 from have
 group by id
 having max(ifn(TestDate&amp;lt;=TrainingDate,TestDate,.))=testdate or min(ifn(TestDate&amp;gt;TrainingDate,TestDate,.))=testdate
 order by id,TestDate;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 30 Oct 2020 23:32:11 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2020-10-30T23:32:11Z</dc:date>
    <item>
      <title>Keep the observations with the most recent date</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Keep-the-observations-with-the-most-recent-date/m-p/695580#M25189</link>
      <description>&lt;P&gt;I have a dataset (attached) with column names&amp;nbsp;id, TrainingDate, Score, TestDate.&amp;nbsp;Each id has a single TrainingDate and multiple TestDate with score. Some TestDates are before TrainngDate and some TestDates are after TrainingDate. I would like to keep the&amp;nbsp;closest&amp;nbsp;TestDate before the TraingDate and the closest TestDate after the TrainingDate.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 30 Oct 2020 20:05:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Keep-the-observations-with-the-most-recent-date/m-p/695580#M25189</guid>
      <dc:creator>Barkat</dc:creator>
      <dc:date>2020-10-30T20:05:19Z</dc:date>
    </item>
    <item>
      <title>Re: Keep the observations with the most recent date</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Keep-the-observations-with-the-most-recent-date/m-p/695583#M25190</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/335985"&gt;@Barkat&lt;/a&gt;&amp;nbsp; Since you didn't post your expected output for your input sample, please see if the following is what you are after i.e. whether my understanding of your requirement is correct-&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input id	TrainingDate :mmddyy10.	Score	TestDate :mmddyy10.;
format TrainingDate mmddyy10. TestDate mmddyy10.;
cards;
1	11/21/16	3	08/26/10
1	11/21/16	3	06/27/11
1	11/21/16	7	10/20/16
1	11/21/16	3	06/13/17
1	11/21/16	2.5	08/13/17
2	04/23/18	5	11/15/10
2	04/23/18	3.3	10/26/11
2	04/23/18	13.4	10/26/11
2	04/23/18	3.3	11/28/12
2	04/23/18	11.8	11/28/12
2	04/23/18	6	12/27/17
2	04/23/18	3.6	05/09/18
2	04/23/18	30	08/10/18
2	04/23/18	2.6	01/02/19
3	10/08/18	3	06/11/11
3	10/08/18	5	05/14/18
3	10/08/18	4	06/11/18
3	10/08/18	3	08/13/18
4	12/06/16	1.4	07/01/17
4	12/06/16	4.6	11/17/17
4	12/06/16	2	07/19/18
;
proc sql;
 create table want as
 select *
 from have
 group by id
 having max(ifn(TestDate&amp;lt;=TrainingDate,TestDate,.))=testdate or min(ifn(TestDate&amp;gt;TrainingDate,TestDate,.))=testdate
 order by id,TestDate;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 30 Oct 2020 23:32:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Keep-the-observations-with-the-most-recent-date/m-p/695583#M25190</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-10-30T23:32:11Z</dc:date>
    </item>
    <item>
      <title>Re: Keep the observations with the most recent date</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Keep-the-observations-with-the-most-recent-date/m-p/695584#M25191</link>
      <description>&lt;P&gt;Sort by id and testdate, then you can do a "look-ahead merge" (firstobs=2) of the dataset with itself.&lt;/P&gt;
&lt;P&gt;For code, please supply example data in usable form (as code in a data step with datalines).&lt;/P&gt;</description>
      <pubDate>Fri, 30 Oct 2020 20:28:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Keep-the-observations-with-the-most-recent-date/m-p/695584#M25191</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-10-30T20:28:19Z</dc:date>
    </item>
    <item>
      <title>Re: Keep the observations with the most recent date</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Keep-the-observations-with-the-most-recent-date/m-p/695662#M25193</link>
      <description>&lt;P&gt;Some fun with Hashes-&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id	TrainingDate :mmddyy10.	Score	TestDate :mmddyy10.;
format TrainingDate mmddyy10. TestDate mmddyy10.;
cards;
1	11/21/16	3	08/26/10
1	11/21/16	3	06/27/11
1	11/21/16	7	10/20/16
1	11/21/16	3	06/13/17
1	11/21/16	2.5	08/13/17
2	04/23/18	5	11/15/10
2	04/23/18	3.3	10/26/11
2	04/23/18	13.4	10/26/11
2	04/23/18	3.3	11/28/12
2	04/23/18	11.8	11/28/12
2	04/23/18	6	12/27/17
2	04/23/18	3.6	05/09/18
2	04/23/18	30	08/10/18
2	04/23/18	2.6	01/02/19
3	10/08/18	3	06/11/11
3	10/08/18	5	05/14/18
3	10/08/18	4	06/11/18
3	10/08/18	3	08/13/18
4	12/06/16	1.4	07/01/17
4	12/06/16	4.6	11/17/17
4	12/06/16	2	07/19/18
;

data want;
 if _n_=1 then do;
  dcl hash h(dataset:'have(obs=0)',multidata:'y');
  h.definekey('id');
  h.definedata(all:'y');
  h.definedone();
  dcl hiter hi('h');
 end;
 do until(last.id);
  set have;
  by id;
  if h.num_items =2 then continue;
  if TestDate&amp;lt;=TrainingDate then h.replace();
  else if _n_ then do;
   h.add();
   _n_=0;
  end;
 end;
 do while(hi.next()=0);
  output;
 end;
 h.clear();
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 31 Oct 2020 16:40:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Keep-the-observations-with-the-most-recent-date/m-p/695662#M25193</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-10-31T16:40:55Z</dc:date>
    </item>
    <item>
      <title>Re: Keep the observations with the most recent date</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Keep-the-observations-with-the-most-recent-date/m-p/695777#M25208</link>
      <description>&lt;P&gt;It appears your data are already sorted by id/testdate.&amp;nbsp; If so, then:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  where trainingdate&amp;gt;=testdate;
  by id;
  if last.id;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The WHERE statement "outsources" the filter to keep only qualifying testdates.&amp;nbsp; The DATA steps never sees the non-qualifying testdates.&amp;nbsp; Therefore the last testdate seen for each id&amp;nbsp; (the "by id;" and "if last.id" statements) is the most recent qualifying testdate.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The important point here is the operational difference between the where statement and the subsetting if statement.&amp;nbsp; The latter filters only the records that are seen by the data step, while the former, as said above, filters prior to processing by the data step.&lt;/P&gt;</description>
      <pubDate>Sun, 01 Nov 2020 20:43:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Keep-the-observations-with-the-most-recent-date/m-p/695777#M25208</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-11-01T20:43:43Z</dc:date>
    </item>
    <item>
      <title>Re: Keep the observations with the most recent date</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Keep-the-observations-with-the-most-recent-date/m-p/696053#M25227</link>
      <description>&lt;P&gt;Thanks! Could you help me learn&amp;nbsp;the full procedure&amp;nbsp;by recommending some materials.&lt;/P&gt;</description>
      <pubDate>Mon, 02 Nov 2020 22:55:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Keep-the-observations-with-the-most-recent-date/m-p/696053#M25227</guid>
      <dc:creator>Barkat</dc:creator>
      <dc:date>2020-11-02T22:55:46Z</dc:date>
    </item>
    <item>
      <title>Re: Keep the observations with the most recent date</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Keep-the-observations-with-the-most-recent-date/m-p/696057#M25228</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/335985"&gt;@Barkat&lt;/a&gt;&amp;nbsp; Follo&lt;SPAN&gt;wing are the books I&amp;nbsp;would recommend-&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;1.&amp;nbsp;&lt;STRONG&gt;By&amp;nbsp;&lt;EM&gt;Ron Cody&lt;/EM&gt;. Learning&amp;nbsp;&lt;EM&gt;SAS&lt;/EM&gt;&lt;SUP&gt;®&lt;/SUP&gt;&amp;nbsp;by Example&lt;/STRONG&gt;: A Programmer's Guide, Second Edition-&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2.&amp;nbsp;&lt;EM&gt;&lt;STRONG&gt;Practical and Efficient SAS Programming&lt;/STRONG&gt;: The Insider's Guide&lt;/EM&gt;: Messineo, Martha&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;3.&amp;nbsp;&lt;STRONG&gt;&lt;EM&gt;High&lt;/EM&gt;-&lt;EM&gt;Performance SAS Coding&lt;/EM&gt;&lt;/STRONG&gt;&amp;nbsp;--Graffeuille, Christian&amp;nbsp; aka&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;4.&amp;nbsp;&lt;STRONG&gt;Data Management Solutions Using&amp;nbsp;&lt;EM&gt;SAS&lt;/EM&gt;&amp;nbsp;Hash Table Operations:&lt;/STRONG&gt; A Business Intelligence Case Study eBook:&amp;nbsp;Dorfman,Paul, Henderson, Don&lt;STRONG&gt;&amp;nbsp; courtesy&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/21262"&gt;@hashman&lt;/a&gt;&amp;nbsp;/&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13569"&gt;@DonH&lt;/a&gt;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Start&amp;nbsp;&lt;SPAN&gt;with &lt;STRONG&gt;1 &amp;amp; 2&lt;/STRONG&gt;. Learn, understand &amp;amp; practice. This is &lt;U&gt;&lt;STRONG&gt;absolute&lt;/STRONG&gt;&lt;/U&gt;. Take your time before you hit 3 &amp;amp; 4 until you have a solid grasp of 1,2.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Source:&lt;/STRONG&gt;&lt;/U&gt; I have a paid subscription&amp;nbsp;with&amp;nbsp;&lt;A href="https://www.oreilly.com/" target="_blank"&gt;https://www.oreilly.com/&lt;/A&gt;&amp;nbsp;&lt;EM&gt;&lt;STRONG&gt;e-library&lt;/STRONG&gt;&lt;/EM&gt;. It is expensive ho&lt;SPAN&gt;wever my life is dependent on it, so I need it. Depending on your professional needs you may choose to subscribe or buy each one of these books on &lt;STRONG&gt;Amazon or SAS press&lt;/STRONG&gt;. Best!&lt;/SPAN&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/14323"&gt;@ShelleySessoms&lt;/a&gt;&amp;nbsp; Mam, any thoughts or&amp;nbsp;&lt;SPAN&gt;would you like to add something more or perhaps advice a prospective discount on offer?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 02 Nov 2020 23:52:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Keep-the-observations-with-the-most-recent-date/m-p/696057#M25228</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-11-02T23:52:02Z</dc:date>
    </item>
    <item>
      <title>Re: Keep the observations with the most recent date</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Keep-the-observations-with-the-most-recent-date/m-p/696168#M25230</link>
      <description>&lt;P&gt;You have a great list&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;. I don't have anything additional to add at this time.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As far as discounts, you can always visit&amp;nbsp;&lt;A href="https://support.sas.com/en/books.html" target="_blank"&gt;https://support.sas.com/en/books.html&lt;/A&gt;&amp;nbsp;and see what is available. The SAS Books teams lists all discounts and offers on their website. There is always a "book of the month" and a list of free e-books.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Happy learning,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/335985"&gt;@Barkat&lt;/a&gt;.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 03 Nov 2020 12:34:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Keep-the-observations-with-the-most-recent-date/m-p/696168#M25230</guid>
      <dc:creator>ShelleySessoms</dc:creator>
      <dc:date>2020-11-03T12:34:54Z</dc:date>
    </item>
  </channel>
</rss>

