<?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: Selecting Data based off of Date and ID in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Selecting-Data-based-off-of-Date-and-ID/m-p/576693#M163274</link>
    <description>I am not sure why I did the dates that way, I was adjusting them trying to get it to work earlier and I must have been off that whole time. Thanks!</description>
    <pubDate>Thu, 25 Jul 2019 17:00:03 GMT</pubDate>
    <dc:creator>A_SAS_Man</dc:creator>
    <dc:date>2019-07-25T17:00:03Z</dc:date>
    <item>
      <title>Selecting Data based off of Date and ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-Data-based-off-of-Date-and-ID/m-p/576672#M163269</link>
      <description>&lt;P&gt;I am trying to select data based off of date and id from another table and summing a third column. Hopefully the below illustrates what I'm trying to do.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data reference_table;
infile datalines dlm='#';
 input id &amp;amp; $1. date date7. product $ dollars;
 format date ddmmyy.;
 datalines;
1# 01JAN17# shoe# 200
1# 01Mar18# shoe# 200
1# 01Sep15# shoe# 200
2# 01Feb19# shoe# 200
2# 01Jul18# shoe# 200
2# 01Jun17# tops# 200
3# 01Aug14# tops# 300
3# 01Dec13# tape# 200
4# 01May17# tape# 200
4# 01Sep17# limo# 800
5# 01Apr16# hats# 500
5# 01Oct18# pens# 500
 ;
 run;

data new_month_ids;
 infile datalines dlm='#';
 input id &amp;amp; $1. date date7.;
  format date ddmmyy.;
 datalines;
1# '01Jan17'd
2# '13Feb19'd
3# '28Dec13'd
4# '27Jun17'd
 ;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;From these two tables I am trying to do a query involving both which selects all the id's that appear in the "new_month_ids" table, and sum all of the dollars&amp;nbsp; from the reference table that occurred within two years of the date in the "new_month_ids" table by both id and product. I believe the below table is what the final result should look like.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; data want;
 infile datalines dlm='#';
 input id &amp;amp; $1. product $ dollars;
 datalines;
 1# shoe# 400
 2# shoe# 200
 2# tops# 200
 3# tape# 200
 4# tape# 200
 4# limo# 800
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Notice for example, for id 1 we only sum two of the lines because those are the only two that appear within the two year time frame 01Jan2015-01Jan2017.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Below is my attempt at this, but when I run this code it returns no values. I have a feeling this might be related to my date format in the intnx function but I haven't figured it out yet.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table test as
	select a.id,
		   a.product,
		   sum(a.dollars) as dollars
	from reference_table a left join new_month_ids b on
		 (a.id=b.id)
	where a.id in (select distinct b.id from new_month_ids b)
		  And
		  intnx('year',b.date,-2,'same')&amp;lt;=a.date&amp;lt;=b.date
	group by a.id,
			 a.product
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Any help is appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jul 2019 15:57:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-Data-based-off-of-Date-and-ID/m-p/576672#M163269</guid>
      <dc:creator>A_SAS_Man</dc:creator>
      <dc:date>2019-07-25T15:57:51Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting Data based off of Date and ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-Data-based-off-of-Date-and-ID/m-p/576682#M163270</link>
      <description>&lt;P&gt;If that is actually how you built the NEW_MONTH_IDS data set then that code is the major problem.&lt;/P&gt;
&lt;P&gt;When I run&lt;/P&gt;
&lt;PRE&gt;proc print data=new_month_ids;
  var date;
  format date date9.;
run;&lt;/PRE&gt;
&lt;P&gt;The result is&lt;/P&gt;
&lt;PRE&gt;Obs         date

 1     01JAN2001
 2     13FEB2001
 3     28DEC2001
 4     27JUN2001
&lt;/PRE&gt;
&lt;P&gt;Why did you have the date literal format?&lt;/P&gt;
&lt;P&gt;Try&lt;/P&gt;
&lt;PRE&gt;data new_month_ids;
 infile datalines ;
 input id : $1. date :date7.;
  format date ddmmyy.;
 datalines;
1 01Jan17
2 13Feb19
3 28Dec13
4 27Jun17
 ;
run;&lt;/PRE&gt;
&lt;P&gt;After fixing that data set so the dates are correct I get 4 records and&amp;nbsp;a message about invalid arguments to INTNX because the&lt;/P&gt;
&lt;P&gt;NEW_MONTH_IDS data set does not have all the IDs that are in the reference set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your data does not support getting all of the desired output.&lt;/P&gt;
&lt;P&gt;4# 01Sep17# limo# 800&lt;/P&gt;
&lt;P&gt;does not occur in the interval for 4 in the months set : 4 27Jun17 as Sep17 comes AFTER Jun 17 so that can't pull the 4 limo 800 into the result&lt;/P&gt;
&lt;P&gt;This gets closer to the output desired barring the date issue above&lt;/P&gt;
&lt;PRE&gt;proc sql;
   create table test as
	select a.id,
		   a.product,
		   sum(a.dollars) as dollars
	from reference_table a left join new_month_ids b on
		 (a.id=b.id)
   where intnx('year',b.date,-2,'same')&amp;lt;= a.date &amp;lt;= b.date
	group by a.id,
			 a.product
;
quit;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;LI-WRAPPER&gt;&lt;/LI-WRAPPER&gt;&lt;/P&gt;
&lt;P&gt;&lt;LI-WRAPPER&gt;&lt;/LI-WRAPPER&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jul 2019 16:36:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-Data-based-off-of-Date-and-ID/m-p/576682#M163270</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-07-25T16:36:19Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting Data based off of Date and ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-Data-based-off-of-Date-and-ID/m-p/576693#M163274</link>
      <description>I am not sure why I did the dates that way, I was adjusting them trying to get it to work earlier and I must have been off that whole time. Thanks!</description>
      <pubDate>Thu, 25 Jul 2019 17:00:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-Data-based-off-of-Date-and-ID/m-p/576693#M163274</guid>
      <dc:creator>A_SAS_Man</dc:creator>
      <dc:date>2019-07-25T17:00:03Z</dc:date>
    </item>
  </channel>
</rss>

