<?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 observations matches in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Keep-observations-matches/m-p/796538#M255615</link>
    <description>Thank you! I should sort by id type and date and I think this should work!</description>
    <pubDate>Wed, 16 Feb 2022 13:20:15 GMT</pubDate>
    <dc:creator>Emma2021</dc:creator>
    <dc:date>2022-02-16T13:20:15Z</dc:date>
    <item>
      <title>Keep observations matches</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-observations-matches/m-p/796200#M255476</link>
      <description>&lt;P&gt;I have the following data:&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;input id type $ date :mmddyy10.;&lt;BR /&gt;format type $3. date mmddyy10.;&lt;BR /&gt;cards;&lt;BR /&gt;1 A-2 1/1/2021&lt;BR /&gt;1 A-1 1/4/2021&lt;BR /&gt;1 A-1 1/20/2021&lt;BR /&gt;2 A-1 1/1/2021&lt;BR /&gt;2 A-1 1/4/2021&lt;BR /&gt;3 A-1 1/1/2021&lt;BR /&gt;3 A-2 1/4/2021&lt;BR /&gt;4 A-2 1/1/2021&lt;BR /&gt;4 A-3 1/4/2021&lt;BR /&gt;4 A-1 1/5/2021&lt;BR /&gt;5 A-2 2/1/2021&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to keep only all observations with 10 or more days difference by ID, but if within 10-days then type should be with smallest value's row (see id=1 and type=A-1; also id=4 and type=A-1) even the date was higher than the previous date, see wanted data:&lt;/P&gt;
&lt;TABLE width="268"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="66"&gt;ID&lt;/TD&gt;
&lt;TD width="114"&gt;TYPE&lt;/TD&gt;
&lt;TD width="88"&gt;DATE&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;A-1&lt;/TD&gt;
&lt;TD&gt;1/4/2021&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;A-1&lt;/TD&gt;
&lt;TD&gt;1/20/2021&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;A-1&lt;/TD&gt;
&lt;TD&gt;1/1/2021&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;TD&gt;A-1&lt;/TD&gt;
&lt;TD&gt;1/1/2021&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;4&lt;/TD&gt;
&lt;TD&gt;A-1&lt;/TD&gt;
&lt;TD&gt;1/5/2021&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;5&lt;/TD&gt;
&lt;TD&gt;A-2&lt;/TD&gt;
&lt;TD&gt;2/1/2021&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How can I accomplish this? Thank you.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Feb 2022 04:41:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-observations-matches/m-p/796200#M255476</guid>
      <dc:creator>Emma2021</dc:creator>
      <dc:date>2022-02-15T04:41:31Z</dc:date>
    </item>
    <item>
      <title>Re: Keep observations matches</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-observations-matches/m-p/796231#M255485</link>
      <description>&lt;P&gt;I have two approaches, one using two outputs, the other a look-ahead:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by id date;
retain _date _type;
if first.id
then do;
  _type = type;
  _date = date;
end;
_type = ifc(type &amp;lt; _type,type,_type);
if date - _date gt 10
then do;
  _t = type;
  _d = date;
  type = _type;
  date = _date;
  output;
  _type = _t;
  date = _d;
end;
if last.id
then do;
  type = _type;
  output;
end;
_date = date;
drop _:;
run;

data want2;
set have;
by id;
set
  have (
    firstobs=2
    keep=id date
    rename=(
      id = _id
      date = _date
    )
  )
  have (
    obs=1
    keep=id date
    rename=(
      id = _id
      date = _date
    )
  )
;
retain _start _type;
if first.id
then do;
  _start = date;
  _type = type;
end;
_type = ifc(type &amp;lt; _type,type,_type);
if _id ne id or _date gt _start + 10
then do;
  type = _type;
  output;
end;
drop _:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 15 Feb 2022 08:52:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-observations-matches/m-p/796231#M255485</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-02-15T08:52:04Z</dc:date>
    </item>
    <item>
      <title>Re: Keep observations matches</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-observations-matches/m-p/796441#M255574</link>
      <description>&lt;P&gt;The code does not work for below data (when the same type and within 10 days it shifts the date, but I do not want to shift any date):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;input id type $ date :mmddyy10.;&lt;BR /&gt;format type $3. date mmddyy10.;&lt;BR /&gt;cards;&lt;BR /&gt;1 A-2 1/1/2021&lt;BR /&gt;1 A-1 1/4/2021&lt;BR /&gt;1 A-1 1/20/2021&lt;BR /&gt;1 A-1 1/23/2021&lt;BR /&gt;1 A-1 1/25/2021&lt;BR /&gt;1 A-0 1/25/2021&lt;BR /&gt;2 A-1 1/1/2021&lt;BR /&gt;2 A-1 1/4/2021&lt;BR /&gt;3 A-1 1/1/2021&lt;BR /&gt;3 A-2 1/4/2021&lt;BR /&gt;4 A-2 1/1/2021&lt;BR /&gt;4 A-3 1/4/2021&lt;BR /&gt;4 A-1 1/5/2021&lt;BR /&gt;5 A-2 2/1/2021&lt;BR /&gt;6 0-B 07/22/2020 &lt;BR /&gt;6 1-B 08/05/2021 &lt;BR /&gt;6 1-B 08/11/2021 &lt;BR /&gt;6 1-B 08/18/2021 &lt;BR /&gt;6 1-B 01/26/2022 &lt;BR /&gt;;&lt;/P&gt;
&lt;P&gt;data wanted;&lt;BR /&gt;input id type $ date :mmddyy10.;&lt;BR /&gt;format type $3. date mmddyy10.;&lt;BR /&gt;cards;&lt;BR /&gt;1 A-1 1/4/2021&lt;BR /&gt;1 A-0 1/25/2021&lt;BR /&gt;2 A-1 1/1/2021&lt;BR /&gt;3 A-1 1/1/2021&lt;BR /&gt;4 A-1 1/5/2021&lt;BR /&gt;5 A-2 2/1/2021&lt;BR /&gt;6 0-B 07/22/2020 &lt;BR /&gt;6 1-B 08/05/2021 &lt;BR /&gt;6 1-B 01/26/2022 &lt;BR /&gt;;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Feb 2022 03:11:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-observations-matches/m-p/796441#M255574</guid>
      <dc:creator>Emma2021</dc:creator>
      <dc:date>2022-02-16T03:11:59Z</dc:date>
    </item>
    <item>
      <title>Re: Keep observations matches</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-observations-matches/m-p/796442#M255575</link>
      <description>Thank you, but the both codes do not work.</description>
      <pubDate>Wed, 16 Feb 2022 03:12:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-observations-matches/m-p/796442#M255575</guid>
      <dc:creator>Emma2021</dc:creator>
      <dc:date>2022-02-16T03:12:29Z</dc:date>
    </item>
    <item>
      <title>Re: Keep observations matches</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-observations-matches/m-p/796456#M255581</link>
      <description>&lt;P&gt;Assuming the data are sorted by ID/DATE, I believe your criterion can be restated as "keep the first record for each ID, and all other records that are more than 10 days after their predecessor".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If so, then it's easy:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id type $ date :mmddyy10.;
format type $3. date mmddyy10.;
cards;
1 A-2 1/1/2021
1 A-1 1/4/2021
1 A-1 1/20/2021
2 A-1 1/1/2021
2 A-1 1/4/2021
3 A-1 1/1/2021
3 A-2 1/4/2021
4 A-2 1/1/2021
4 A-3 1/4/2021
4 A-1 1/5/2021
5 A-2 2/1/2021
run;

data want;
  set have ;
  by id;

  if first.id=1 or date-10&amp;gt;lag(date);
run;
  &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Feb 2022 05:34:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-observations-matches/m-p/796456#M255581</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-02-16T05:34:35Z</dc:date>
    </item>
    <item>
      <title>Re: Keep observations matches</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-observations-matches/m-p/796490#M255596</link>
      <description>&lt;P&gt;Your WANT data is inconsistent.&lt;/P&gt;
&lt;P&gt;For the first "window" for ID 1 and 4, you take the last date, but for the windows of ID 2 and 3 you take the first.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please restate your requirements in a complete and consistent way for all variables in the WANT dataset.&lt;/P&gt;</description>
      <pubDate>Wed, 16 Feb 2022 09:03:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-observations-matches/m-p/796490#M255596</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-02-16T09:03:18Z</dc:date>
    </item>
    <item>
      <title>Re: Keep observations matches</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-observations-matches/m-p/796538#M255615</link>
      <description>Thank you! I should sort by id type and date and I think this should work!</description>
      <pubDate>Wed, 16 Feb 2022 13:20:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-observations-matches/m-p/796538#M255615</guid>
      <dc:creator>Emma2021</dc:creator>
      <dc:date>2022-02-16T13:20:15Z</dc:date>
    </item>
    <item>
      <title>Re: Keep observations matches</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-observations-matches/m-p/796577#M255634</link>
      <description>Sorry it does not work too: see below data:&lt;BR /&gt;&lt;BR /&gt;data have;&lt;BR /&gt;input id type $ date :mmddyy10.;&lt;BR /&gt;format type $3. date mmddyy10.;&lt;BR /&gt;cards;&lt;BR /&gt;1 A-2 1/1/2021&lt;BR /&gt;1 A-1 1/4/2021&lt;BR /&gt;1 A-1 1/20/2021&lt;BR /&gt;1 A-1 1/23/2021&lt;BR /&gt;1 A-1 1/25/2021&lt;BR /&gt;1 A-0 1/25/2021&lt;BR /&gt;2 A-1 1/1/2021&lt;BR /&gt;2 A-1 1/4/2021&lt;BR /&gt;3 A-1 1/1/2021&lt;BR /&gt;3 A-2 1/4/2021&lt;BR /&gt;4 A-2 1/1/2021&lt;BR /&gt;4 A-3 1/4/2021&lt;BR /&gt;4 A-1 1/5/2021&lt;BR /&gt;5 A-2 2/1/2021&lt;BR /&gt;6 0-B 07/22/2020 &lt;BR /&gt;6 1-B 08/05/2021 &lt;BR /&gt;6 1-B 08/11/2021 &lt;BR /&gt;6 1-B 08/18/2021 &lt;BR /&gt;6 1-B 01/26/2022 &lt;BR /&gt;;&lt;BR /&gt;data wanted;&lt;BR /&gt;input id type $ date :mmddyy10.;&lt;BR /&gt;format type $3. date mmddyy10.;&lt;BR /&gt;cards;&lt;BR /&gt;1 A-1 1/4/2021&lt;BR /&gt;1 A-0 1/25/2021&lt;BR /&gt;2 A-1 1/1/2021&lt;BR /&gt;3 A-1 1/1/2021&lt;BR /&gt;4 A-1 1/5/2021&lt;BR /&gt;5 A-2 2/1/2021&lt;BR /&gt;6 0-B 07/22/2020 &lt;BR /&gt;6 1-B 08/05/2021 &lt;BR /&gt;6 1-B 01/26/2022</description>
      <pubDate>Wed, 16 Feb 2022 15:26:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-observations-matches/m-p/796577#M255634</guid>
      <dc:creator>Emma2021</dc:creator>
      <dc:date>2022-02-16T15:26:07Z</dc:date>
    </item>
    <item>
      <title>Re: Keep observations matches</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-observations-matches/m-p/796742#M255684</link>
      <description>&lt;P&gt;Simply telling me "it does not work"&amp;nbsp; &amp;nbsp;...&amp;nbsp; &amp;nbsp;does not work.&amp;nbsp; Please explain what you got that was&amp;nbsp;unexpected or undesired/&amp;nbsp; And show the log of the failing program.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also earlier you said&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/381519"&gt;@Emma2021&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Thank you! I should sort by id type and date and I think this should work!&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Why are you sorting by id &lt;STRONG&gt;type&lt;/STRONG&gt; and date.&amp;nbsp; &amp;nbsp;You initial posting asked only for 10-day separation within ID, implying no role for &lt;STRONG&gt;type&lt;/STRONG&gt;, and therefore no apparent need for type to be a sort key.&amp;nbsp; &amp;nbsp; Are you now saying you want 10-day separation within each ID/TYPE?&amp;nbsp; &amp;nbsp;Is there some other role of the variable type that has not yet been explained (such as choosing between two types within an ID if they have tied dates)?&lt;/P&gt;</description>
      <pubDate>Wed, 16 Feb 2022 23:22:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-observations-matches/m-p/796742#M255684</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-02-16T23:22:06Z</dc:date>
    </item>
    <item>
      <title>Re: Keep observations matches</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-observations-matches/m-p/796754#M255686</link>
      <description>It runs but it does not remove the desired rows. &lt;BR /&gt;The rule is simple:&lt;BR /&gt;Sort by id and date, then remove all rows within 10 days: but before removing all rows within 10-days -check the type —keep only the one row with lowest type value.</description>
      <pubDate>Thu, 17 Feb 2022 00:29:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-observations-matches/m-p/796754#M255686</guid>
      <dc:creator>Emma2021</dc:creator>
      <dc:date>2022-02-17T00:29:43Z</dc:date>
    </item>
    <item>
      <title>Re: Keep observations matches</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-observations-matches/m-p/796775#M255702</link>
      <description>&lt;P&gt;Then the task is to read through a time span - i.e. all obs until there is a trailing 10day+ gap.&amp;nbsp; Over the course of that span determine what is the lowest type.&amp;nbsp; Then re-read the same obs, and output the first one that has type equal to lowest type.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_: );
  /* Read and count obs until a ten-day gap follows, determine lowest type */
  _lowest_type='   ';
  do _i=1 by 1 until (last.id=1 or date+10&amp;lt;_nxt_date);
    set have (keep=id);
    by id;
    merge have have (firstobs=2 keep=date rename=(date=_nxt_date));
    if _lowest_type=' ' then _lowest_type=type;
    else if type&amp;lt;_lowest_type then _lowest_type=type;  
  end;

  /* Reread obs and output first instance with lowest type */
  do _i=_i to 1 by -1;
    set have;
    if type=_lowest_type then do;
      output;
      call missing(_lowest_type); /*Prevent multiple hits */
    end;
  end;  
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note the data need to be in chronological order, (sort by ID/DATE).&amp;nbsp; You don't need to include TYPE in the sort keys.&lt;/P&gt;</description>
      <pubDate>Thu, 17 Feb 2022 02:12:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-observations-matches/m-p/796775#M255702</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-02-17T02:12:18Z</dc:date>
    </item>
    <item>
      <title>Re: Keep observations matches</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-observations-matches/m-p/796821#M255723</link>
      <description>&lt;P&gt;I'm still baffled. Do you want to calculate over a &lt;EM&gt;window&lt;/EM&gt; of 10 days, or keep calculating until there is a 10-day &lt;EM&gt;span&lt;/EM&gt; between dates?&lt;/P&gt;</description>
      <pubDate>Thu, 17 Feb 2022 09:34:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-observations-matches/m-p/796821#M255723</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-02-17T09:34:04Z</dc:date>
    </item>
    <item>
      <title>Re: Keep observations matches</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-observations-matches/m-p/796900#M255771</link>
      <description>This code does not do anything</description>
      <pubDate>Thu, 17 Feb 2022 15:33:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-observations-matches/m-p/796900#M255771</guid>
      <dc:creator>Emma2021</dc:creator>
      <dc:date>2022-02-17T15:33:22Z</dc:date>
    </item>
  </channel>
</rss>

