<?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: Arrange date values closest to another date in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Arrange-date-values-closest-to-another-date/m-p/904157#M357226</link>
    <description>Should L be sorted in any manner? It doesn't appear to be consistently sorted.</description>
    <pubDate>Tue, 21 Nov 2023 22:26:01 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2023-11-21T22:26:01Z</dc:date>
    <item>
      <title>Arrange date values closest to another date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrange-date-values-closest-to-another-date/m-p/904154#M357225</link>
      <description>&lt;P&gt;The current data structure for the problem I have is:&lt;/P&gt;
&lt;TABLE width="345"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="64"&gt;id&lt;/TD&gt;
&lt;TD width="75"&gt;L_date&lt;/TD&gt;
&lt;TD width="75"&gt;M_date&lt;/TD&gt;
&lt;TD width="131"&gt;Note&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;9/1/2020&lt;/TD&gt;
&lt;TD&gt;9/29/2020&lt;/TD&gt;
&lt;TD&gt;keep, as it is closest&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;8/30/2020&lt;/TD&gt;
&lt;TD&gt;9/29/2020&lt;/TD&gt;
&lt;TD&gt;change to null&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;8/13/2022&lt;/TD&gt;
&lt;TD&gt;10/21/2022&lt;/TD&gt;
&lt;TD&gt;change to null&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;10/1/2022&lt;/TD&gt;
&lt;TD&gt;10/21/2022&lt;/TD&gt;
&lt;TD&gt;keep, as it is closest&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;10/2/2023&lt;/TD&gt;
&lt;TD&gt;10/21/2022&lt;/TD&gt;
&lt;TD&gt;change to null&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;TD&gt;12/11/2021&lt;/TD&gt;
&lt;TD&gt;.&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;TD&gt;5/19/2022&lt;/TD&gt;
&lt;TD&gt;.&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;4&lt;/TD&gt;
&lt;TD&gt;2/5/2022&lt;/TD&gt;
&lt;TD&gt;12/11/2021&lt;/TD&gt;
&lt;TD&gt;keep, as it is closest&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;4&lt;/TD&gt;
&lt;TD&gt;3/19/2022&lt;/TD&gt;
&lt;TD&gt;12/11/2021&lt;/TD&gt;
&lt;TD&gt;change to null&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;4&lt;/TD&gt;
&lt;TD&gt;6/21/2022&lt;/TD&gt;
&lt;TD&gt;12/11/2021&lt;/TD&gt;
&lt;TD&gt;change to null&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am trying to retain the closest M_date column value that is to the L_date value and turn the other same M_date values for the same ID to null.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The data structure that I am trying to achieve is below:&lt;/P&gt;
&lt;TABLE width="214"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="64"&gt;id&lt;/TD&gt;
&lt;TD width="75"&gt;L_date&lt;/TD&gt;
&lt;TD width="75"&gt;M_date&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;9/1/2020&lt;/TD&gt;
&lt;TD&gt;9/29/2020&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;8/30/2020&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;8/13/2022&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;10/1/2022&lt;/TD&gt;
&lt;TD&gt;10/21/2022&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;10/2/2023&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;TD&gt;12/11/2021&lt;/TD&gt;
&lt;TD&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;TD&gt;5/19/2022&lt;/TD&gt;
&lt;TD&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;4&lt;/TD&gt;
&lt;TD&gt;2/5/2022&lt;/TD&gt;
&lt;TD&gt;12/11/2021&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;4&lt;/TD&gt;
&lt;TD&gt;3/19/2022&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;4&lt;/TD&gt;
&lt;TD&gt;6/21/2022&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Tue, 21 Nov 2023 21:51:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrange-date-values-closest-to-another-date/m-p/904154#M357225</guid>
      <dc:creator>SP01</dc:creator>
      <dc:date>2023-11-21T21:51:52Z</dc:date>
    </item>
    <item>
      <title>Re: Arrange date values closest to another date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrange-date-values-closest-to-another-date/m-p/904157#M357226</link>
      <description>Should L be sorted in any manner? It doesn't appear to be consistently sorted.</description>
      <pubDate>Tue, 21 Nov 2023 22:26:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrange-date-values-closest-to-another-date/m-p/904157#M357226</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-11-21T22:26:01Z</dc:date>
    </item>
    <item>
      <title>Re: Arrange date values closest to another date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrange-date-values-closest-to-another-date/m-p/904158#M357227</link>
      <description>&lt;P&gt;It can be sorted from oldest dates first to newest dates last.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Nov 2023 22:27:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrange-date-values-closest-to-another-date/m-p/904158#M357227</guid>
      <dc:creator>SP01</dc:creator>
      <dc:date>2023-11-21T22:27:47Z</dc:date>
    </item>
    <item>
      <title>Re: Arrange date values closest to another date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrange-date-values-closest-to-another-date/m-p/904159#M357228</link>
      <description>&lt;OL&gt;
&lt;LI&gt;Subtract L_date from M_date; then take the absolute value.&lt;/LI&gt;
&lt;LI&gt;Run PROC RANK on the absolute value of the difference, computed in step 1, use a BY ID; statement&lt;/LI&gt;
&lt;LI&gt;If the ranked value is 1 then keep M_date; other wise set M_date to null&lt;/LI&gt;
&lt;/OL&gt;</description>
      <pubDate>Tue, 21 Nov 2023 22:31:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrange-date-values-closest-to-another-date/m-p/904159#M357228</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-11-21T22:31:39Z</dc:date>
    </item>
    <item>
      <title>Re: Arrange date values closest to another date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrange-date-values-closest-to-another-date/m-p/904160#M357229</link>
      <description>And how to deal with ties?</description>
      <pubDate>Tue, 21 Nov 2023 22:33:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrange-date-values-closest-to-another-date/m-p/904160#M357229</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-11-21T22:33:13Z</dc:date>
    </item>
    <item>
      <title>Re: Arrange date values closest to another date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrange-date-values-closest-to-another-date/m-p/904161#M357230</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;And how to deal with ties?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Don't ask me, that's up to the OP &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/432452"&gt;@SP01&lt;/a&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Nov 2023 22:34:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrange-date-values-closest-to-another-date/m-p/904161#M357230</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-11-21T22:34:21Z</dc:date>
    </item>
    <item>
      <title>Re: Arrange date values closest to another date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrange-date-values-closest-to-another-date/m-p/904162#M357231</link>
      <description>&lt;P&gt;Ties can be left&amp;nbsp; as same.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Nov 2023 22:35:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrange-date-values-closest-to-another-date/m-p/904162#M357231</guid>
      <dc:creator>SP01</dc:creator>
      <dc:date>2023-11-21T22:35:26Z</dc:date>
    </item>
    <item>
      <title>Re: Arrange date values closest to another date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrange-date-values-closest-to-another-date/m-p/904165#M357233</link>
      <description>&lt;P&gt;Here's a solution, I expanded your sample data to include two different scenarios - one where there is a tie and one where there are missing M_Date and non missing M_date for the same ID.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards truncover;
input id 1. L_date : mmddyy10.  M_date : mmddyy10.;
format l_date m_date yymmdd10.;;
cards;
1   9/1/2020    9/29/2020   
1   8/30/2020   9/27/2020
2   8/13/2022   10/21/2022
2   10/1/2022   10/21/2022
2   10/2/2023   10/21/2022
3   12/11/2021  .    
3   5/19/2022   .    
4   2/5/2022    12/11/2021
4   3/19/2022   12/11/2021  
4   6/21/2022   12/11/2021
5   2/5/2022    12/11/2021
5   2/5/2022    12/14/2021
5   3/19/2022   .
5   6/21/2022   .
;;;
run;

data step1;
set have;
abs_diff = abs(m_date-l_date);
run;

proc sort data=step1;
by id abs_diff;
run;

data want;
set step1;
by id;
retain flag;
if first.id then flag=0;

if first.id and not missing(abs_diff) then flag=1;
else if flag=0 and not missing(abs_diff) then flag=1;
else if flag=1 then call missing(m_date);

run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 21 Nov 2023 23:03:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrange-date-values-closest-to-another-date/m-p/904165#M357233</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-11-21T23:03:30Z</dc:date>
    </item>
    <item>
      <title>Re: Arrange date values closest to another date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrange-date-values-closest-to-another-date/m-p/904174#M357237</link>
      <description>Anybody want to try  their hand at a one-step double DOW solution?</description>
      <pubDate>Wed, 22 Nov 2023 01:51:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrange-date-values-closest-to-another-date/m-p/904174#M357237</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2023-11-22T01:51:27Z</dc:date>
    </item>
    <item>
      <title>Re: Arrange date values closest to another date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrange-date-values-closest-to-another-date/m-p/904175#M357238</link>
      <description>&lt;P&gt;Yes, what is DOW?&lt;/P&gt;</description>
      <pubDate>Wed, 22 Nov 2023 01:53:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrange-date-values-closest-to-another-date/m-p/904175#M357238</guid>
      <dc:creator>SP01</dc:creator>
      <dc:date>2023-11-22T01:53:21Z</dc:date>
    </item>
    <item>
      <title>Re: Arrange date values closest to another date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrange-date-values-closest-to-another-date/m-p/904179#M357240</link>
      <description>DOW is a DO loop that runs through groups of observations.  It was popularized by Ian Whitlock (although he didn't  name it so it is not clear whether the W stands for Whitlock).  The basic form for this problem would be:&lt;BR /&gt;&lt;BR /&gt;data want;&lt;BR /&gt;do until (last.id);&lt;BR /&gt;  set have;&lt;BR /&gt;  *code to find min diff for this id;&lt;BR /&gt;end;&lt;BR /&gt;do until (last.id);&lt;BR /&gt;   set have;&lt;BR /&gt;   by id;&lt;BR /&gt;   *code to compare same observations to min and output;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;You could also take a similar approach without the explicit looping:&lt;BR /&gt;&lt;BR /&gt;data want;&lt;BR /&gt;set have (in=a) have;&lt;BR /&gt;by id;&lt;BR /&gt;if a then do;&lt;BR /&gt;end;&lt;BR /&gt;else do;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;The bottom program would require retaining some calculated results, and checking for first.id.</description>
      <pubDate>Wed, 22 Nov 2023 05:19:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrange-date-values-closest-to-another-date/m-p/904179#M357240</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2023-11-22T05:19:13Z</dc:date>
    </item>
    <item>
      <title>Re: Arrange date values closest to another date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrange-date-values-closest-to-another-date/m-p/904216#M357256</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards truncover;
input id 1. L_date : mmddyy10.  M_date : mmddyy10.;
format l_date m_date yymmdd10.;;
cards;
1   9/1/2020    9/29/2020   
1   8/30/2020   9/29/2020
2   8/13/2022   10/21/2022
2   10/1/2022   10/21/2022
2   10/2/2023   10/21/2022
3   12/11/2021  .    
3   5/19/2022   .    
4   2/5/2022    12/11/2021
4   3/19/2022   12/11/2021  
4   6/21/2022   12/11/2021
5   2/5/2022    12/11/2021
5   3/19/2022   .
5   6/21/2022   .
;;;
run;

proc sql;
create table want(drop=diff M_date) as
select *,ifn(diff=min(diff),M_date,.) as new_Mdate format=mmddyy10. from
(select *, abs(M_date-L_date) as diff from have)
group by id 
order by 1,2;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 22 Nov 2023 12:01:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrange-date-values-closest-to-another-date/m-p/904216#M357256</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-11-22T12:01:41Z</dc:date>
    </item>
    <item>
      <title>Re: Arrange date values closest to another date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrange-date-values-closest-to-another-date/m-p/904311#M357298</link>
      <description>&lt;P&gt;Here first ID both records dates l_date &amp;amp; m_Date have same days difference (28 days) , it means m_date should populate for both records ?&lt;/P&gt;</description>
      <pubDate>Thu, 23 Nov 2023 06:15:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrange-date-values-closest-to-another-date/m-p/904311#M357298</guid>
      <dc:creator>lavu_90</dc:creator>
      <dc:date>2023-11-23T06:15:41Z</dc:date>
    </item>
  </channel>
</rss>

