<?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 Subtracting dates in WHERE step of PROC SQL in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Subtracting-dates-in-WHERE-step-of-PROC-SQL/m-p/242790#M55853</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'd like to run a Proc SQL step to link 3 databases. &amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to use the WHERE statement to filter results by subtracting a date one from table from the other.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's what it looks like right now:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql; 
	connect to oracle;
	create table MRSA as select * from connection to oracle 
		(
			select distinct 
				c.MRN,
				a.account_num, a.discharge_date, 
				b.collect_date, a.admit_date
			from table1 a, 
				 table2 b,
				 table3 c
			where a.td_patient_id=b.td_patient_id and
			      b.td_patient_id=c.td_patient_id and
			      0 &amp;lt;= (datepart(b.collect_date) - datepart(a.admit_date)) &amp;lt;= 2 and
			      trunc(a.admit_date, 'DDD') &amp;gt;= '1-Jan-2012' and
			      trunc(a.admit_date, 'DDD') &amp;lt;= '1-Jan-2012' 
);
	disconnect from oracle; 
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm receiving this error when running:&lt;/P&gt;&lt;P&gt;ORACLE prepare error&lt;BR /&gt;ORA-00933: SQL command not properly ended&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for the help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 11 Jan 2016 19:40:40 GMT</pubDate>
    <dc:creator>malmario</dc:creator>
    <dc:date>2016-01-11T19:40:40Z</dc:date>
    <item>
      <title>Subtracting dates in WHERE step of PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Subtracting-dates-in-WHERE-step-of-PROC-SQL/m-p/242790#M55853</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'd like to run a Proc SQL step to link 3 databases. &amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to use the WHERE statement to filter results by subtracting a date one from table from the other.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's what it looks like right now:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql; 
	connect to oracle;
	create table MRSA as select * from connection to oracle 
		(
			select distinct 
				c.MRN,
				a.account_num, a.discharge_date, 
				b.collect_date, a.admit_date
			from table1 a, 
				 table2 b,
				 table3 c
			where a.td_patient_id=b.td_patient_id and
			      b.td_patient_id=c.td_patient_id and
			      0 &amp;lt;= (datepart(b.collect_date) - datepart(a.admit_date)) &amp;lt;= 2 and
			      trunc(a.admit_date, 'DDD') &amp;gt;= '1-Jan-2012' and
			      trunc(a.admit_date, 'DDD') &amp;lt;= '1-Jan-2012' 
);
	disconnect from oracle; 
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm receiving this error when running:&lt;/P&gt;&lt;P&gt;ORACLE prepare error&lt;BR /&gt;ORA-00933: SQL command not properly ended&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for the help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jan 2016 19:40:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Subtracting-dates-in-WHERE-step-of-PROC-SQL/m-p/242790#M55853</guid>
      <dc:creator>malmario</dc:creator>
      <dc:date>2016-01-11T19:40:40Z</dc:date>
    </item>
    <item>
      <title>Re: Subtracting dates in WHERE step of PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Subtracting-dates-in-WHERE-step-of-PROC-SQL/m-p/242796#M55854</link>
      <description>Is that the correct usage of DATEPART() function in Oracle?</description>
      <pubDate>Mon, 11 Jan 2016 19:52:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Subtracting-dates-in-WHERE-step-of-PROC-SQL/m-p/242796#M55854</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-01-11T19:52:35Z</dc:date>
    </item>
    <item>
      <title>Re: Subtracting dates in WHERE step of PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Subtracting-dates-in-WHERE-step-of-PROC-SQL/m-p/242798#M55855</link>
      <description>&lt;P&gt;Ah good point.&amp;nbsp; I tried using the trunc function instead but got the same error &lt;img id="smileysad" class="emoticon emoticon-smileysad" src="https://communities.sas.com/i/smilies/16x16_smiley-sad.png" alt="Smiley Sad" title="Smiley Sad" /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql; 
	connect to oracle;
	create table MRSA as select * from connection to oracle 
		(
			select distinct 
				c.MRN,
				a.account_num, a.discharge_date, 
				b.collect_date, a.admit_date
			from table1 a, 
				 table2 b,
				 table3 c
			where a.td_patient_id=b.td_patient_id and
			      b.td_patient_id=c.td_patient_id and
			      0 &amp;lt;= (trunc(b.collect_date, 'DDD') - trunc(a.admit_date, 'DDD')) &amp;lt;= 2 and
			      trunc(a.admit_date, 'DDD') &amp;gt;= '1-Jan-2012' and
			      trunc(a.admit_date, 'DDD') &amp;lt;= '1-Jan-2012' 
);
	disconnect from oracle; 
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 11 Jan 2016 20:04:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Subtracting-dates-in-WHERE-step-of-PROC-SQL/m-p/242798#M55855</guid>
      <dc:creator>malmario</dc:creator>
      <dc:date>2016-01-11T20:04:47Z</dc:date>
    </item>
    <item>
      <title>Re: Subtracting dates in WHERE step of PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Subtracting-dates-in-WHERE-step-of-PROC-SQL/m-p/242812#M55861</link>
      <description>&lt;P&gt;To narrow down the cause of the error, I would try to remove more and more parts of the query (beginning with the WHERE clause) until the error disappears. Then reconstruct the original query in even smaller steps, until the error reappears. (If it doesn't reappear, you're lucky.) Of course, you have to make sure that each of the reduced queries still makes sense.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;I think, at least one simplification could be done anyway: Aren't the last two lines of the WHERE clause equivalent to &lt;FONT face="courier new,courier"&gt;trunc(&lt;/FONT&gt;...&lt;FONT face="courier new,courier"&gt;)='&lt;/FONT&gt;...&lt;FONT face="courier new,courier"&gt;'&lt;/FONT&gt; or is Oracle's logic at odds with mathematics?&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jan 2016 21:03:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Subtracting-dates-in-WHERE-step-of-PROC-SQL/m-p/242812#M55861</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-01-11T21:03:38Z</dc:date>
    </item>
    <item>
      <title>Re: Subtracting dates in WHERE step of PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Subtracting-dates-in-WHERE-step-of-PROC-SQL/m-p/242814#M55862</link>
      <description>&lt;P&gt;I QC'd and the cause of the error is this line:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;0 &amp;lt;= (trunc(b.collect_date, 'DDD') - trunc(a.admit_date, 'DDD')) &amp;lt;= 2&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;When I comment it out, the code runs.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For the last two lines, I changed it just for QC purposes. &amp;nbsp;The actual date range to pull the data will be over a couple of years.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jan 2016 21:10:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Subtracting-dates-in-WHERE-step-of-PROC-SQL/m-p/242814#M55862</guid>
      <dc:creator>malmario</dc:creator>
      <dc:date>2016-01-11T21:10:42Z</dc:date>
    </item>
    <item>
      <title>Re: Subtracting dates in WHERE step of PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Subtracting-dates-in-WHERE-step-of-PROC-SQL/m-p/242818#M55863</link>
      <description>&lt;P&gt;Good. And, in general, Oracle can handle such chains of inequalities (as it is possible in SAS, except in SAS Macro language)?&lt;/P&gt;
&lt;P&gt;Maybe you could try to use the BETWEEN condition&lt;FONT face="courier new,courier"&gt;&amp;nbsp;trunc(...) - trunc(...) between 0 and 2&lt;/FONT&gt; instead (if that is the correct syntax in Oracle). Or something like &lt;FONT face="courier new,courier"&gt;abs(&lt;/FONT&gt;&lt;SPAN&gt;&lt;FONT face="courier new,courier"&gt;trunc(...) - trunc(...) - 1) &amp;lt;= 1&lt;/FONT&gt; (assuming there is an ABS function like the one in SAS).&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jan 2016 21:45:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Subtracting-dates-in-WHERE-step-of-PROC-SQL/m-p/242818#M55863</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-01-11T21:45:35Z</dc:date>
    </item>
    <item>
      <title>Re: Subtracting dates in WHERE step of PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Subtracting-dates-in-WHERE-step-of-PROC-SQL/m-p/242823#M55865</link>
      <description>&lt;P&gt;Try replacing&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token number"&gt;0&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;&amp;lt;=&lt;/SPAN&gt; &lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;trunc&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;b&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;collect_date&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; &lt;SPAN class="token string"&gt;'DDD'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;-&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;trunc&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;a&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;admit_date&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; &lt;SPAN class="token string"&gt;'DDD'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;&amp;lt;=&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;2&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;with&lt;/P&gt;
&lt;P&gt;(trunc(b.collect_date, 'DDD') - trunc(a.admit_date, 'DDD')) &amp;gt;= 0 and&lt;BR /&gt;(trunc(b.collect_date, 'DDD') - trunc(a.admit_date, 'DDD')) &amp;lt;= 2&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;then try using &lt;STRONG&gt;between 0 and 2&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jan 2016 22:14:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Subtracting-dates-in-WHERE-step-of-PROC-SQL/m-p/242823#M55865</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-01-11T22:14:41Z</dc:date>
    </item>
  </channel>
</rss>

