<?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 the record with nearest date in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Keep-the-record-with-nearest-date/m-p/724028#M224762</link>
    <description>&lt;P&gt;In the following code observations with closed date are ranked based on the closeness.&lt;BR /&gt;The closest date will have rank 1, the next will have 2,&amp;nbsp;&lt;BR /&gt;You can filter based on how many records you want..&lt;/P&gt;&lt;P&gt;I have named the ranking variable as time_rank. You can call anything else.&lt;BR /&gt;For each customer there will be a different rank.&lt;BR /&gt;Please let me know if you have questions.&lt;/P&gt;&lt;P&gt;Here is the code:&lt;/P&gt;&lt;P&gt;/*---------------------------------------------*/&lt;/P&gt;&lt;P&gt;data test;&lt;BR /&gt;Retain customer_id order_date delivery_date;&lt;BR /&gt;format order_date delivery_date date9.;&lt;BR /&gt;informat order_date delivery_date date9.;&lt;BR /&gt;input customer_id order_date delivery_date;&lt;BR /&gt;date_diff=delivery_Date -order_date;&lt;BR /&gt;datalines;&lt;/P&gt;&lt;P&gt;1 01JAN2021 01JAN2021&lt;/P&gt;&lt;P&gt;1 01JAN2021 10JAN2021&lt;/P&gt;&lt;P&gt;1 02JAN2021 10JAN2021&lt;/P&gt;&lt;P&gt;1 02JAN2021 11JAN2021&lt;/P&gt;&lt;P&gt;2 02JAN2021 10JAN2021&lt;/P&gt;&lt;P&gt;2 02JAN2021 20JAN2021&lt;BR /&gt;;&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;proc sort data=test;&lt;BR /&gt;by customer_id order_date delivery_date;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;proc rank data=test out=test2 (drop=date_diff) ties=low;&lt;BR /&gt;by customer_id;&lt;BR /&gt;var date_diff;&lt;BR /&gt;ranks time_rank;&lt;BR /&gt;run;&lt;BR /&gt;proc sort data=test2;&lt;BR /&gt;by customer_id time_rank;&lt;BR /&gt;run;&lt;BR /&gt;The output will be like this:&lt;BR /&gt;&lt;BR /&gt;customer_id order_date delivery_date time_rank&lt;BR /&gt;1 01JAN2021 01JAN2021 1&lt;BR /&gt;1 02JAN2021 10JAN2021 2&lt;BR /&gt;1 01JAN2021 10JAN2021 3&lt;BR /&gt;1 02JAN2021 11JAN2021 3&lt;BR /&gt;2 02JAN2021 10JAN2021 1&lt;BR /&gt;2 02JAN2021 20JAN2021 2&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 05 Mar 2021 20:32:21 GMT</pubDate>
    <dc:creator>Sajid01</dc:creator>
    <dc:date>2021-03-05T20:32:21Z</dc:date>
    <item>
      <title>Keep the record with nearest date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-the-record-with-nearest-date/m-p/723973#M224742</link>
      <description>&lt;P&gt;Hello all. I have the below table and would like to keep the record with delivery date that is nearest to the order date for every combination of customer and order date. In this case, I would like to keep record 1, 3 and 5. I'm not sure how to write the code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you in advance!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;customer_id&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;order_date&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;delivery_date&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;01JAN2021&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 01JAN2021&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;01JAN2021&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 10JAN2021&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;02JAN2021&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 10JAN2021&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;02JAN2021&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 11JAN2021&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;02JAN2021&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;10JAN2021&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;02JAN2021&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 20JAN2021&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Mar 2021 19:21:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-the-record-with-nearest-date/m-p/723973#M224742</guid>
      <dc:creator>di_niu0</dc:creator>
      <dc:date>2021-03-05T19:21:57Z</dc:date>
    </item>
    <item>
      <title>Re: Keep the record with nearest date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-the-record-with-nearest-date/m-p/724007#M224752</link>
      <description>&lt;P&gt;This can be done using &lt;A href="https://go.documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=lepg&amp;amp;docsetTarget=p181g1p4bw3phkn1vt5p67xvynd5.htm&amp;amp;locale=en" target="_self"&gt;FIRST. and LAST. Data step variables&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
/* Sample data */

data got ;
	infile cards ;
	input
		customer_id $
		order_date : date9.
		delivery   : date9. ;
	format 
		order_date date9. 
		delivery   date9. 
		;
	cards ;
1                         01JAN2021        01JAN2021 
1                         01JAN2021        10JAN2021 
1                         02JAN2021        10JAN2021 
1                         02JAN2021        11JAN2021 
2                         02JAN2021        10JAN2021
2                         02JAN2021        20JAN2021
;

/* Sort the data */
proc sort 
	data=got
	out=srtdGot ;
	by 
		customer_id order_date delivery ;
run ;


data want ;
	set srtdGot ;
	by 
		customer_id order_date delivery ;
	/* If it's the first occurance of order_date then you are going to want this */
	if first.order_date then
		output want ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 05 Mar 2021 20:05:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-the-record-with-nearest-date/m-p/724007#M224752</guid>
      <dc:creator>AMSAS</dc:creator>
      <dc:date>2021-03-05T20:05:39Z</dc:date>
    </item>
    <item>
      <title>Re: Keep the record with nearest date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-the-record-with-nearest-date/m-p/724014#M224754</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;this may help:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input customer_id (order_date delivery_date) (:date9.);
format order_date delivery_date date9.; 
OBS = _N_;
cards;
1 01JAN2021 01JAN2021
1 01JAN2021 10JAN2021
1 02JAN2021 10JAN2021
1 02JAN2021 11JAN2021
2 02JAN2021 10JAN2021
2 02JAN2021 20JAN2021
;
run;
proc print;
run;

data want;
  _min_ = constant("big");
  do _N_ = 1 by 1 until(last.order_date);
    set have;
    by customer_id order_date;

    _x_ = abs(order_date-delivery_date);

    if _min_ &amp;gt; _x_ &amp;gt; . then
      do;
        _min_ = _x_;
        _O_ = _N_;
      end;
  end;


  do _N_ = 1 to _N_;
    set have;
    if _N_ = _O_ then output;
  end;

  drop _:;
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;</description>
      <pubDate>Fri, 05 Mar 2021 20:28:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-the-record-with-nearest-date/m-p/724014#M224754</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2021-03-05T20:28:03Z</dc:date>
    </item>
    <item>
      <title>Re: Keep the record with nearest date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-the-record-with-nearest-date/m-p/724019#M224756</link>
      <description>&lt;P&gt;This "mean" example doesn't work:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input customer_id (order_date delivery) (:date9.);
format order_date delivery date9.; 
OBS = _N_;
cards;
1 01JAN2021 01JAN2021
1 01JAN2021 10JAN2021
1 01JAN2021 .
1 02JAN2021 10JAN2021
1 02JAN2021 11JAN2021
2 02JAN2021 10JAN2021
2 02JAN2021 20JAN2021
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Fri, 05 Mar 2021 20:19:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-the-record-with-nearest-date/m-p/724019#M224756</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2021-03-05T20:19:00Z</dc:date>
    </item>
    <item>
      <title>Re: Keep the record with nearest date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-the-record-with-nearest-date/m-p/724028#M224762</link>
      <description>&lt;P&gt;In the following code observations with closed date are ranked based on the closeness.&lt;BR /&gt;The closest date will have rank 1, the next will have 2,&amp;nbsp;&lt;BR /&gt;You can filter based on how many records you want..&lt;/P&gt;&lt;P&gt;I have named the ranking variable as time_rank. You can call anything else.&lt;BR /&gt;For each customer there will be a different rank.&lt;BR /&gt;Please let me know if you have questions.&lt;/P&gt;&lt;P&gt;Here is the code:&lt;/P&gt;&lt;P&gt;/*---------------------------------------------*/&lt;/P&gt;&lt;P&gt;data test;&lt;BR /&gt;Retain customer_id order_date delivery_date;&lt;BR /&gt;format order_date delivery_date date9.;&lt;BR /&gt;informat order_date delivery_date date9.;&lt;BR /&gt;input customer_id order_date delivery_date;&lt;BR /&gt;date_diff=delivery_Date -order_date;&lt;BR /&gt;datalines;&lt;/P&gt;&lt;P&gt;1 01JAN2021 01JAN2021&lt;/P&gt;&lt;P&gt;1 01JAN2021 10JAN2021&lt;/P&gt;&lt;P&gt;1 02JAN2021 10JAN2021&lt;/P&gt;&lt;P&gt;1 02JAN2021 11JAN2021&lt;/P&gt;&lt;P&gt;2 02JAN2021 10JAN2021&lt;/P&gt;&lt;P&gt;2 02JAN2021 20JAN2021&lt;BR /&gt;;&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;proc sort data=test;&lt;BR /&gt;by customer_id order_date delivery_date;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;proc rank data=test out=test2 (drop=date_diff) ties=low;&lt;BR /&gt;by customer_id;&lt;BR /&gt;var date_diff;&lt;BR /&gt;ranks time_rank;&lt;BR /&gt;run;&lt;BR /&gt;proc sort data=test2;&lt;BR /&gt;by customer_id time_rank;&lt;BR /&gt;run;&lt;BR /&gt;The output will be like this:&lt;BR /&gt;&lt;BR /&gt;customer_id order_date delivery_date time_rank&lt;BR /&gt;1 01JAN2021 01JAN2021 1&lt;BR /&gt;1 02JAN2021 10JAN2021 2&lt;BR /&gt;1 01JAN2021 10JAN2021 3&lt;BR /&gt;1 02JAN2021 11JAN2021 3&lt;BR /&gt;2 02JAN2021 10JAN2021 1&lt;BR /&gt;2 02JAN2021 20JAN2021 2&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Mar 2021 20:32:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-the-record-with-nearest-date/m-p/724028#M224762</guid>
      <dc:creator>Sajid01</dc:creator>
      <dc:date>2021-03-05T20:32:21Z</dc:date>
    </item>
    <item>
      <title>Re: Keep the record with nearest date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-the-record-with-nearest-date/m-p/724955#M225103</link>
      <description>Thank you!</description>
      <pubDate>Tue, 09 Mar 2021 19:14:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-the-record-with-nearest-date/m-p/724955#M225103</guid>
      <dc:creator>di_niu0</dc:creator>
      <dc:date>2021-03-09T19:14:39Z</dc:date>
    </item>
    <item>
      <title>Re: Keep the record with nearest date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-the-record-with-nearest-date/m-p/724956#M225104</link>
      <description>Thank you!</description>
      <pubDate>Tue, 09 Mar 2021 19:15:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-the-record-with-nearest-date/m-p/724956#M225104</guid>
      <dc:creator>di_niu0</dc:creator>
      <dc:date>2021-03-09T19:15:11Z</dc:date>
    </item>
  </channel>
</rss>

