<?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: defining before and after dates using changing dates in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/defining-before-and-after-dates-using-changing-dates/m-p/768536#M243776</link>
    <description>&lt;P&gt;Sure -&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have the dataset 'have' and from it I want create an output which lists the&amp;nbsp;MTHLY_PAY value before and after the PT_DT by account id&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;for instance for account id 12345 I would require the following output&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ACCOUNT_ID&lt;/TD&gt;&lt;TD&gt;PT_DT&lt;/TD&gt;&lt;TD&gt;MTHLY_PAY_BEFORE_PT&lt;/TD&gt;&lt;TD&gt;MTHLY_PAY_AFTER_PT&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;12345&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;01FEB2017&lt;/TD&gt;&lt;TD&gt;480.64&lt;/TD&gt;&lt;TD&gt;512.34&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;the value of 480.64 was the last&amp;nbsp;MTHLY_PAY value before the&amp;nbsp;PT_DT of 01FEB2017 and 512.34 was the first value after 01FEB2017&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;hope this explanation helps&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Adnan&lt;/P&gt;</description>
    <pubDate>Mon, 20 Sep 2021 12:18:00 GMT</pubDate>
    <dc:creator>Adnan2</dc:creator>
    <dc:date>2021-09-20T12:18:00Z</dc:date>
    <item>
      <title>defining before and after dates using changing dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/defining-before-and-after-dates-using-changing-dates/m-p/768531#M243773</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have the following dataset&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;

input account_id PT_DT MTHLY_PAY MTHLY_PAY_DT_CHANGE ;

cards;
12345 01FEB2017 456.98 25JUL2016
12345 01FEB2017 480.64 12SEP2016
12345 01FEB2017 512.34 12FEB2017
12345 01FEB2017 476.96 23MAY2018
12346 01APR2020 984.76 15JUN2019
12346 01APR2020 887.91 03APR2020
12436 01APR2020 634.33 04MAR2021
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;and require the following output&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ACCOUNT_ID&lt;/TD&gt;&lt;TD&gt;PT_DT&lt;/TD&gt;&lt;TD&gt;MTHLY_PAY_BEFORE_PT&lt;/TD&gt;&lt;TD&gt;MTHLY_PAY_AFTER_PT&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;12345&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;01FEB2017&lt;/TD&gt;&lt;TD&gt;480.64&lt;/TD&gt;&lt;TD&gt;512.34&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;12346&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;01APR2020&lt;/TD&gt;&lt;TD&gt;984.76&lt;/TD&gt;&lt;TD&gt;887.91&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;any recommendations will be well appreciated !&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Sep 2021 11:57:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/defining-before-and-after-dates-using-changing-dates/m-p/768531#M243773</guid>
      <dc:creator>Adnan2</dc:creator>
      <dc:date>2021-09-20T11:57:03Z</dc:date>
    </item>
    <item>
      <title>Re: defining before and after dates using changing dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/defining-before-and-after-dates-using-changing-dates/m-p/768534#M243775</link>
      <description>Can you try explaining what you want?</description>
      <pubDate>Mon, 20 Sep 2021 12:11:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/defining-before-and-after-dates-using-changing-dates/m-p/768534#M243775</guid>
      <dc:creator>tarheel13</dc:creator>
      <dc:date>2021-09-20T12:11:11Z</dc:date>
    </item>
    <item>
      <title>Re: defining before and after dates using changing dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/defining-before-and-after-dates-using-changing-dates/m-p/768536#M243776</link>
      <description>&lt;P&gt;Sure -&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have the dataset 'have' and from it I want create an output which lists the&amp;nbsp;MTHLY_PAY value before and after the PT_DT by account id&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;for instance for account id 12345 I would require the following output&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ACCOUNT_ID&lt;/TD&gt;&lt;TD&gt;PT_DT&lt;/TD&gt;&lt;TD&gt;MTHLY_PAY_BEFORE_PT&lt;/TD&gt;&lt;TD&gt;MTHLY_PAY_AFTER_PT&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;12345&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;01FEB2017&lt;/TD&gt;&lt;TD&gt;480.64&lt;/TD&gt;&lt;TD&gt;512.34&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;the value of 480.64 was the last&amp;nbsp;MTHLY_PAY value before the&amp;nbsp;PT_DT of 01FEB2017 and 512.34 was the first value after 01FEB2017&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;hope this explanation helps&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Adnan&lt;/P&gt;</description>
      <pubDate>Mon, 20 Sep 2021 12:18:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/defining-before-and-after-dates-using-changing-dates/m-p/768536#M243776</guid>
      <dc:creator>Adnan2</dc:creator>
      <dc:date>2021-09-20T12:18:00Z</dc:date>
    </item>
    <item>
      <title>Re: defining before and after dates using changing dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/defining-before-and-after-dates-using-changing-dates/m-p/768540#M243777</link>
      <description>&lt;P&gt;The data step you provided is not inputting correctly.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Sep 2021 12:28:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/defining-before-and-after-dates-using-changing-dates/m-p/768540#M243777</guid>
      <dc:creator>tarheel13</dc:creator>
      <dc:date>2021-09-20T12:28:00Z</dc:date>
    </item>
    <item>
      <title>Re: defining before and after dates using changing dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/defining-before-and-after-dates-using-changing-dates/m-p/768542#M243778</link>
      <description>&lt;P&gt;apologies&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;

input account_id PT_DT date9. MTHLY_PAY MTHLY_PAY_DT_CHANGE date9.;
format PT_DT MTHLY_PAY_DT_CHANGE date9.;

cards;
12345 01FEB2017 456.98 25JUL2016
12345 01FEB2017 480.64 12SEP2016
12345 01FEB2017 512.34 12FEB2017
12345 01FEB2017 476.96 23MAY2018
12346 01APR2020 984.76 15JUN2019
12346 01APR2020 887.91 03APR2020
12436 01APR2020 634.33 04MAR2021
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Adnan&lt;/P&gt;</description>
      <pubDate>Mon, 20 Sep 2021 12:32:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/defining-before-and-after-dates-using-changing-dates/m-p/768542#M243778</guid>
      <dc:creator>Adnan2</dc:creator>
      <dc:date>2021-09-20T12:32:49Z</dc:date>
    </item>
    <item>
      <title>Re: defining before and after dates using changing dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/defining-before-and-after-dates-using-changing-dates/m-p/768544#M243779</link>
      <description>&lt;P&gt;Is the last row a typo? Is it 12436 or 12346 in the last row?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Sep 2021 12:36:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/defining-before-and-after-dates-using-changing-dates/m-p/768544#M243779</guid>
      <dc:creator>tarheel13</dc:creator>
      <dc:date>2021-09-20T12:36:07Z</dc:date>
    </item>
    <item>
      <title>Re: defining before and after dates using changing dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/defining-before-and-after-dates-using-changing-dates/m-p/768548#M243780</link>
      <description>&lt;P&gt;This gets what you want.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input account_id PT_DT date9. MTHLY_PAY MTHLY_PAY_DT_CHANGE date9.;
format pt_dt mthly_pay_dt_change date9.;
cards;
12345 01FEB2017 456.98 25JUL2016
12345 01FEB2017 480.64 12SEP2016
12345 01FEB2017 512.34 12FEB2017
12345 01FEB2017 476.96 23MAY2018
12346 01APR2020 984.76 15JUN2019
12346 01APR2020 887.91 03APR2020
12436 01APR2020 634.33 04MAR2021
;
run;

data have2;
	set have;
	days_diff=abs(mthly_pay_dt_change - pt_dt);
run;

proc sql;
	create table before as 
	select account_id, pt_dt, mthly_pay as mthly_pay_before_pt
		from have2
		where mthly_pay_dt_change &amp;lt; pt_dt
		group by account_id, pt_dt
		having days_diff=min(days_diff);
		
	create table after as select account_id, pt_dt, mthly_pay as mthly_pay_after_pt
		from have2
		where mthly_pay_dt_change &amp;gt; pt_dt
		group by account_id, pt_dt
		having days_diff=max(days_diff);
		
	create table want as 
	select b.*, a.mthly_pay_after_pt
		from before as b
		join
		after as a
		on b.account_id=a.account_id;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;FONT size="6"&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="lrackley_0-1632141734918.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/63730i044F1E04C5475F1A/image-size/medium?v=v2&amp;amp;px=400" role="button" title="lrackley_0-1632141734918.png" alt="lrackley_0-1632141734918.png" /&gt;&lt;/span&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Sep 2021 12:42:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/defining-before-and-after-dates-using-changing-dates/m-p/768548#M243780</guid>
      <dc:creator>tarheel13</dc:creator>
      <dc:date>2021-09-20T12:42:40Z</dc:date>
    </item>
    <item>
      <title>Re: defining before and after dates using changing dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/defining-before-and-after-dates-using-changing-dates/m-p/768549#M243781</link>
      <description>&lt;P&gt;Please try&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input account_id PT_DT:date9. MTHLY_PAY MTHLY_PAY_DT_CHANGE:date9.;
format PT_DT MTHLY_PAY_DT_CHANGE date9.;
cards;
12345 01FEB2017 456.98 25JUL2016
12345 01FEB2017 480.64 12SEP2016
12345 01FEB2017 512.34 12FEB2017
12345 01FEB2017 476.96 23MAY2018
12346 01APR2020 984.76 15JUN2019
12346 01APR2020 887.91 03APR2020
12346 01APR2020 634.33 04MAR2021
;
run;

 

data want;
set have;
by account_id;
MTHLY_PAY_BEFORE_PT=lag(MTHLY_PAY);
if first.account_id then MTHLY_PAY_BEFORE_PT=.;
if PT_DT&amp;lt;=MTHLY_PAY_DT_CHANGE then flag=1;
MTHLY_PAY_AFTER_PT=MTHLY_PAY;
keep account_id PT_DT MTHLY_PAY_BEFORE_PT MTHLY_PAY_AFTER_PT flag;
run;

proc sort data=want;
by account_id;
where flag=1;
run;

data want;
set want;
by account_id;
if first.account_id;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 20 Sep 2021 12:43:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/defining-before-and-after-dates-using-changing-dates/m-p/768549#M243781</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2021-09-20T12:43:44Z</dc:date>
    </item>
    <item>
      <title>Re: defining before and after dates using changing dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/defining-before-and-after-dates-using-changing-dates/m-p/768749#M243863</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input account_id PT_DT:date9. MTHLY_PAY MTHLY_PAY_DT_CHANGE:date9.;
format PT_DT MTHLY_PAY_DT_CHANGE date9.;
cards;
12345 01FEB2017 456.98 25JUL2016
12345 01FEB2017 480.64 12SEP2016
12345 01FEB2017 512.34 12FEB2017
12345 01FEB2017 476.96 23MAY2018
12346 01APR2020 984.76 15JUN2019
12346 01APR2020 887.91 03APR2020
12346 01APR2020 634.33 04MAR2021
;
run;
data want;
 if _n_=1 then do;
   if 0 then set have;
   declare hash h();
   h.definekey('MTHLY_PAY_DT_CHANGE');
   h.definedata('MTHLY_PAY');
   h.definedone();
 end;

do until(last.PT_DT );
 set have;
 by  account_id PT_DT ;
 h.add();
 min=min(min,MTHLY_PAY_DT_CHANGE);
 max=max(max,MTHLY_PAY_DT_CHANGE);
end;

do temp=PT_DT to min by -1;
  call missing(MTHLY_PAY);
  if h.find(key:temp)=0 then do;MTHLY_PAY_BEFORE_PT=MTHLY_PAY;leave;end;
end;
do temp=PT_DT to max;
  call missing(MTHLY_PAY);
  if h.find(key:temp)=0 then do;MTHLY_PAY_AFTER_PT=MTHLY_PAY;leave;end;
end;

h.clear();
drop min max temp MTHLY_PAY_DT_CHANGE MTHLY_PAY;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 21 Sep 2021 11:50:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/defining-before-and-after-dates-using-changing-dates/m-p/768749#M243863</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-09-21T11:50:27Z</dc:date>
    </item>
  </channel>
</rss>

