<?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: Only take last month's entry in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Only-take-last-month-s-entry/m-p/666102#M79013</link>
    <description>&lt;P&gt;Your example output does not match your description. You say "So if a have two codes within the same month I only need the entry with the last date". Your data for ID=1 and Code=abc is from two different months, not within the same month.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you meant that you want the last &lt;STRONG&gt;Date&lt;/STRONG&gt; within each ID and code that would be&lt;/P&gt;
&lt;PRE&gt;Proc sort data=have;
   by id code date;
run;

data want;
   set have;
   by id code;
   if last.code;
run;&lt;/PRE&gt;
&lt;P&gt;Or provide a more consistent start data set and matching output.&lt;/P&gt;</description>
    <pubDate>Tue, 30 Jun 2020 14:41:38 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2020-06-30T14:41:38Z</dc:date>
    <item>
      <title>Only take last month's entry</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Only-take-last-month-s-entry/m-p/666085#M79012</link>
      <description>&lt;P&gt;Hello everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a two part question and hope you can help me with some SAS codes.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a table which contains the following (have):&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;Code&lt;/TD&gt;&lt;TD&gt;date&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt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t;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would only need the last entry for each ID and month. So if a have two codes within the same month I only need the entry with the last date. If an ID has two same codes at the same day, it doesnt matter which entry I take.&lt;/P&gt;&lt;P&gt;The data should not have different codes on the same day, but maybe you have an idea how I can check for that before reducing the table?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So my needed tabe would look like this (want):&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;Code&lt;/TD&gt;&lt;TD&gt;date&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;abc&lt;/TD&gt;&lt;TD&gt;26.10.2017&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;def&lt;/TD&gt;&lt;TD&gt;23.02.2018&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;ghi&lt;/TD&gt;&lt;TD&gt;04.05.2018&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;def&lt;/TD&gt;&lt;TD&gt;16.04.2019&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;abc&lt;/TD&gt;&lt;TD&gt;07.08.2019&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;abc&lt;/TD&gt;&lt;TD&gt;28.01.2016&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;ghi&lt;/TD&gt;&lt;TD&gt;03.11.2018&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;def&lt;/TD&gt;&lt;TD&gt;17.12.2019&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;In this example I only have maximum two entries for the same month, but theoretically it could also be more than two, relevant for me would only be the very last per each month.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I very much appreciate your help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jun 2020 14:14:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Only-take-last-month-s-entry/m-p/666085#M79012</guid>
      <dc:creator>Jay_Aguilar</dc:creator>
      <dc:date>2020-06-30T14:14:29Z</dc:date>
    </item>
    <item>
      <title>Re: Only take last month's entry</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Only-take-last-month-s-entry/m-p/666102#M79013</link>
      <description>&lt;P&gt;Your example output does not match your description. You say "So if a have two codes within the same month I only need the entry with the last date". Your data for ID=1 and Code=abc is from two different months, not within the same month.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you meant that you want the last &lt;STRONG&gt;Date&lt;/STRONG&gt; within each ID and code that would be&lt;/P&gt;
&lt;PRE&gt;Proc sort data=have;
   by id code date;
run;

data want;
   set have;
   by id code;
   if last.code;
run;&lt;/PRE&gt;
&lt;P&gt;Or provide a more consistent start data set and matching output.&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jun 2020 14:41:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Only-take-last-month-s-entry/m-p/666102#M79013</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-06-30T14:41:38Z</dc:date>
    </item>
    <item>
      <title>Re: Only take last month's entry</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Only-take-last-month-s-entry/m-p/666114#M79014</link>
      <description>Thank you for the input.&lt;BR /&gt;Maybe I did not explain it correctly, I try again:&lt;BR /&gt;What I meant was that I would need for each month the last dated entry and not for each ID only the last entry which would be the result of the code you provided.&lt;BR /&gt;Is it clear now?</description>
      <pubDate>Tue, 30 Jun 2020 15:13:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Only-take-last-month-s-entry/m-p/666114#M79014</guid>
      <dc:creator>Jay_Aguilar</dc:creator>
      <dc:date>2020-06-30T15:13:54Z</dc:date>
    </item>
    <item>
      <title>Re: Only take last month's entry</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Only-take-last-month-s-entry/m-p/666126#M79015</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/319424"&gt;@Jay_Aguilar&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Thank you for the input.&lt;BR /&gt;Maybe I did not explain it correctly, I try again:&lt;BR /&gt;What I meant was that I would need for each month the last dated entry and not for each ID only the last entry which would be the result of the code you provided.&lt;BR /&gt;Is it clear now?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Not understanding "each month" without a better example. WHY is the first row of&lt;/P&gt;
&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;abc&lt;/TD&gt;
&lt;TD&gt;26.10.2017&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&lt;EM&gt;1&lt;/EM&gt;&lt;/TD&gt;
&lt;TD&gt;&lt;EM&gt;abc&lt;/EM&gt;&lt;/TD&gt;
&lt;TD&gt;&lt;EM&gt;17.02.2018&lt;/EM&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;not in your output? It is a different month. So that should be there if Month is considered at all, in fact those aren't even in the same year. So All is see is the LAST DATE, not month. Provide an example where "last month" is easier to see and point it out.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And my code does not have the last ID but the last date for each Id and Code combination.&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jun 2020 15:36:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Only-take-last-month-s-entry/m-p/666126#M79015</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-06-30T15:36:30Z</dc:date>
    </item>
    <item>
      <title>Re: Only take last month's entry</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Only-take-last-month-s-entry/m-p/666132#M79017</link>
      <description>The output table does not contain&lt;BR /&gt;&lt;BR /&gt;ID Code date&lt;BR /&gt;1 abc 17.02.2018&lt;BR /&gt;&lt;BR /&gt;and just&lt;BR /&gt;ID Code date&lt;BR /&gt;1 abc 26.10.2017&lt;BR /&gt;1 def 23.02.2018&lt;BR /&gt;&lt;BR /&gt;because the entry of the 17.02.2018 for ID=1 is not needed. I just need the latest entry from February since there are two entries for February. For October 2017 there is only 1 entry, so I am fine. Cause per month and ID I need maximum 1 entry.&lt;BR /&gt;If I apply your code, I end up with only one row per ID.&lt;BR /&gt;</description>
      <pubDate>Tue, 30 Jun 2020 15:47:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Only-take-last-month-s-entry/m-p/666132#M79017</guid>
      <dc:creator>Jay_Aguilar</dc:creator>
      <dc:date>2020-06-30T15:47:49Z</dc:date>
    </item>
    <item>
      <title>Re: Only take last month's entry</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Only-take-last-month-s-entry/m-p/666137#M79018</link>
      <description>&lt;P&gt;You know your problem.&lt;/P&gt;
&lt;P&gt;You are not describing it well to others.&lt;/P&gt;
&lt;P&gt;So is February to be considered across ALL Id values? You did not say so very clearly&lt;/P&gt;
&lt;P&gt;. Or All Code values? Or does ID and Code have no role at all?&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jun 2020 16:02:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Only-take-last-month-s-entry/m-p/666137#M79018</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-06-30T16:02:43Z</dc:date>
    </item>
    <item>
      <title>Re: Only take last month's entry</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Only-take-last-month-s-entry/m-p/666140#M79019</link>
      <description>&lt;P&gt;hmm ok, I try it differently: The table I need at the end which should look like the want table above will be ued to join to a table which looks like this:&lt;BR /&gt;ID startdate enddate&lt;BR /&gt;1 01.10.2017 31.10.2017&lt;BR /&gt;1 01.11.2017 30.11.2017&lt;BR /&gt;1 01.12.2017 31.12.2017&lt;BR /&gt;…&lt;BR /&gt;1 01.02.2018 28.02.2018&lt;BR /&gt;1 01.03.2018 31.03.2018&lt;BR /&gt;…&lt;BR /&gt;2 01.05.2018 31.05.2018&lt;BR /&gt;2 01.06.2018 30.06.2018&lt;BR /&gt;…&lt;BR /&gt;2 01.04.2019 30.04.2019&lt;BR /&gt;2 01.05.2019 31.05.2019&lt;BR /&gt;&lt;BR /&gt;I have for every ID one row per month. The target would be now only get the information contained in the Code column for each row, so it would look like this:&lt;BR /&gt;ID startdate enddate Code&lt;BR /&gt;1 01.10.2017 31.10.2017 abc&lt;BR /&gt;1 01.11.2017 30.11.2017&lt;BR /&gt;1 01.12.2017 31.12.2017&lt;BR /&gt;…&lt;BR /&gt;1 01.02.2018 28.02.2018 def&lt;BR /&gt;1 01.03.2018 31.03.2018&lt;BR /&gt;…&lt;BR /&gt;2 01.05.2018 31.05.2018 ghi&lt;BR /&gt;2 01.06.2018 30.06.2018&lt;BR /&gt;…&lt;BR /&gt;2 01.04.2019 30.04.2019 def&lt;BR /&gt;2 01.05.2019 31.05.2019&lt;BR /&gt;&lt;BR /&gt;Therefore ID plays a role and date as well. The information contained in Code is only relevant once per month. Since in the have table I have 2 entries for ID=1 and month=Februray, I could not join them correctly to the final table, cause I would have two matching values for Code.&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jun 2020 16:19:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Only-take-last-month-s-entry/m-p/666140#M79019</guid>
      <dc:creator>Jay_Aguilar</dc:creator>
      <dc:date>2020-06-30T16:19:06Z</dc:date>
    </item>
    <item>
      <title>Re: Only take last month's entry</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Only-take-last-month-s-entry/m-p/666191#M79020</link>
      <description>&lt;P&gt;I too found the problem description a bit confusing.&amp;nbsp; What I think you want is, for each ID/MONTH take the latest date.&amp;nbsp;&amp;nbsp; If there is are tied records for the latest date, then you can take either reocrd because you expect them to have the same CODE value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If that is correct, and if your data are already sorted by ID/DATE, then :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input ID	Code :$3.	date ddmmyy10.;
  format date date9.;
datalines;
1	abc	26.10.2017
1	abc	17.02.2018
1	def	23.02.2018
2	ghi	04.05.2018
2	def	16.04.2019
3	abc	07.08.2019
4	abc	28.01.2016
4	ghi	03.11.2018
4	def	17.12.2019
4	def	17.12.2019
run;

data want;
  set have  end=end_of_have;
  by id ;
  if end_of_have=0 then set have (keep=date firstobs=2 rename=(date=nxt_date));
  if last.id=1 or intck('month',date,nxt_date)&amp;gt;0);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This program allows each iteration of the data step to look ahead one record to examine the subsequent date (variable nxt_date). The look-ahead is done in the second (conditional) SET statement which starts at record 2, keeps only DATE and renames it to NXT_DATE).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the record in hand is the last record for a given ID, or not in the same month as the subsequent record then keep it.&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jun 2020 19:32:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Only-take-last-month-s-entry/m-p/666191#M79020</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-06-30T19:32:03Z</dc:date>
    </item>
    <item>
      <title>Re: Only take last month's entry</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Only-take-last-month-s-entry/m-p/666341#M79024</link>
      <description>&lt;P&gt;&lt;A href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461" target="_blank"&gt;@mkeintz&lt;/A&gt;&lt;SPAN&gt;&amp;nbsp;that is exactly what I wanted: "each ID/MONTH take the latest date." Thank you for providing the code.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Thanks a lot!&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Jul 2020 11:52:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Only-take-last-month-s-entry/m-p/666341#M79024</guid>
      <dc:creator>Jay_Aguilar</dc:creator>
      <dc:date>2020-07-01T11:52:21Z</dc:date>
    </item>
  </channel>
</rss>

