<?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 date diff SQL to Sas in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/date-diff-SQL-to-Sas/m-p/20693#M3252</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;A couple of issues I can identify: &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;OL start="1"&gt;&lt;LI&gt;In SAS you just subtract dates, if its date times then you need to take the date part, unless you want the time in seconds or milliseconds, not sure what the smallest unit of date time is. &lt;/LI&gt;&lt;LI&gt;Also there is no CASE statement, there's a SELECT statement, but an if/then is probably what you're looking for. &lt;/LI&gt;&lt;LI&gt;Your SQL statement is merging, but your datastep code is APPENDING the dataset not merging.&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm not 100% sure what you want but the following is a start with the following assumptions:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;date is in datetime &lt;/LI&gt;&lt;LI&gt;you want difference in terms of days&lt;/LI&gt;&lt;LI&gt;Merging files by client id and id&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;*First need to sort both files:&lt;/P&gt;&lt;P&gt;proc sort data=data_333; by client id;&lt;/P&gt;&lt;P&gt;proc sort data=test_data; by client id;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;*then need to merge and get differences;&lt;/P&gt;&lt;P&gt;Data Test;&lt;/P&gt;&lt;P&gt;MERGE data_333 (in=a) test_data (in=b);&lt;/P&gt;&lt;P&gt;by client id;&lt;/P&gt;&lt;P&gt;*get only records from data_333;&lt;/P&gt;&lt;P&gt;if a;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;if datepart(cd_date)-datepart(add_date))&amp;lt;=1 then cd_1=1;&lt;/P&gt;&lt;P&gt;else cd_1=0;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;if datepart(cd_date)-datepart(add_date)&amp;lt;=2 then cd_2=1;&lt;/P&gt;&lt;P&gt;else cd_2=0;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;OR SQL&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;create table test as&lt;/P&gt;&lt;P&gt;select a.add_date&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,b.cd_date&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,case when datepart(b.cd_date)-datepart(a.add_date) &amp;lt;= 1 then 1 else 0 end as cd_1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,case when datepart(b.cd_date)-datepart(a.add_date) &amp;lt;= 1 then 1 else 0 end as cd_2&lt;/P&gt;&lt;P&gt;from data_333 a left join test_data b&lt;/P&gt;&lt;P&gt;on a.client = b.client and a.id = b.id;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 24 Jun 2011 22:25:25 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2011-06-24T22:25:25Z</dc:date>
    <item>
      <title>date diff SQL to Sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/date-diff-SQL-to-Sas/m-p/20692#M3251</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have the code running successful in SQL.&amp;nbsp; but I am having trouble converting into SAS.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;SQL code&lt;/P&gt;&lt;P&gt;cd_1 = case when datediff (d,a.add_date, b.cd_date) ,&amp;lt;= 1 then 1 else 0 end&lt;/P&gt;&lt;P&gt;cd_2 = case when datediff (d,a.add_date, b.cd_date) ,&amp;lt;= 2 then 1 else 0 end&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;sas code&lt;/P&gt;&lt;P&gt;create table test as&lt;/P&gt;&lt;P&gt;select a.add_date&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,b.cd_date&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,cd_1 = case when datediff (d, a.add_date, b.cd_date) &amp;lt;= 1 then 1 else 0 end&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,cd_2 = case when datediff (d, a.add_date, b.cd_date) &amp;lt;= 2 then 1 else 0 end&lt;/P&gt;&lt;P&gt;from data_333 a left join test_data b&lt;/P&gt;&lt;P&gt;on a.client = b.client and a.id = b.id;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;or is it a data step;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data test;&lt;/P&gt;&lt;P&gt;set data_333 test_data;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;cd_1 = case when datediff (d, a.add_date, b.cd_date) , &amp;lt;= 1 then 1 else 0 end&lt;/P&gt;&lt;P&gt;cd_2 = case when datediff (d, a.add_date, b.cd_date) &amp;lt;= 2 then 1 else 0 end&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 24 Jun 2011 19:42:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/date-diff-SQL-to-Sas/m-p/20692#M3251</guid>
      <dc:creator>mick_g</dc:creator>
      <dc:date>2011-06-24T19:42:45Z</dc:date>
    </item>
    <item>
      <title>date diff SQL to Sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/date-diff-SQL-to-Sas/m-p/20693#M3252</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;A couple of issues I can identify: &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;OL start="1"&gt;&lt;LI&gt;In SAS you just subtract dates, if its date times then you need to take the date part, unless you want the time in seconds or milliseconds, not sure what the smallest unit of date time is. &lt;/LI&gt;&lt;LI&gt;Also there is no CASE statement, there's a SELECT statement, but an if/then is probably what you're looking for. &lt;/LI&gt;&lt;LI&gt;Your SQL statement is merging, but your datastep code is APPENDING the dataset not merging.&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm not 100% sure what you want but the following is a start with the following assumptions:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;date is in datetime &lt;/LI&gt;&lt;LI&gt;you want difference in terms of days&lt;/LI&gt;&lt;LI&gt;Merging files by client id and id&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;*First need to sort both files:&lt;/P&gt;&lt;P&gt;proc sort data=data_333; by client id;&lt;/P&gt;&lt;P&gt;proc sort data=test_data; by client id;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;*then need to merge and get differences;&lt;/P&gt;&lt;P&gt;Data Test;&lt;/P&gt;&lt;P&gt;MERGE data_333 (in=a) test_data (in=b);&lt;/P&gt;&lt;P&gt;by client id;&lt;/P&gt;&lt;P&gt;*get only records from data_333;&lt;/P&gt;&lt;P&gt;if a;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;if datepart(cd_date)-datepart(add_date))&amp;lt;=1 then cd_1=1;&lt;/P&gt;&lt;P&gt;else cd_1=0;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;if datepart(cd_date)-datepart(add_date)&amp;lt;=2 then cd_2=1;&lt;/P&gt;&lt;P&gt;else cd_2=0;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;OR SQL&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;create table test as&lt;/P&gt;&lt;P&gt;select a.add_date&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,b.cd_date&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,case when datepart(b.cd_date)-datepart(a.add_date) &amp;lt;= 1 then 1 else 0 end as cd_1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,case when datepart(b.cd_date)-datepart(a.add_date) &amp;lt;= 1 then 1 else 0 end as cd_2&lt;/P&gt;&lt;P&gt;from data_333 a left join test_data b&lt;/P&gt;&lt;P&gt;on a.client = b.client and a.id = b.id;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 24 Jun 2011 22:25:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/date-diff-SQL-to-Sas/m-p/20693#M3252</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2011-06-24T22:25:25Z</dc:date>
    </item>
    <item>
      <title>date diff SQL to Sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/date-diff-SQL-to-Sas/m-p/20694#M3253</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;SAS doesn't have the DATEDIFF function. See INTCK instead. Use DTDAY as the interval if your values are datetimes rather than dates.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;PROC SQL and the DATA step are both suitable, but you can't mix and match. CASE is part of SQL and can't be used in the DATA step.&lt;/P&gt;&lt;PRE __jive_macro_name="quote" class="jive_text_macro jive_macro_quote"&gt;&lt;P&gt;mick_g wrote:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have the code running successful in SQL.&amp;nbsp; but I am having trouble converting into SAS.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;SQL code&lt;/P&gt;&lt;P&gt;cd_1 = case when datediff (d,a.add_date, b.cd_date) ,&amp;lt;= 1 then 1 else 0 end&lt;/P&gt;&lt;P&gt;cd_2 = case when datediff (d,a.add_date, b.cd_date) ,&amp;lt;= 2 then 1 else 0 end&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;sas code&lt;/P&gt;&lt;P&gt;create table test as&lt;/P&gt;&lt;P&gt;select a.add_date&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,b.cd_date&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,cd_1 = case when datediff (d, a.add_date, b.cd_date) &amp;lt;= 1 then 1 else 0 end&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,cd_2 = case when datediff (d, a.add_date, b.cd_date) &amp;lt;= 2 then 1 else 0 end&lt;/P&gt;&lt;P&gt;from data_333 a left join test_data b&lt;/P&gt;&lt;P&gt;on a.client = b.client and a.id = b.id;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;or is it a data step;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data test;&lt;/P&gt;&lt;P&gt;set data_333 test_data;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;cd_1 = case when datediff (d, a.add_date, b.cd_date) , &amp;lt;= 1 then 1 else 0 end&lt;/P&gt;&lt;P&gt;cd_2 = case when datediff (d, a.add_date, b.cd_date) &amp;lt;= 2 then 1 else 0 end&lt;/P&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 24 Jun 2011 22:26:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/date-diff-SQL-to-Sas/m-p/20694#M3253</guid>
      <dc:creator>Howles</dc:creator>
      <dc:date>2011-06-24T22:26:47Z</dc:date>
    </item>
    <item>
      <title>date diff SQL to Sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/date-diff-SQL-to-Sas/m-p/20695#M3254</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt; Hi&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You can do it both in SQL or SAS code. Below the SAS SQL version of your code (untested).&lt;/P&gt;&lt;P&gt; &lt;BR /&gt;proc sql;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; create table test as&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; select a.add_date&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,b.cd_date&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,case&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; when(intck('day',a.add_date, b.cd_date)&amp;lt;=1) then 1&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else 0&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end as cd_1&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,case&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; when(intck('day',a.add_date, b.cd_date)&amp;lt;=2) then 1&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else 0&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end as cd_1&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; from data_333 a left join test_data b&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; on a.client = b.client and a.id = b.id;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;intck(): 'day' for SAS date values, 'dtdate' for SAS datetime values&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You could of course also just paste your existing SQL code into a pass-through SQL block and send it directly to the database.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If your data is in a database then use SQL before data step in order to have all the processing done in the data base.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;HTH&lt;/P&gt;&lt;P&gt;Patrick &lt;/P&gt;&lt;DIV class="mcePaste" id="_mcePaste" style="position: absolute; width: 1px; height: 1px; overflow: hidden; top: 0px; left: -10000px;"&gt;﻿&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 25 Jun 2011 00:02:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/date-diff-SQL-to-Sas/m-p/20695#M3254</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2011-06-25T00:02:25Z</dc:date>
    </item>
  </channel>
</rss>

