<?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: How to select groups by condition occurring within a date range? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-groups-by-condition-occurring-within-a-date-range/m-p/726509#M225744</link>
    <description>&lt;P&gt;Ah ok. There you go.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID test_date :date9. test_resultb $;
format test_date date9.;
datalines;
1 27Mar1992 N
1 08Dec1999 P
1 29Jan2005 N
2 13Jan2015 N
2 09Mar2017 P
2 05Jun2018 P
3 15Oct1996 N
3 05Sep1997 N
3 28Jun1998 N
;

data want;

   if _N_ = 1 then do;
      dcl hash h(dataset : "have(where=(year(test_date) in (2017:2018)))");
      h.definekey("ID");
      h.definedone();
   end;

   set have;
   
   if h.check() = 0;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 15 Mar 2021 19:14:07 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2021-03-15T19:14:07Z</dc:date>
    <item>
      <title>How to select groups by condition occurring within a date range?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-groups-by-condition-occurring-within-a-date-range/m-p/726493#M225736</link>
      <description>&lt;DIV&gt;&lt;DIV class="resolved"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;Hello, I have data structured like this:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="1" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;test_date&lt;/TD&gt;&lt;TD&gt;test_result&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;27Mar1992&lt;/TD&gt;&lt;TD&gt;N&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;08Dec1999&lt;/TD&gt;&lt;TD&gt;P&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;29Jan2005&lt;/TD&gt;&lt;TD&gt;N&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;13Jan2015&lt;/TD&gt;&lt;TD&gt;N&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;09Mar2017&lt;/TD&gt;&lt;TD&gt;P&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;05Jun2018&lt;/TD&gt;&lt;TD&gt;P&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;15Oct1996&lt;/TD&gt;&lt;TD&gt;N&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;05Sep1997&lt;/TD&gt;&lt;TD&gt;N&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;28Jun1998&lt;/TD&gt;&lt;TD&gt;N&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;I need to keep all records for each ID if they had a test_result=P that occurred within 2017-2018. For this example only records from ID 2 would be kept.&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Thank you!&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Mon, 15 Mar 2021 18:25:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-groups-by-condition-occurring-within-a-date-range/m-p/726493#M225736</guid>
      <dc:creator>martyvd</dc:creator>
      <dc:date>2021-03-15T18:25:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to select groups by condition occurring within a date range?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-groups-by-condition-occurring-within-a-date-range/m-p/726498#M225739</link>
      <description>&lt;P&gt;Try this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID test_date :date9. test_resultb $;
format test_date date9.;
datalines;
1 27Mar1992 N
1 08Dec1999 P
1 29Jan2005 N
2 13Jan2015 N
2 09Mar2017 P
2 05Jun2018 P
3 15Oct1996 N
3 05Sep1997 N
3 28Jun1998 N
;

data want;
   set have;
   where year(test_date) in (2017:2018);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 15 Mar 2021 18:30:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-groups-by-condition-occurring-within-a-date-range/m-p/726498#M225739</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-03-15T18:30:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to select groups by condition occurring within a date range?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-groups-by-condition-occurring-within-a-date-range/m-p/726500#M225740</link>
      <description>Peter, maybe I should have clarified. I need to subset records for only IDs that had a positive test result (test_result=P) in 2017-2018. If an ID had a positive test in this time period, I need to keep all other records for that ID as well.</description>
      <pubDate>Mon, 15 Mar 2021 18:36:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-groups-by-condition-occurring-within-a-date-range/m-p/726500#M225740</guid>
      <dc:creator>martyvd</dc:creator>
      <dc:date>2021-03-15T18:36:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to select groups by condition occurring within a date range?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-groups-by-condition-occurring-within-a-date-range/m-p/726501#M225741</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;I need to keep all records for each ID if they had a test_result=P that occurred within 2017-2018. For this example only records from ID 2 would be kept.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as 
select * from have
where ID in (select id from have where test_result='P' and year(date) in (2017 2018) )
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Mar 2021 18:38:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-groups-by-condition-occurring-within-a-date-range/m-p/726501#M225741</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-03-15T18:38:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to select groups by condition occurring within a date range?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-groups-by-condition-occurring-within-a-date-range/m-p/726508#M225743</link>
      <description>This code does not give me any errors in the log but it produces a dataset with no rows. Not sure why.</description>
      <pubDate>Mon, 15 Mar 2021 19:10:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-groups-by-condition-occurring-within-a-date-range/m-p/726508#M225743</guid>
      <dc:creator>martyvd</dc:creator>
      <dc:date>2021-03-15T19:10:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to select groups by condition occurring within a date range?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-groups-by-condition-occurring-within-a-date-range/m-p/726509#M225744</link>
      <description>&lt;P&gt;Ah ok. There you go.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID test_date :date9. test_resultb $;
format test_date date9.;
datalines;
1 27Mar1992 N
1 08Dec1999 P
1 29Jan2005 N
2 13Jan2015 N
2 09Mar2017 P
2 05Jun2018 P
3 15Oct1996 N
3 05Sep1997 N
3 28Jun1998 N
;

data want;

   if _N_ = 1 then do;
      dcl hash h(dataset : "have(where=(year(test_date) in (2017:2018)))");
      h.definekey("ID");
      h.definedone();
   end;

   set have;
   
   if h.check() = 0;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 15 Mar 2021 19:14:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-groups-by-condition-occurring-within-a-date-range/m-p/726509#M225744</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-03-15T19:14:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to select groups by condition occurring within a date range?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-groups-by-condition-occurring-within-a-date-range/m-p/726519#M225750</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/228746"&gt;@martyvd&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;This code does not give me any errors in the log but it produces a dataset with no rows. Not sure why.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Did you make sure to change the data set names to match yours, and the date variable name?&lt;/P&gt;
&lt;P&gt;When I run:&lt;/P&gt;
&lt;PRE&gt;
data have;
input ID	test_date :date9.	test_result $;
format test_date date9.;
datalines;
1	27Mar1992	N
1	08Dec1999	P
1	29Jan2005	N
2	13Jan2015	N
2	09Mar2017	P
2	05Jun2018	P
3	15Oct1996	N
3	05Sep1997	N
3	28Jun1998	N
;

proc sql;
create table want as 
select * from have
where ID in (select id from have where test_result='P' and year(test_date) in (2017 2018) )
;
quit; &lt;/PRE&gt;
&lt;P&gt;I get the 3 records with ID=2.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Post your log from running the code.&lt;/P&gt;</description>
      <pubDate>Mon, 15 Mar 2021 19:38:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-groups-by-condition-occurring-within-a-date-range/m-p/726519#M225750</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-03-15T19:38:19Z</dc:date>
    </item>
    <item>
      <title>Re: How to select groups by condition occurring within a date range?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-groups-by-condition-occurring-within-a-date-range/m-p/726522#M225752</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/228746"&gt;@martyvd&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input ID test_date :date9. test_resultb $;
format test_date date9.;
datalines;
1 27Mar1992 N
1 08Dec1999 P
1 29Jan2005 N
2 13Jan2015 N
2 09Mar2017 P
2 05Jun2018 P
3 15Oct1996 N
3 05Sep1997 N
3 28Jun1998 N
;

proc sql;
 create table want as
 select *
 from have 
 group by id
 having sum((test_resultb='P') and year(test_date) in (2017,2018));
quit;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 15 Mar 2021 19:50:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-groups-by-condition-occurring-within-a-date-range/m-p/726522#M225752</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2021-03-15T19:50:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to select groups by condition occurring within a date range?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-groups-by-condition-occurring-within-a-date-range/m-p/726766#M225883</link>
      <description>There was a problem with my data that has been resolved. Now the code seems to be working, thanks.</description>
      <pubDate>Tue, 16 Mar 2021 14:04:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-groups-by-condition-occurring-within-a-date-range/m-p/726766#M225883</guid>
      <dc:creator>martyvd</dc:creator>
      <dc:date>2021-03-16T14:04:16Z</dc:date>
    </item>
  </channel>
</rss>

