<?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: DIF by ID sql in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/DIF-by-ID-sql/m-p/790932#M253266</link>
    <description>You may want to consider learning PL/SQL if you have Oracle and want similar functionality in SQL. &lt;BR /&gt;&lt;BR /&gt;To do this type of calculation you would need different logic but if you had SAS dates it would be easier. &lt;BR /&gt;&lt;BR /&gt;</description>
    <pubDate>Wed, 19 Jan 2022 16:09:49 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2022-01-19T16:09:49Z</dc:date>
    <item>
      <title>DIF by ID sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/DIF-by-ID-sql/m-p/790897#M253251</link>
      <description>&lt;P&gt;I would like to calculate difference within ID. I have a below SAS code, how can I change this to SQL?&lt;/P&gt;
&lt;P&gt;data tmp1;&lt;/P&gt;
&lt;P&gt;set tmp;&lt;/P&gt;
&lt;P&gt;by ID;&lt;/P&gt;
&lt;P&gt;dif_day=dif(day);&lt;/P&gt;
&lt;P&gt;if first.id then dif_day=.;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;input id day ;&lt;BR /&gt;cards;&lt;BR /&gt;1 1 &lt;BR /&gt;1 1 &lt;BR /&gt;2 4 &lt;BR /&gt;2 4 &lt;BR /&gt;2 5 &lt;BR /&gt;3 3 &lt;BR /&gt;3 3 &lt;BR /&gt;3 3 &lt;BR /&gt;3 4 &lt;BR /&gt;3 4 &lt;BR /&gt;;&lt;BR /&gt;data want;&lt;BR /&gt;set have;&lt;BR /&gt;by id;&lt;BR /&gt;dif_day=dif(day);&lt;BR /&gt;if first.id then dif_day=.;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you.&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jan 2022 15:53:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/DIF-by-ID-sql/m-p/790897#M253251</guid>
      <dc:creator>Emma2021</dc:creator>
      <dc:date>2022-01-19T15:53:58Z</dc:date>
    </item>
    <item>
      <title>Re: DIF by ID sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/DIF-by-ID-sql/m-p/790922#M253262</link>
      <description>&lt;P&gt;There is basically no guarantee that SQL will process records in any given order so the concept of DIF (and closely related LAG) functions is basically a non-starter. I won't say it can't be done but there can be considerably more work needed. Use a data step as that is the correct tool if you need to process data in a given order.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also SQL will not have anything related to the FIRST. or LAST. functionality.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/381519"&gt;@Emma2021&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I would like to calculate difference within ID. I have a below SAS code, how can I change this to SQL?&lt;/P&gt;
&lt;P&gt;data tmp1;&lt;/P&gt;
&lt;P&gt;set tmp;&lt;/P&gt;
&lt;P&gt;by ID;&lt;/P&gt;
&lt;P&gt;dif_day=dif(day);&lt;/P&gt;
&lt;P&gt;if first.id then dif_day=.;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jan 2022 15:43:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/DIF-by-ID-sql/m-p/790922#M253262</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-01-19T15:43:02Z</dc:date>
    </item>
    <item>
      <title>Re: DIF by ID sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/DIF-by-ID-sql/m-p/790925#M253263</link>
      <description>&lt;P&gt;If you give us example data in usable form, we might be able to give you an example of SQL code, but it won't look good, and the performance may well be horrible.&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jan 2022 15:50:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/DIF-by-ID-sql/m-p/790925#M253263</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-01-19T15:50:06Z</dc:date>
    </item>
    <item>
      <title>Re: DIF by ID sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/DIF-by-ID-sql/m-p/790932#M253266</link>
      <description>You may want to consider learning PL/SQL if you have Oracle and want similar functionality in SQL. &lt;BR /&gt;&lt;BR /&gt;To do this type of calculation you would need different logic but if you had SAS dates it would be easier. &lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 19 Jan 2022 16:09:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/DIF-by-ID-sql/m-p/790932#M253266</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-01-19T16:09:49Z</dc:date>
    </item>
    <item>
      <title>Re: DIF by ID sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/DIF-by-ID-sql/m-p/791130#M253351</link>
      <description>&lt;P&gt;It is not easy for SQL , you need a index variable to identity a unique row.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
input id day ;
n+1;
cards;
1 1
1 1
2 4
2 4
2 5
3 3
3 3
3 3
3 4
3 4
;

proc sql;
create table want as
select id,day,day-(select day from have where id=a.id and n=a.n-1) as dif
 from have as a;
quit;&lt;/PRE&gt;</description>
      <pubDate>Thu, 20 Jan 2022 12:39:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/DIF-by-ID-sql/m-p/791130#M253351</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-01-20T12:39:43Z</dc:date>
    </item>
  </channel>
</rss>

