<?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: how to Pick dates on below Scenario in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-to-Pick-dates-on-below-Scenario/m-p/803566#M316412</link>
    <description>&lt;P&gt;You want 1 record per id, having the last DTE_END, and the latest DTE_START following a gap of over 90 days.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
input id : BEST8. DTE_START : DATE9. DTE_END : DATE9.;
FORMAT DTE_START DATE9. DTE_END DATE9.;
cards;
1 01JAN1961 01JAN1962
1 01MAR1962 01JAN1963
1 01FEB1963 31DEC9999
2 01JAN2001 01FEB2001
2 01MAR2001 31DEC9999
3 01JUL2011 30JUN2015
3 01NOV2015 31DEC9999
4 01MAY2016 01MAY2018
;
run;

data want (drop=_:);
  set test;
  by id;
  retain _strt;
  _strt=ifn(first.id=1 or dte_start-90&amp;gt;lag(dte_end),dte_start,_strt);
  if last.id;
  dte_start=_strt;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 23 Mar 2022 14:03:25 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2022-03-23T14:03:25Z</dc:date>
    <item>
      <title>how to Pick dates on below Scenario</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-Pick-dates-on-below-Scenario/m-p/803538#M316404</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;Please help -&lt;/P&gt;&lt;P&gt;In test data set I have id , dte_start and dte_end columns.&lt;/P&gt;&lt;P&gt;for ID 1-&amp;nbsp;&lt;/P&gt;&lt;P&gt;Customer Joined on&amp;nbsp;01JAN1961&amp;nbsp; and&amp;nbsp; left on&amp;nbsp;01JAN1962 and again joined on&amp;nbsp;01MAR1962. So here if the difference between dte_end (01JAN1962 )and latest&amp;nbsp; Dte_start (01MAR1962) is less than 90 days, so we keep&amp;nbsp;Dte_start as initial&amp;nbsp;Dte_start (01JAN1961). Dte_start only change if latest dte_start and previous Dte_end difference is more than 90 days.&lt;/P&gt;&lt;P&gt;DTE_end should be latest dte_end. For ID=1 DTE_end should be&amp;nbsp;31DEC9999.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;OUTPUT FOR ID=1 -&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;id DTE_START DTE_END&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;1 01JAN1961 31DEC9999&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data test;&lt;BR /&gt;input id : BEST8. DTE_START : DATE9. DTE_END : DATE9.;&lt;BR /&gt;FORMAT DTE_START DATE9. DTE_END DATE9.;&lt;BR /&gt;cards;&lt;BR /&gt;1 01JAN1961 01JAN1962&lt;BR /&gt;1 01MAR1962 01JAN1963&lt;BR /&gt;1 01FEB1963 31DEC9999&lt;BR /&gt;2 01JAN2001 01FEB2001&lt;BR /&gt;2 01MAR2001 31DEC9999&lt;BR /&gt;3 01JUL2011 30JUN2015&lt;BR /&gt;3 01NOV2015 31DEC9999&lt;BR /&gt;4 01MAY2016 01MAY2018&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Output I am looking for -&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;id DTE_START DTE_END&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;1 01JAN1961 31DEC9999&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;2 01JAN2001 31DEC9999&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;3 01NOV2015 31DEC9999&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;4 01MAY2016 01MAY2018&lt;/EM&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 23 Mar 2022 11:40:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-Pick-dates-on-below-Scenario/m-p/803538#M316404</guid>
      <dc:creator>nitink26</dc:creator>
      <dc:date>2022-03-23T11:40:05Z</dc:date>
    </item>
    <item>
      <title>Re: how to Pick dates on below Scenario</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-Pick-dates-on-below-Scenario/m-p/803545#M316408</link>
      <description>&lt;P&gt;Assuming I understood what you mean.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
input id : BEST8. DTE_START : DATE9. DTE_END : DATE9.;
FORMAT DTE_START DATE9. DTE_END DATE9.;
cards;
1 01JAN1961 01JAN1962
1 01MAR1962 01JAN1963
1 01FEB1963 31DEC9999
2 01JAN2001 01FEB2001
2 01MAR2001 31DEC9999
3 01JUL2011 30JUN2015
3 01NOV2015 31DEC9999
4 01MAY2016 01MAY2018
;
run;

data temp;
 set test;
 by id;
 diff=DTE_START-lag(DTE_END) ;
 if first.id then diff=.;
run;
data temp2;
 set temp;
 by id;
 if first.id or diff&amp;gt;90 then group+1;
run;
proc sql;
create table want as
select id,min(DTE_START) as DTE_START format=date9.,max(DTE_END) as DTE_END format=date9.
 from (select * from temp2 group by id  having group=max(group))
  group by id;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 23 Mar 2022 12:30:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-Pick-dates-on-below-Scenario/m-p/803545#M316408</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-03-23T12:30:40Z</dc:date>
    </item>
    <item>
      <title>Re: how to Pick dates on below Scenario</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-Pick-dates-on-below-Scenario/m-p/803550#M316409</link>
      <description>&lt;P&gt;According to data you provided I think id=3 should have 2 obs in result&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  x = '30JUN2015'd - '01NOV2015'd;
  put x=;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;A simple approach would be something like that:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
input id : BEST8. DTE_START : DATE9. DTE_END : DATE9.;
FORMAT DTE_START DATE9. DTE_END DATE9.;
cards;
1 01JAN1961 01JAN1962
1 01MAR1962 01JAN1963
1 01FEB1963 31DEC9999
2 01JAN2001 01FEB2001
2 01MAR2001 31DEC9999
3 01JUL2011 30JUN2015
3 01NOV2015 31DEC9999
4 01MAY2016 01MAY2018
;
run;

data want1;
  set test;
  if abs(DTE_START - lag(DTE_END)) &amp;gt;= 90  then grp + 1;
run;
proc print;
run;

data want2;
  set want1;
  by id grp;

  if first.grp then s = DTE_START;
  if last.grp then output;

  retain s;
  drop DTE_START;
  rename 
    s = DTE_START
  ;
  format s DATE9.;
run;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&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>Wed, 23 Mar 2022 12:40:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-Pick-dates-on-below-Scenario/m-p/803550#M316409</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2022-03-23T12:40:01Z</dc:date>
    </item>
    <item>
      <title>Re: how to Pick dates on below Scenario</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-Pick-dates-on-below-Scenario/m-p/803566#M316412</link>
      <description>&lt;P&gt;You want 1 record per id, having the last DTE_END, and the latest DTE_START following a gap of over 90 days.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
input id : BEST8. DTE_START : DATE9. DTE_END : DATE9.;
FORMAT DTE_START DATE9. DTE_END DATE9.;
cards;
1 01JAN1961 01JAN1962
1 01MAR1962 01JAN1963
1 01FEB1963 31DEC9999
2 01JAN2001 01FEB2001
2 01MAR2001 31DEC9999
3 01JUL2011 30JUN2015
3 01NOV2015 31DEC9999
4 01MAY2016 01MAY2018
;
run;

data want (drop=_:);
  set test;
  by id;
  retain _strt;
  _strt=ifn(first.id=1 or dte_start-90&amp;gt;lag(dte_end),dte_start,_strt);
  if last.id;
  dte_start=_strt;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 23 Mar 2022 14:03:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-Pick-dates-on-below-Scenario/m-p/803566#M316412</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-03-23T14:03:25Z</dc:date>
    </item>
    <item>
      <title>Re: how to Pick dates on below Scenario</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-Pick-dates-on-below-Scenario/m-p/803568#M316413</link>
      <description>&lt;P&gt;Thanks for the reply and you are right but If the difference is more than 90 days then dte_start I wanna pick should be the latest dte_start as I am looking only one record per ID.&lt;/P&gt;&lt;P&gt;so for ID =3 output will be&amp;nbsp; -&lt;/P&gt;&lt;P&gt;id DTE_START DTE_END&lt;/P&gt;&lt;P&gt;3 01NOV2015 31DEC9999&lt;/P&gt;&lt;P&gt;If you could let me know how to get that would be highly appreciated. Thanks a ton!&lt;/P&gt;</description>
      <pubDate>Wed, 23 Mar 2022 14:04:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-Pick-dates-on-below-Scenario/m-p/803568#M316413</guid>
      <dc:creator>nitink26</dc:creator>
      <dc:date>2022-03-23T14:04:17Z</dc:date>
    </item>
  </channel>
</rss>

