<?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 Flag dates based on their proximity to a reference date in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Flag-dates-based-on-their-proximity-to-a-reference-date/m-p/984013#M43681</link>
    <description>&lt;P&gt;Hi guys,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;suppose to have the following dataset:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data DB;&lt;BR /&gt;input ID :$20. Date :$20. ReferenceDate :$20. Outcome :$20.;&lt;BR /&gt;cards;&lt;BR /&gt;0001 01/09/2024 01/06/2024 Response&lt;BR /&gt;0001 03/09/2024 01/06/2024 Response&lt;BR /&gt;0001 09/13/2024 01/06/2024 Response&lt;BR /&gt;0002 11/09/2016 06/30/2016 Complete Response&lt;BR /&gt;0002 09/20/2016 06/30/2016 Relapse&lt;BR /&gt;0003 06/30/2025 04/15/2025 Stable Disease &lt;BR /&gt;0003 07/31/2025 04/15/2025 Relapse &lt;BR /&gt;0004 03/12/2024 12/13/2023 Complete Response&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;/*Assign the correct format to dates */&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data DB1;
    set DB;
    numeric_date1 = input(Date, mmddyy10.); 
	numeric_date2 = input(ReferenceDate, mmddyy10.); 
	drop Date ReferenceDate; 
	rename numeric_date1 = Date numeric_date2 = ReferenceDate;
    format &lt;CODE class="language-sas"&gt;numeric_date1 numeric_date2 &lt;/CODE&gt;date9.; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Rule 1:&lt;/P&gt;
&lt;P&gt;I have to flag the date ("Date" variable) in the interval between 2 and 4 months after the ReferenceDate. This is okay for me. I know how to program this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Rule 2:&lt;/P&gt;
&lt;P&gt;Then, if more than one date falls into Rule 1, I have to flag (with another flag) the one that is closest to the Rule 1, i.e., the time point of 3 months.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The ReferenceDate is always the same for each subject.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can anyone help me to do this?&amp;nbsp;&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;Best&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 25 Feb 2026 13:45:57 GMT</pubDate>
    <dc:creator>NewUsrStat</dc:creator>
    <dc:date>2026-02-25T13:45:57Z</dc:date>
    <item>
      <title>Flag dates based on their proximity to a reference date</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Flag-dates-based-on-their-proximity-to-a-reference-date/m-p/984013#M43681</link>
      <description>&lt;P&gt;Hi guys,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;suppose to have the following dataset:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data DB;&lt;BR /&gt;input ID :$20. Date :$20. ReferenceDate :$20. Outcome :$20.;&lt;BR /&gt;cards;&lt;BR /&gt;0001 01/09/2024 01/06/2024 Response&lt;BR /&gt;0001 03/09/2024 01/06/2024 Response&lt;BR /&gt;0001 09/13/2024 01/06/2024 Response&lt;BR /&gt;0002 11/09/2016 06/30/2016 Complete Response&lt;BR /&gt;0002 09/20/2016 06/30/2016 Relapse&lt;BR /&gt;0003 06/30/2025 04/15/2025 Stable Disease &lt;BR /&gt;0003 07/31/2025 04/15/2025 Relapse &lt;BR /&gt;0004 03/12/2024 12/13/2023 Complete Response&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;/*Assign the correct format to dates */&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data DB1;
    set DB;
    numeric_date1 = input(Date, mmddyy10.); 
	numeric_date2 = input(ReferenceDate, mmddyy10.); 
	drop Date ReferenceDate; 
	rename numeric_date1 = Date numeric_date2 = ReferenceDate;
    format &lt;CODE class="language-sas"&gt;numeric_date1 numeric_date2 &lt;/CODE&gt;date9.; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Rule 1:&lt;/P&gt;
&lt;P&gt;I have to flag the date ("Date" variable) in the interval between 2 and 4 months after the ReferenceDate. This is okay for me. I know how to program this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Rule 2:&lt;/P&gt;
&lt;P&gt;Then, if more than one date falls into Rule 1, I have to flag (with another flag) the one that is closest to the Rule 1, i.e., the time point of 3 months.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The ReferenceDate is always the same for each subject.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can anyone help me to do this?&amp;nbsp;&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;Best&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 25 Feb 2026 13:45:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Flag-dates-based-on-their-proximity-to-a-reference-date/m-p/984013#M43681</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2026-02-25T13:45:57Z</dc:date>
    </item>
    <item>
      <title>Re: Flag dates based on their proximity to a reference date</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Flag-dates-based-on-their-proximity-to-a-reference-date/m-p/984017#M43682</link>
      <description>&lt;P&gt;Please do us a favor. First, its great that you provided data with SAS data step code. But you have provided us with data where the dates are character strings and not valid SAS numeric variable. We can't work with character string dates. The code also returns errors. I request that you fix the code so that there are no errors, and so that we have numeric variables that contain dates. I'm sure you know how to do that, so please do that for us.&lt;/P&gt;</description>
      <pubDate>Wed, 25 Feb 2026 11:43:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Flag-dates-based-on-their-proximity-to-a-reference-date/m-p/984017#M43682</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2026-02-25T11:43:54Z</dc:date>
    </item>
    <item>
      <title>Re: Flag dates based on their proximity to a reference date</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Flag-dates-based-on-their-proximity-to-a-reference-date/m-p/984020#M43683</link>
      <description>Sorry, I edited adding a code for conversion.</description>
      <pubDate>Wed, 25 Feb 2026 12:39:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Flag-dates-based-on-their-proximity-to-a-reference-date/m-p/984020#M43683</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2026-02-25T12:39:05Z</dc:date>
    </item>
    <item>
      <title>Re: Flag dates based on their proximity to a reference date</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Flag-dates-based-on-their-proximity-to-a-reference-date/m-p/984027#M43684</link>
      <description>&lt;P&gt;Your code to create data set DB still produces errors.&lt;/P&gt;</description>
      <pubDate>Wed, 25 Feb 2026 13:29:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Flag-dates-based-on-their-proximity-to-a-reference-date/m-p/984027#M43684</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2026-02-25T13:29:44Z</dc:date>
    </item>
    <item>
      <title>Re: Flag dates based on their proximity to a reference date</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Flag-dates-based-on-their-proximity-to-a-reference-date/m-p/984033#M43685</link>
      <description>Sorry again. Edited.</description>
      <pubDate>Wed, 25 Feb 2026 13:46:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Flag-dates-based-on-their-proximity-to-a-reference-date/m-p/984033#M43685</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2026-02-25T13:46:43Z</dc:date>
    </item>
    <item>
      <title>Re: Flag dates based on their proximity to a reference date</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Flag-dates-based-on-their-proximity-to-a-reference-date/m-p/984038#M43686</link>
      <description>&lt;P&gt;Thank you. Here is my solution, there are probably other ways to do this, I wouldn't be surprised if someone else comes up with a shorter solution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Create first flag */
data db2;
    set db1;
    flag1 = (intnx('month',referencedate,2,'s') &amp;lt;= date &amp;lt;= intnx('month',referencedate,4,'s'));
run;

/* Find IDs with more than one flag */
proc summary data=db2 nway;
    class id;
    var flag1;
    output out=_nflags_ sum=nflags;
run;

/* If more than one flag, determine distance to refdate+3 months */
data db3;
    merge db2 _nflags_(drop=_:);
    by id;
    if nflags&amp;gt;=2 then dist_betw_refdate_plus3months=abs(date-intnx('month',referencedate,3,'s'));
run;

/* Find minimum distance to refdate+3 months and select that one */
proc sort data=db3;
    by id dist_betw_refdate_plus3months;
run;
data want;
    set db3;
    by id dist_betw_refdate_plus3months;
    if (flag1=1 and nflags=1) or (flag1=1 and nflags&amp;gt;=2 and first.id);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PS: In the future, don't create date values as character. Create them as numeric and save yourself and all of us here a step.&lt;/P&gt;</description>
      <pubDate>Wed, 25 Feb 2026 14:18:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Flag-dates-based-on-their-proximity-to-a-reference-date/m-p/984038#M43686</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2026-02-25T14:18:29Z</dc:date>
    </item>
    <item>
      <title>Re: Flag dates based on their proximity to a reference date</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Flag-dates-based-on-their-proximity-to-a-reference-date/m-p/984050#M43687</link>
      <description>&lt;P&gt;What do you want to do when there are two dates that are the same distance, but on different sides?&amp;nbsp; Would you like to take the one that is BEFORE the 3 months post reference date or AFTER?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You need to decide how you want to count "months".&amp;nbsp; &amp;nbsp;Is it important to use actual calendar months?&amp;nbsp; If so then the number of days will vary because the length of calendar months varies.&amp;nbsp; It is usually only required to use calendar months when doing legal applications.&amp;nbsp; This looks like a Medical application so it is better to just use a constant number of days so you don't need to worry about the varying length of calendar months.&amp;nbsp; For a small number of months like 2 to 4 it is probably simplest to just use 30 day intervals as your proxy for "month".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First let's fix your example data step so it makes the DB dataset directly.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data DB;
  input ID :$20. (Date  ReferenceDate) (:mmddyy.) Outcome :$20.;
  format Date ReferenceDate date9.;
cards;
0001 01/09/2024 01/06/2024 Response
0001 03/09/2024 01/06/2024 Response
0001 09/13/2024 01/06/2024 Response
0002 11/09/2016 06/30/2016 Complete Response
0002 09/20/2016 06/30/2016 Relapse
0003 06/30/2025 04/15/2025 Stable Disease 
0003 07/31/2025 04/15/2025 Relapse 
0004 03/12/2024 12/13/2023 Complete Response
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So you can create FLAG1 directly reading the data as it is.&amp;nbsp; But to make FLAG2 you will first need to find the difference between DATE and the 3 month target date so you can discover which one is closest.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could do the calculation and ordering in one step by using PROC SQL.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table step1 as
select *
     , (date between referencedate+60 and referencedate+120) as Flag1
     , case when calculated Flag1 then abs(referencedate+90-date) 
            else .
       end as diff3month
from db
order by id, diff3month desc
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then you can use another data step to flag the closest date.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set step1;
  by id ;
  Flag2 = first.id and Flag1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 712px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/113362iB7DFE1C58B6F3EF3/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 25 Feb 2026 15:41:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Flag-dates-based-on-their-proximity-to-a-reference-date/m-p/984050#M43687</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2026-02-25T15:41:49Z</dc:date>
    </item>
  </channel>
</rss>

