<?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: selecting consequent observations by date difference in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/selecting-consequent-observations-by-date-difference/m-p/471351#M120727</link>
    <description>&lt;P&gt;Thanks RW9&lt;/P&gt;
&lt;P&gt;the problem is that it is not always 6 rows per subject, sometimes it is 50...&lt;/P&gt;
&lt;P&gt;Thanks again&lt;/P&gt;</description>
    <pubDate>Tue, 19 Jun 2018 12:51:22 GMT</pubDate>
    <dc:creator>ammarhm</dc:creator>
    <dc:date>2018-06-19T12:51:22Z</dc:date>
    <item>
      <title>selecting consequent observations by date difference</title>
      <link>https://communities.sas.com/t5/SAS-Programming/selecting-consequent-observations-by-date-difference/m-p/471341#M120723</link>
      <description>&lt;P&gt;Hi everyone&lt;/P&gt;
&lt;P&gt;Having a mental block issue&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a dataset that looks like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Tabkle;
input nr subject date anydtdte23.;
format date date9.;

cards;
1	1	01Jan2013
2	1	31Jan2013
3	1	02Mar2013
4	1	01Apr2013
5	1	01May2013
6	2	31May2013
7	2	30Jun2013
8	2	30Jul2013
9	2	29Aug2013
10	2	28Sep2013
11	2	28Oct2013
12	3	27Nov2013
13	3	27Dec2013
14	3	26Jan2014
15	3	25Feb2014
16	3	27Mar2014
17	4	26Apr2014
18	4	26May2014
19	4	25Jun2014
20	4	25Jul2014
21	4	24Aug2014

;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;To make my question easier to understand i made the difference between the dates in the rows = 30 days&lt;/P&gt;
&lt;P&gt;What I want to do is to select observations (starting with the first from each group) where the date difference is &amp;gt; 40 days from the previous one.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So the code will fist take the first observation&lt;/P&gt;
&lt;P&gt;The second observation will be deleted because the date (obs2)-date(obs1) &amp;lt; 40&lt;/P&gt;
&lt;P&gt;Then the third observation will be selected because date (obs3) - date (obs1) &amp;gt; 40 : here obs2 is deleted by previous step so comparison occurs with obs1&lt;/P&gt;
&lt;P&gt;then obs 4 will be deleted because date(obs4)-date(obs3) &amp;lt;40&lt;/P&gt;
&lt;P&gt;then obs 5 selected because&amp;nbsp;&lt;SPAN&gt;ate (obs5) - date (obs3) &amp;gt; 40 : here obs4 is deleted by previous step so comparison&amp;nbsp;occurs&amp;nbsp;with obs3&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;This is for group 1&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;then it repeats for each&amp;nbsp;group and so forth&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I have tried the following, it didnt work because it still compares with the observation that should have been seleted:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data table2;
set table;
by subject;

if lag(date) ne . and  date - lag(date) &amp;lt;40 then delete;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Neither did the following work&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

proc sql;
create table table2 as

select distinct 
	L.*,
	R.date as next_date format=ddmmyy10.

from table as L inner join table as R
on
	L.nr=R.nr
and  intck('day', L.date, R.date) &amp;gt; 40
order by L.nr, L.date
	;
	quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Any help in solving this is appreciated&lt;/P&gt;</description>
      <pubDate>Tue, 19 Jun 2018 12:44:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/selecting-consequent-observations-by-date-difference/m-p/471341#M120723</guid>
      <dc:creator>ammarhm</dc:creator>
      <dc:date>2018-06-19T12:44:44Z</dc:date>
    </item>
    <item>
      <title>Re: selecting consequent observations by date difference</title>
      <link>https://communities.sas.com/t5/SAS-Programming/selecting-consequent-observations-by-date-difference/m-p/471347#M120724</link>
      <description>&lt;P&gt;Here is a quick bash at it, I have to go in a meeting now.&amp;nbsp; Very hardcoded at the mo:&lt;/P&gt;
&lt;PRE&gt;data have;
  input nr subject date yymmdd8.;
  format date date9.;
datalines;
1 1 20130101
2 1 20130131
3 1 20130302
4 1 20130401
5 1 20130501
6 2 20130531
7 2 20130630
8 2 20130730
9 2 20130829
10 2 20130928
11 2 20131028
;
run;

proc transpose data=have out=inter;
  by subject;
  var date;
run;
data inter;
  set inter;
  array col{6};
  if col{2}-col{1} &amp;lt; 40 then col{2}=.;
  if col{3}-coalesce(col{2},col{1}) &amp;lt; 40 then col{3}=.;
  if col{4}-coalesce(col{3},col{2},col{1}) &amp;lt; 40 then col{4}=.;
  if col{5}-coalesce(col{4},col{3},col{2},col{1}) &amp;lt; 40 then col{5}=.;
  if col{6}-coalesce(col{5},col{4},col{3},col{2},col{1}) &amp;lt; 40 then col{6}=.;
run;

proc transpose data=inter out=list;
  by subject;
  var col:;
run;
data list;
  set list;
  where date ne .;
run;

proc sql;
  create table want as
  select a.*
  from   have a
  right join list b
  on     a.subject=b.subject
  and    a.date=b.date;
quit;&lt;/PRE&gt;
&lt;P&gt;Basically transpose dates, do the check on array, then use that to keep only those values in the original data.&lt;/P&gt;
&lt;P&gt;Am a bit confused by your logic though, sometimes its &amp;gt; 40, sometimes less.&amp;nbsp; Perhaps give a clearer example, including the why.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Jun 2018 12:44:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/selecting-consequent-observations-by-date-difference/m-p/471347#M120724</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-06-19T12:44:29Z</dc:date>
    </item>
    <item>
      <title>Re: selecting consequent observations by date difference</title>
      <link>https://communities.sas.com/t5/SAS-Programming/selecting-consequent-observations-by-date-difference/m-p/471350#M120726</link>
      <description>&lt;P&gt;I haven't tested this, but something like this logic ought to work&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    retain startdate;
    set have;
    by group;
    if first.group then startdate=date;
    if date-startdate&amp;lt;40 and not first.group then delete;
    else startdate=date;
run;
    &lt;/CODE&gt;&amp;nbsp;&lt;/PRE&gt;</description>
      <pubDate>Tue, 19 Jun 2018 12:56:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/selecting-consequent-observations-by-date-difference/m-p/471350#M120726</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-06-19T12:56:19Z</dc:date>
    </item>
    <item>
      <title>Re: selecting consequent observations by date difference</title>
      <link>https://communities.sas.com/t5/SAS-Programming/selecting-consequent-observations-by-date-difference/m-p/471351#M120727</link>
      <description>&lt;P&gt;Thanks RW9&lt;/P&gt;
&lt;P&gt;the problem is that it is not always 6 rows per subject, sometimes it is 50...&lt;/P&gt;
&lt;P&gt;Thanks again&lt;/P&gt;</description>
      <pubDate>Tue, 19 Jun 2018 12:51:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/selecting-consequent-observations-by-date-difference/m-p/471351#M120727</guid>
      <dc:creator>ammarhm</dc:creator>
      <dc:date>2018-06-19T12:51:22Z</dc:date>
    </item>
    <item>
      <title>Re: selecting consequent observations by date difference</title>
      <link>https://communities.sas.com/t5/SAS-Programming/selecting-consequent-observations-by-date-difference/m-p/471352#M120728</link>
      <description>&lt;P&gt;Use a retained variable:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input nr subject date :yymmdd10.;
format date yymmddd10.;
datalines;
1 1 20130101
2 1 20130131
3 1 20130302
4 1 20130401
5 1 20130501
6 2 20130531
7 2 20130630
8 2 20130730
9 2 20130829
10 2 20130928
11 2 20131028
12 3 20131127
13 3 20131227
14 3 20140126
15 3 20140225
16 3 20140327
17 4 20140426
18 4 20140526
19 4 20140625
20 4 20140725
21 4 20140824
;
run;

data want;
set have;
by subject;
retain comp_date;
if first.subject then comp_date = date;
else do;
  if date - comp_date &amp;gt; 40
  then comp_date = date;
  else delete;
end;
drop comp_date;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 19 Jun 2018 12:51:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/selecting-consequent-observations-by-date-difference/m-p/471352#M120728</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-06-19T12:51:32Z</dc:date>
    </item>
    <item>
      <title>Re: selecting consequent observations by date difference</title>
      <link>https://communities.sas.com/t5/SAS-Programming/selecting-consequent-observations-by-date-difference/m-p/471355#M120730</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/24798"&gt;@ammarhm&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks RW9&lt;/P&gt;
&lt;P&gt;the problem is that it is not always 6 rows per subject, sometimes it is 50...&lt;/P&gt;
&lt;P&gt;Thanks again&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Naturally, we can only write code that works on the example you gave us ...&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Jun 2018 12:55:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/selecting-consequent-observations-by-date-difference/m-p/471355#M120730</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-06-19T12:55:08Z</dc:date>
    </item>
  </channel>
</rss>

