<?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: select rows within one month of a time in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/select-rows-within-one-month-of-a-time/m-p/368432#M87875</link>
    <description>&lt;P&gt;Hi art297,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried to use the slolution you helped me, it works!! but when I use my real data (I have lots of columns and rows) it doesn't work.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;it shows " The query requires remerging summary statistics back with the original data." in the log. I&amp;nbsp;doubt. Do you know where&amp;nbsp;is the problem? Thanks,&lt;/P&gt;&lt;P&gt;L&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;1455  proc sql noprint;
1456      create table want as
1457          select mrn, event_end_UA, event_end_dt
1458          from combine_CBC
1459          where intnx('month', event_end_UA, -1, 's') &amp;lt;= datepart(event_end_dt) &amp;lt;=
1459! intnx('month', event_end_UA, 1, 's')
1460          group by mrn, event_end_UA
1461          having abs(datepart(event_end_dt) - event_end_UA) = min(abs(datepart(event_end_UA)-
1461! event_end_dt))
1462  ;
NOTE: The query requires remerging summary statistics back with the original data.
NOTE: Table WORK.WANT created, with 0 rows and 3 columns.

1463  quit;
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.03 seconds
      cpu time            0.04 seconds

&lt;/PRE&gt;</description>
    <pubDate>Mon, 19 Jun 2017 18:46:40 GMT</pubDate>
    <dc:creator>echoli</dc:creator>
    <dc:date>2017-06-19T18:46:40Z</dc:date>
    <item>
      <title>select rows within one month of a time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-rows-within-one-month-of-a-time/m-p/367884#M87644</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;Here is a dataset, how can I select rows if the event_time closest to &lt;STRONG&gt;date&lt;/STRONG&gt; &lt;FONT color="#FF0000"&gt;measured within one month&lt;/FONT&gt;. For example, for id = 14022, the date is 6/22/2010, I want to select reows that event_time should be in 5/22/2010 to 7/22/2010. (if there are two&amp;nbsp;measures within one month, choose the closest one. if there is no measure within one month, then NA.). &amp;nbsp; Any clue or idea?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data abc;&lt;BR /&gt;input @1 ID @8 Date mmddyy10. @20 time anydtdtm.;&lt;BR /&gt;format date mmddyy10. time MDYAMPM.;&lt;BR /&gt;datalines;&lt;BR /&gt;14022 6/22/2010 10/11/2004 9:02&lt;BR /&gt;14022 6/22/2010 3/25/2008 12:53&lt;BR /&gt;14022 6/22/2010 4/29/2008 9:04&lt;BR /&gt;14022 6/22/2010 6/17/2008 9:14&lt;BR /&gt;14022 6/22/2010 7/8/2008 10:41&lt;BR /&gt;14022 6/22/2010 8/12/2008 12:30&lt;BR /&gt;14022 6/22/2010 9/16/2008 14:24&lt;BR /&gt;14022 6/22/2010 10/21/2008 1:44&lt;BR /&gt;14022 6/22/2010 11/18/2008 2:11&lt;BR /&gt;14022 6/22/2010 12/30/2008 9:53&lt;BR /&gt;14022 6/22/2010 2/10/2009 13:07&lt;BR /&gt;14022 6/22/2010 4/7/2009 9:45&lt;BR /&gt;14022 6/22/2010 6/2/2009 7:35&lt;BR /&gt;14022 6/22/2010 7/28/2009 10:11&lt;BR /&gt;14022 6/22/2010 9/29/2009 10:46&lt;BR /&gt;14022 6/22/2010 12/1/2009 10:51&lt;BR /&gt;14022 6/22/2010 1/26/2010 8:49&lt;BR /&gt;14022 6/22/2010 4/13/2010 7:48&lt;BR /&gt;14022 6/22/2010 6/22/2010 7:46&lt;BR /&gt;14032 2/7/2012 12/24/2002 10:25&lt;BR /&gt;14032 2/7/2012 1/21/2003 12:00&lt;BR /&gt;14032 2/7/2012 1/28/2003 9:25&lt;BR /&gt;14032 2/7/2012 1/29/2008 9:00&lt;BR /&gt;14032 2/7/2012 3/20/2008 13:52&lt;BR /&gt;14032 2/7/2012 5/1/2008 8:53&lt;BR /&gt;14032 2/7/2012 6/12/2008 14:01&lt;BR /&gt;14032 2/7/2012 7/24/2008 15:00&lt;BR /&gt;14032 2/7/2012 9/11/2008 11:02&lt;BR /&gt;14032 2/7/2012 9/20/2011 9:25&lt;BR /&gt;14032 2/7/2012 11/1/2011 10:40&lt;BR /&gt;14032 2/7/2012 12/22/2011 12:58&lt;BR /&gt;14032 2/7/2012 2/7/2012 10:20&lt;BR /&gt;14032 2/7/2012 3/20/2012 9:18&lt;BR /&gt;14032 2/7/2012 5/15/2012 12:43&lt;BR /&gt;14032 2/7/2012 6/19/2012 9:19&lt;BR /&gt;14032 2/7/2012 7/31/2012 11:48&lt;BR /&gt;14032 2/7/2012 9/18/2012 12:15&lt;BR /&gt;14032 2/7/2012 10/16/2012 12:35&lt;BR /&gt;14032 2/7/2012 10/18/2012 11:12&lt;BR /&gt;14032 2/7/2012 10/19/2012 5:00&lt;BR /&gt;14032 2/7/2012 10/20/2012 4:33&lt;BR /&gt;14032 2/7/2012 10/21/2012 4:19&lt;BR /&gt;14032 2/7/2012 10/22/2012 4:30&lt;BR /&gt;14032 2/7/2012 10/23/2012 5:03&lt;BR /&gt;14032 2/7/2012 10/24/2012 3:14&lt;BR /&gt;14032 2/7/2012 10/30/2012 11:10&lt;BR /&gt;14032 2/7/2012 11/27/2012 12:30&lt;BR /&gt;14032 2/7/2012 1/8/2013 9:58&lt;BR /&gt;14032 2/7/2012 2/19/2013 10:07&lt;BR /&gt;14032 2/7/2012 4/2/2013 13:45&lt;BR /&gt;14032 2/7/2012 5/14/2013 10:18&lt;BR /&gt;14601 1/30/2014 4/26/2005 8:40&lt;BR /&gt;14601 1/30/2014 6/28/2005 9:05&lt;BR /&gt;14601 1/30/2014 7/26/2005 8:21&lt;BR /&gt;14601 1/30/2014 8/9/2005 8:24&lt;BR /&gt;14601 1/30/2014 8/23/2005 8:10&lt;BR /&gt;14601 1/30/2014 10/18/2005 9:20&lt;BR /&gt;14601 1/30/2014 12/13/2005 9:32&lt;BR /&gt;14601 1/30/2014 2/7/2006 9:04&lt;BR /&gt;14601 1/30/2014 2/16/2006 8:44&lt;BR /&gt;14601 1/30/2014 5/2/2006 10:31&lt;BR /&gt;14601 1/30/2014 7/12/2006 7:31&lt;BR /&gt;14601 1/30/2014 9/7/2006 8:55&lt;BR /&gt;14601 1/30/2014 10/31/2006 8:43&lt;BR /&gt;14601 1/30/2014 2/6/2007 9:15&lt;BR /&gt;14601 1/30/2014 5/3/2007 7:54&lt;BR /&gt;14601 1/30/2014 6/28/2007 7:52&lt;BR /&gt;14601 1/30/2014 8/23/2007 8:10&lt;BR /&gt;14601 1/30/2014 10/18/2007 8:17&lt;BR /&gt;14601 1/30/2014 11/19/2007 2:07&lt;BR /&gt;14601 1/30/2014 12/13/2007 8:16&lt;BR /&gt;14601 1/30/2014 1/24/2008 10:23&lt;BR /&gt;14601 1/30/2014 1/31/2008 10:07&lt;BR /&gt;14601 1/30/2014 2/12/2008 12:31&lt;BR /&gt;14601 1/30/2014 3/25/2008 9:52&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Jun 2017 20:24:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-rows-within-one-month-of-a-time/m-p/367884#M87644</guid>
      <dc:creator>echoli</dc:creator>
      <dc:date>2017-06-16T20:24:43Z</dc:date>
    </item>
    <item>
      <title>Re: select rows within one month of a time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-rows-within-one-month-of-a-time/m-p/367901#M87647</link>
      <description>&lt;P&gt;I modified your example data a bit so that there would be some records that allowed for a better test. Does the following do what you want?&lt;/P&gt;
&lt;PRE&gt;data abc;
  informat date mmddyy10.;
  informat time anydtdtm16.;
  input ID Date time;
  format date mmddyy10. time MDYAMPM.;
  datalines;
14022 6/22/2010 10/11/2004 9:02
14022 6/22/2010 3/25/2008 12:53
14022 6/22/2010 4/29/2008 9:04
14022 6/22/2010 6/17/2008 9:14
14022 6/22/2010 7/8/2008 10:41
14022 6/22/2010 8/12/2008 12:30
14022 6/22/2010 9/16/2008 14:24
14022 6/22/2010 10/21/2008 1:44
14022 6/22/2010 11/18/2008 2:11
14022 6/22/2010 12/30/2008 9:53
14022 6/22/2010 2/10/2009 13:07
14022 6/22/2010 4/7/2009 9:45
14022 6/22/2010 6/2/2009 7:35
14022 6/22/2010 7/28/2009 10:11
14022 6/22/2010 9/29/2009 10:46
14022 6/22/2010 12/1/2009 10:51
14022 6/22/2010 1/26/2010 8:49
14022 6/22/2010 5/23/2010 7:48
14022 6/22/2010 6/22/2010 7:46
14032 2/7/2012 7/24/2002 10:25
14032 2/7/2012 1/21/2003 12:00
14032 2/7/2012 1/28/2003 9:25
14032 2/7/2012 1/29/2008 9:00
14032 2/7/2012 3/20/2008 13:52
14032 2/7/2012 5/1/2008 8:53
14032 2/7/2012 6/12/2008 14:01
14032 2/7/2012 7/24/2008 15:00
14032 2/7/2012 9/11/2008 11:02
14032 2/7/2012 9/20/2011 9:25
14032 2/7/2012 11/1/2011 10:40
14032 2/7/2012 1/8/2012 12:58
14032 2/7/2012 2/7/2012 10:20
14032 2/7/2012 3/7/2012 9:18
14032 2/7/2012 5/15/2012 12:43
14032 2/7/2012 6/19/2012 9:19
14032 2/7/2012 7/31/2012 11:48
14032 2/7/2012 9/18/2012 12:15
14032 2/7/2012 10/16/2012 12:35
14032 2/7/2012 10/18/2012 11:12
14032 2/7/2012 10/19/2012 5:00
14032 2/7/2012 10/20/2012 4:33
14032 2/7/2012 10/21/2012 4:19
14032 2/7/2012 10/22/2012 4:30
14032 2/7/2012 10/23/2012 5:03
14032 2/7/2012 10/24/2012 3:14
14032 2/7/2012 10/30/2012 11:10
14032 2/7/2012 11/27/2012 12:30
14032 2/7/2012 1/8/2013 9:58
14032 2/7/2012 2/19/2013 10:07
14032 2/7/2012 4/2/2013 13:45
14032 2/7/2012 5/14/2013 10:18
14601 1/30/2014 4/26/2005 8:40
14601 1/30/2014 6/28/2005 9:05
14601 1/30/2014 7/26/2005 8:21
14601 1/30/2014 8/9/2005 8:24
14601 1/30/2014 8/23/2005 8:10
14601 1/30/2014 10/18/2005 9:20
14601 1/30/2014 12/29/2013 9:32
14601 1/30/2014 12/30/2013 9:04
14601 1/30/2014 2/16/2014 8:44
14601 1/30/2014 5/2/2006 10:31
14601 1/30/2014 7/12/2006 7:31
14601 1/30/2014 9/7/2006 8:55
14601 1/30/2014 10/31/2006 8:43
14601 1/30/2014 2/6/2007 9:15
14601 1/30/2014 5/3/2007 7:54
14601 1/30/2014 6/28/2007 7:52
14601 1/30/2014 8/23/2007 8:10
14601 1/30/2014 10/18/2007 8:17
14601 1/30/2014 11/19/2007 2:07
14601 1/30/2014 12/13/2007 8:16
14601 1/30/2014 1/24/2008 10:23
14601 1/30/2014 1/31/2008 10:07
14601 1/30/2014 2/12/2008 12:31
14601 1/30/2014 3/25/2008 9:52
;
run;

proc sql noprint;
  create table want as
    select *
      from abc 
       where intnx('month',date,-1,'s')&amp;lt;=datepart(time)&amp;lt;=intnx('month',date,1,'s')
         group by id
           having abs(datepart(time)-date) eq min(abs(datepart(time)-date))
  ;
quit;

&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;</description>
      <pubDate>Fri, 16 Jun 2017 22:06:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-rows-within-one-month-of-a-time/m-p/367901#M87647</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-06-16T22:06:16Z</dc:date>
    </item>
    <item>
      <title>Re: select rows within one month of a time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-rows-within-one-month-of-a-time/m-p/367903#M87648</link>
      <description>&lt;P&gt;Your example data does not have a variable named event_time only "time". So I suspose that's the one you want to use.&lt;/P&gt;
&lt;P&gt;Is the time of day supposed to be considered when picking "closest"? if so, since there is no time component with your date variable which time of day would you want?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that we can't run your code as pasted as the window reformatted the columns so the time actually starts in column 16 or&amp;nbsp;17 instead of 20.&lt;/P&gt;
&lt;P&gt;Please post code in a code box using the forum {i} menu icon if you are going to post &lt;STRONG&gt;anything&lt;/STRONG&gt; where columns might be important.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This line of code added to the read will reduce the data set to only datetimes where the date part is within you specified range.&lt;/P&gt;
&lt;PRE&gt;   if intnx('month',date,-1,'S') le datepart(time) le intnx('month',date,+1,'S');
&lt;/PRE&gt;
&lt;P&gt;And this will determine the number of days different.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;   DaysDifferent = abs(date- datepart(time));
&lt;/PRE&gt;
&lt;P&gt;Sort the resulting data by ID and DaysDifferent and keep the first.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assuming the above two lines were added to your code:&lt;/P&gt;
&lt;PRE&gt;proc sort data=abc;
   by id daydifferent;
run;

data want;
   set abc;
   by id;
   if first.id;
run;&lt;/PRE&gt;
&lt;P&gt;Would result in one row per ID.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the there are two days that are exactly the same number of days from the date and also the smallest I think this will pick the early date of time.&lt;/P&gt;</description>
      <pubDate>Fri, 16 Jun 2017 22:18:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-rows-within-one-month-of-a-time/m-p/367903#M87648</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-06-16T22:18:21Z</dc:date>
    </item>
    <item>
      <title>Re: select rows within one month of a time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-rows-within-one-month-of-a-time/m-p/368426#M87873</link>
      <description>&lt;P&gt;Thanks for your explanation, Ballardw! That's really my goal!!&lt;/P&gt;</description>
      <pubDate>Mon, 19 Jun 2017 18:40:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-rows-within-one-month-of-a-time/m-p/368426#M87873</guid>
      <dc:creator>echoli</dc:creator>
      <dc:date>2017-06-19T18:40:46Z</dc:date>
    </item>
    <item>
      <title>Re: select rows within one month of a time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-rows-within-one-month-of-a-time/m-p/368432#M87875</link>
      <description>&lt;P&gt;Hi art297,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried to use the slolution you helped me, it works!! but when I use my real data (I have lots of columns and rows) it doesn't work.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;it shows " The query requires remerging summary statistics back with the original data." in the log. I&amp;nbsp;doubt. Do you know where&amp;nbsp;is the problem? Thanks,&lt;/P&gt;&lt;P&gt;L&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;1455  proc sql noprint;
1456      create table want as
1457          select mrn, event_end_UA, event_end_dt
1458          from combine_CBC
1459          where intnx('month', event_end_UA, -1, 's') &amp;lt;= datepart(event_end_dt) &amp;lt;=
1459! intnx('month', event_end_UA, 1, 's')
1460          group by mrn, event_end_UA
1461          having abs(datepart(event_end_dt) - event_end_UA) = min(abs(datepart(event_end_UA)-
1461! event_end_dt))
1462  ;
NOTE: The query requires remerging summary statistics back with the original data.
NOTE: Table WORK.WANT created, with 0 rows and 3 columns.

1463  quit;
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.03 seconds
      cpu time            0.04 seconds

&lt;/PRE&gt;</description>
      <pubDate>Mon, 19 Jun 2017 18:46:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-rows-within-one-month-of-a-time/m-p/368432#M87875</guid>
      <dc:creator>echoli</dc:creator>
      <dc:date>2017-06-19T18:46:40Z</dc:date>
    </item>
    <item>
      <title>Re: select rows within one month of a time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-rows-within-one-month-of-a-time/m-p/368437#M87876</link>
      <description>&lt;P&gt;That is a normal message from proc sql. It doesn't mean that there was a problem.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, since it didn't select any rows, it indicates that you don't have any records that meet the condition that you specified.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You'd have to post some of your actual data for us to understand.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Are you sure that event_end_UA is a date, rather than a datetime variable?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 19 Jun 2017 19:11:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-rows-within-one-month-of-a-time/m-p/368437#M87876</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-06-19T19:11:43Z</dc:date>
    </item>
  </channel>
</rss>

