<?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 Identify a specific date range among multiple observations of date in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Identify-a-specific-date-range-among-multiple-observations-of/m-p/780284#M248612</link>
    <description>&lt;P&gt;Hi, I have a question related to date interval. I have a dataset in which each subject has multiple observations (different dates). I was asked to identify subjects who have at least two records with their date between 1 and 2 years. The sample data is shown below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data WORK.SAMPLE;
  infile datalines dsd truncover;
  input id 1. date:YYMMDD10.;
  format date YYMMDD10.;
datalines;
1 2012-07-24
1 2015-04-09
1 2018-03-15
1 2020-01-16
2 2019-12-02
2 2020-04-27
2 2020-05-18
3 2017-10-31
3 2018-01-09
3 2018-12-10
3 2019-04-08
3 2019-09-11
3 2020-04-10
3 2020-04-10
3 2020-12-24
;;;;&lt;/PRE&gt;&lt;P&gt;For example, there are four dates for id 1, its third and fourth observations meet the requirement; id 2 doesn't meet the requirement because the largest time range is less than 1 year; id 3 meets the requirement (2017-10-31 to 2018-12-10, etc.). &lt;STRONG&gt;My question is, how can I identify two records that meet requirement from multiple records efficiently?&lt;/STRONG&gt; The only way that I came up with is to compare all records one by one (e.g. 1st date vs 2nd date, 1st date vs 3rd date, 2nd date vs 3rd date), but I don't think it's the best way to do it&lt;SPAN style="font-family: inherit;"&gt;.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
    <pubDate>Mon, 15 Nov 2021 18:40:20 GMT</pubDate>
    <dc:creator>Chaupak</dc:creator>
    <dc:date>2021-11-15T18:40:20Z</dc:date>
    <item>
      <title>Identify a specific date range among multiple observations of date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-a-specific-date-range-among-multiple-observations-of/m-p/780284#M248612</link>
      <description>&lt;P&gt;Hi, I have a question related to date interval. I have a dataset in which each subject has multiple observations (different dates). I was asked to identify subjects who have at least two records with their date between 1 and 2 years. The sample data is shown below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data WORK.SAMPLE;
  infile datalines dsd truncover;
  input id 1. date:YYMMDD10.;
  format date YYMMDD10.;
datalines;
1 2012-07-24
1 2015-04-09
1 2018-03-15
1 2020-01-16
2 2019-12-02
2 2020-04-27
2 2020-05-18
3 2017-10-31
3 2018-01-09
3 2018-12-10
3 2019-04-08
3 2019-09-11
3 2020-04-10
3 2020-04-10
3 2020-12-24
;;;;&lt;/PRE&gt;&lt;P&gt;For example, there are four dates for id 1, its third and fourth observations meet the requirement; id 2 doesn't meet the requirement because the largest time range is less than 1 year; id 3 meets the requirement (2017-10-31 to 2018-12-10, etc.). &lt;STRONG&gt;My question is, how can I identify two records that meet requirement from multiple records efficiently?&lt;/STRONG&gt; The only way that I came up with is to compare all records one by one (e.g. 1st date vs 2nd date, 1st date vs 3rd date, 2nd date vs 3rd date), but I don't think it's the best way to do it&lt;SPAN style="font-family: inherit;"&gt;.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Mon, 15 Nov 2021 18:40:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-a-specific-date-range-among-multiple-observations-of/m-p/780284#M248612</guid>
      <dc:creator>Chaupak</dc:creator>
      <dc:date>2021-11-15T18:40:20Z</dc:date>
    </item>
    <item>
      <title>Re: Identify a specific date range among multiple observations of date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-a-specific-date-range-among-multiple-observations-of/m-p/780286#M248613</link>
      <description>&lt;P&gt;Is date range always computed from the first date for an ID? Or is date range computed from the difference between the date on a given row and the date on the row before it? Or something else?&lt;/P&gt;</description>
      <pubDate>Mon, 15 Nov 2021 19:06:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-a-specific-date-range-among-multiple-observations-of/m-p/780286#M248613</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-11-15T19:06:04Z</dc:date>
    </item>
    <item>
      <title>Re: Identify a specific date range among multiple observations of date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-a-specific-date-range-among-multiple-observations-of/m-p/780289#M248615</link>
      <description>&lt;P&gt;Hi, the date range may be computed from any two records for a single subject. They are not necessarily compared with the earliest date or compared with the adjacent records. That's what I was most concerned about.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Nov 2021 19:11:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-a-specific-date-range-among-multiple-observations-of/m-p/780289#M248615</guid>
      <dc:creator>Chaupak</dc:creator>
      <dc:date>2021-11-15T19:11:36Z</dc:date>
    </item>
    <item>
      <title>Re: Identify a specific date range among multiple observations of date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-a-specific-date-range-among-multiple-observations-of/m-p/780293#M248616</link>
      <description>&lt;P&gt;Normally, I advise against wide data sets, preferring long data sets, but it seems to me the easiest method here is to create a wide data set using PROC TRANSPOSE, and then use ARRAYs to do all possible comparisons within an ID. This code really doesn't adjust for leap years, if that's important then you can use the INTCK function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=sample prefix=date out=sample_t;
    var date;
    by id;
run;

data want;
    set sample_t;
    array d date:;
    count=0;
    do i=1 to dim(d)-1;
        do j=(i+1) to dim(d);
            if 365&amp;lt;=abs(d(i)-d(j))&amp;lt;=730 then count=count+1;
        end;
    end;
    drop j i _name_;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Adding, I'm sure its possible without a transpose, using &lt;A href="https://sasnrd.com/sas-dow-loop-example/" target="_self"&gt;DOW loops&lt;/A&gt;&amp;nbsp;(as explained by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;) or hash objects, but this seems pretty simple to me.&lt;/P&gt;</description>
      <pubDate>Mon, 15 Nov 2021 20:17:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-a-specific-date-range-among-multiple-observations-of/m-p/780293#M248616</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-11-15T20:17:26Z</dc:date>
    </item>
    <item>
      <title>Re: Identify a specific date range among multiple observations of date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-a-specific-date-range-among-multiple-observations-of/m-p/780302#M248621</link>
      <description>&lt;P&gt;Thank you so much for the sample solution! It worked for me when the dataset is small. However, my dataset is large and there are a few extreme values for dates (~1400 dates), which makes the number of variables so large and creates many missing values for dates in other subjects. I can exclude the subjects with extreme dates first, and run the loop separately. I wonder if there is any methods to make this process faster. Anyway, many thanks to your solution!&lt;/P&gt;</description>
      <pubDate>Mon, 15 Nov 2021 21:08:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-a-specific-date-range-among-multiple-observations-of/m-p/780302#M248621</guid>
      <dc:creator>Chaupak</dc:creator>
      <dc:date>2021-11-15T21:08:43Z</dc:date>
    </item>
    <item>
      <title>Re: Identify a specific date range among multiple observations of date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-a-specific-date-range-among-multiple-observations-of/m-p/780303#M248622</link>
      <description>&lt;P&gt;Found a solution to skip missing values and improve the speed. Just add a LEAVE statement when we meet a missing value.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data want;
    set sample_t;
    array d date:;
    count=0;
    do i=1 to dim(d)-1;
        do j=(i+1) to dim(d);
            if 365&amp;lt;=abs(d(i)-d(j))&amp;lt;=730 then count=count+1;
            if d(j) = . then leave;
        end;
        if d(i) = . then leave;
    end;
    drop j i _name_;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 15 Nov 2021 21:27:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-a-specific-date-range-among-multiple-observations-of/m-p/780303#M248622</guid>
      <dc:creator>Chaupak</dc:creator>
      <dc:date>2021-11-15T21:27:34Z</dc:date>
    </item>
    <item>
      <title>Re: Identify a specific date range among multiple observations of date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-a-specific-date-range-among-multiple-observations-of/m-p/780313#M248628</link>
      <description>&lt;P&gt;Wouldn't this be even faster?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    set sample_t;
    array d date:;
    count=0;
    do i=1 to dim(d)-1;
        if d(i) = . then leave;
        do j=(i+1) to dim(d);
            if d(j) = . then leave;
            if 365&amp;lt;=abs(d(i)-d(j))&amp;lt;=730 then count=count+1;
        end;
    end;
    drop j i _name_;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 15 Nov 2021 22:20:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-a-specific-date-range-among-multiple-observations-of/m-p/780313#M248628</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-11-15T22:20:16Z</dc:date>
    </item>
  </channel>
</rss>

