<?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: PROC SQL, Recursive Common Table Expression, dealing with consecutive days in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/PROC-SQL-Recursive-Common-Table-Expression-dealing-with/m-p/721217#M27772</link>
    <description>&lt;P&gt;Start with this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create view v_cte as
  select a.employmentid, a.startdate, a.enddate
  from Tbl a
  left join Tbl b on a.employmentid=b.employmentid and a.startdate-1=b.enddate
  where b.employmentid is null
  union all
  select a.employmentid, a.startdate, b.enddate
     from cte a
     join Tbl b on a.employmentid=b.employmentid and b.startdate-1=a.enddate
;
create table want as
  select
    employmentid,
    startdate,
    enddate
  from v_cte
  group by employmentid, startdate
  order by employmentid
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 23 Feb 2021 09:24:39 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2021-02-23T09:24:39Z</dc:date>
    <item>
      <title>PROC SQL, Recursive Common Table Expression, dealing with consecutive days</title>
      <link>https://communities.sas.com/t5/New-SAS-User/PROC-SQL-Recursive-Common-Table-Expression-dealing-with/m-p/721123#M27764</link>
      <description>&lt;P&gt;Hello Community,&lt;/P&gt;&lt;P&gt;Trying something new where I am reusing the 1st solution from the following:&amp;nbsp;&lt;A href="https://stackoverflow.com/questions/15783315/combine-consecutive-date-ranges" target="_blank"&gt;https://stackoverflow.com/questions/15783315/combine-consecutive-date-ranges&lt;/A&gt;&amp;nbsp;by modifying it to be in PROC SQL instead of SQL Server. This is what they have:&lt;/P&gt;&lt;PRE class="lang-sql s-code-block hljs"&gt;&lt;CODE&gt;;&lt;SPAN class="hljs-keyword"&gt;with&lt;/SPAN&gt; cte &lt;SPAN class="hljs-keyword"&gt;as&lt;/SPAN&gt; (
   &lt;SPAN class="hljs-keyword"&gt;select&lt;/SPAN&gt; a.employmentid, a.startdate, a.enddate
     &lt;SPAN class="hljs-keyword"&gt;from&lt;/SPAN&gt; Tbl a
&lt;SPAN class="hljs-keyword"&gt;left&lt;/SPAN&gt; &lt;SPAN class="hljs-keyword"&gt;join&lt;/SPAN&gt; Tbl b &lt;SPAN class="hljs-keyword"&gt;on&lt;/SPAN&gt; a.employmentid&lt;SPAN class="hljs-operator"&gt;=&lt;/SPAN&gt;b.employmentid &lt;SPAN class="hljs-keyword"&gt;and&lt;/SPAN&gt; a.startdate&lt;SPAN class="hljs-number"&gt;-1&lt;/SPAN&gt;&lt;SPAN class="hljs-operator"&gt;=&lt;/SPAN&gt;b.enddate
    &lt;SPAN class="hljs-keyword"&gt;where&lt;/SPAN&gt; b.employmentid &lt;SPAN class="hljs-keyword"&gt;is&lt;/SPAN&gt; &lt;SPAN class="hljs-keyword"&gt;null&lt;/SPAN&gt;
    &lt;SPAN class="hljs-keyword"&gt;union&lt;/SPAN&gt; &lt;SPAN class="hljs-keyword"&gt;all&lt;/SPAN&gt;
   &lt;SPAN class="hljs-keyword"&gt;select&lt;/SPAN&gt; a.employmentid, a.startdate, b.enddate
     &lt;SPAN class="hljs-keyword"&gt;from&lt;/SPAN&gt; cte a
     &lt;SPAN class="hljs-keyword"&gt;join&lt;/SPAN&gt; Tbl b &lt;SPAN class="hljs-keyword"&gt;on&lt;/SPAN&gt; a.employmentid&lt;SPAN class="hljs-operator"&gt;=&lt;/SPAN&gt;b.employmentid &lt;SPAN class="hljs-keyword"&gt;and&lt;/SPAN&gt; b.startdate&lt;SPAN class="hljs-number"&gt;-1&lt;/SPAN&gt;&lt;SPAN class="hljs-operator"&gt;=&lt;/SPAN&gt;a.enddate
)
   &lt;SPAN class="hljs-keyword"&gt;select&lt;/SPAN&gt; employmentid,
          startdate,
          &lt;SPAN class="hljs-built_in"&gt;nullif&lt;/SPAN&gt;(&lt;SPAN class="hljs-built_in"&gt;max&lt;/SPAN&gt;(isnull(enddate,&lt;SPAN class="hljs-string"&gt;'32121231'&lt;/SPAN&gt;)),&lt;SPAN class="hljs-string"&gt;'32121231'&lt;/SPAN&gt;) enddate
     &lt;SPAN class="hljs-keyword"&gt;from&lt;/SPAN&gt; cte
 &lt;SPAN class="hljs-keyword"&gt;group&lt;/SPAN&gt; &lt;SPAN class="hljs-keyword"&gt;by&lt;/SPAN&gt; employmentid, startdate
 &lt;SPAN class="hljs-keyword"&gt;order&lt;/SPAN&gt; &lt;SPAN class="hljs-keyword"&gt;by&lt;/SPAN&gt; employmentid&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp; This is how I've attempted to modify it:&lt;/P&gt;&lt;PRE&gt;proc sql;
create view cte as (
   select a.employmentid, a.startdate, a.enddate
     from Tbl a
left join Tbl b on a.employmentid=b.employmentid and a.startdate-1=b.enddate
    where b.employmentid is null
    union all
   select a.employmentid, a.startdate, b.enddate
     from cte a
     join Tbl b on a.employmentid=b.employmentid and b.startdate-1=a.enddate
)
   select employmentid,
          startdate,
          nullif(max(isnull(enddate,'32121231')),'32121231') enddate
     from cte
 group by employmentid, startdate
 order by employmentid;
quit;&lt;/PRE&gt;&lt;P&gt;SAS doesn't like the last select piece. "Syntax error, expecting one of the following: EXCEPT, INTERSECT, UNION."&lt;/P&gt;&lt;P&gt;Any ideas as to what I'm doing wrong here? I think it is something to do with not translating SQL's&amp;nbsp; WITH clause correctly.&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Mon, 22 Feb 2021 23:00:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/PROC-SQL-Recursive-Common-Table-Expression-dealing-with/m-p/721123#M27764</guid>
      <dc:creator>iced_tea</dc:creator>
      <dc:date>2021-02-22T23:00:40Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL, Recursive Common Table Expression, dealing with consecutive days</title>
      <link>https://communities.sas.com/t5/New-SAS-User/PROC-SQL-Recursive-Common-Table-Expression-dealing-with/m-p/721127#M27767</link>
      <description>&lt;P&gt;This isn't valid SAS SQL:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;   select employmentid,
          startdate,
          nullif(max(isnull(enddate,'32121231')),'32121231') enddate
     from cte&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I suggest you just try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;   select employmentid,
          startdate,
          enddate
     from cte&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you need to deal with missing values you can always do it afterwards or if necessary use an equivalent CASE statement.&lt;/P&gt;</description>
      <pubDate>Mon, 22 Feb 2021 23:11:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/PROC-SQL-Recursive-Common-Table-Expression-dealing-with/m-p/721127#M27767</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2021-02-22T23:11:57Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL, Recursive Common Table Expression, dealing with consecutive days</title>
      <link>https://communities.sas.com/t5/New-SAS-User/PROC-SQL-Recursive-Common-Table-Expression-dealing-with/m-p/721217#M27772</link>
      <description>&lt;P&gt;Start with this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create view v_cte as
  select a.employmentid, a.startdate, a.enddate
  from Tbl a
  left join Tbl b on a.employmentid=b.employmentid and a.startdate-1=b.enddate
  where b.employmentid is null
  union all
  select a.employmentid, a.startdate, b.enddate
     from cte a
     join Tbl b on a.employmentid=b.employmentid and b.startdate-1=a.enddate
;
create table want as
  select
    employmentid,
    startdate,
    enddate
  from v_cte
  group by employmentid, startdate
  order by employmentid
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 23 Feb 2021 09:24:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/PROC-SQL-Recursive-Common-Table-Expression-dealing-with/m-p/721217#M27772</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-02-23T09:24:39Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL, Recursive Common Table Expression, dealing with consecutive days</title>
      <link>https://communities.sas.com/t5/New-SAS-User/PROC-SQL-Recursive-Common-Table-Expression-dealing-with/m-p/721218#M27773</link>
      <description>&lt;P&gt;In SAS SQL, isnull corresponds to coalesce.&lt;/P&gt;
&lt;P&gt;Max works in SAS.&lt;/P&gt;
&lt;P&gt;nullif can be mimiced as&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13976"&gt;@SASKiwi&lt;/a&gt;&amp;nbsp;stated using case when else logic.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 23 Feb 2021 09:34:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/PROC-SQL-Recursive-Common-Table-Expression-dealing-with/m-p/721218#M27773</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2021-02-23T09:34:14Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL, Recursive Common Table Expression, dealing with consecutive days</title>
      <link>https://communities.sas.com/t5/New-SAS-User/PROC-SQL-Recursive-Common-Table-Expression-dealing-with/m-p/721291#M27778</link>
      <description>Thank you for taking a look. Dropping the nullif piece didn't work as it gives the same error. The last SELECT is underlined and the message reads:&lt;BR /&gt;"Syntax error, expecting one of the following: EXCEPT, INTERSECT, UNION."&lt;BR /&gt;</description>
      <pubDate>Tue, 23 Feb 2021 15:17:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/PROC-SQL-Recursive-Common-Table-Expression-dealing-with/m-p/721291#M27778</guid>
      <dc:creator>iced_tea</dc:creator>
      <dc:date>2021-02-23T15:17:07Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL, Recursive Common Table Expression, dealing with consecutive days</title>
      <link>https://communities.sas.com/t5/New-SAS-User/PROC-SQL-Recursive-Common-Table-Expression-dealing-with/m-p/721310#M27779</link>
      <description>&lt;P&gt;Thank you for taking a look Kurt. This produces the following error:&lt;/P&gt;&lt;P&gt;"The current reference to SQL View WORK.v_cte has exceeded PROC SQL's limit of 50 references&lt;BR /&gt;to a single view in a SQL statement. Please check to make sure that these references are not&lt;BR /&gt;part of a recursive situation."&lt;/P&gt;&lt;P&gt;This means something got stuck in an infinite loop, no?&lt;/P&gt;</description>
      <pubDate>Tue, 23 Feb 2021 15:57:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/PROC-SQL-Recursive-Common-Table-Expression-dealing-with/m-p/721310#M27779</guid>
      <dc:creator>iced_tea</dc:creator>
      <dc:date>2021-02-23T15:57:06Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL, Recursive Common Table Expression, dealing with consecutive days</title>
      <link>https://communities.sas.com/t5/New-SAS-User/PROC-SQL-Recursive-Common-Table-Expression-dealing-with/m-p/721493#M27794</link>
      <description>&lt;P&gt;Make sure that tbl and cte are no views that create a circular reference to themselves.&lt;/P&gt;</description>
      <pubDate>Wed, 24 Feb 2021 08:18:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/PROC-SQL-Recursive-Common-Table-Expression-dealing-with/m-p/721493#M27794</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-02-24T08:18:20Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL, Recursive Common Table Expression, dealing with consecutive days</title>
      <link>https://communities.sas.com/t5/New-SAS-User/PROC-SQL-Recursive-Common-Table-Expression-dealing-with/m-p/721598#M27796</link>
      <description>&lt;P&gt;Thank you. Do you know where in the code it is doing a circular reference?&lt;/P&gt;
&lt;PRE&gt;proc sql;
create view v_cte as
  select a.employmentid, a.startdate, a.enddate
  from Tbl a
  left join Tbl b on a.employmentid=b.employmentid and a.startdate-1=b.enddate
  where b.employmentid is null
  union all
  select a.employmentid, a.startdate, b.enddate
     from cte a
     join Tbl b on a.employmentid=b.employmentid and b.startdate-1=a.enddate
;
create table want as
  select
    employmentid,
    startdate,
    enddate
  from v_cte
  group by employmentid, startdate
  order by employmentid
;
quit;&lt;/PRE&gt;</description>
      <pubDate>Wed, 24 Feb 2021 15:40:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/PROC-SQL-Recursive-Common-Table-Expression-dealing-with/m-p/721598#M27796</guid>
      <dc:creator>iced_tea</dc:creator>
      <dc:date>2021-02-24T15:40:20Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL, Recursive Common Table Expression, dealing with consecutive days</title>
      <link>https://communities.sas.com/t5/New-SAS-User/PROC-SQL-Recursive-Common-Table-Expression-dealing-with/m-p/721606#M27797</link>
      <description>&lt;P&gt;From all I see,&amp;nbsp;&lt;EM&gt;this&lt;/EM&gt; code is not the reason; instead, one of the inputs (CTE and/or TBL) has the issue.&lt;/P&gt;</description>
      <pubDate>Wed, 24 Feb 2021 16:09:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/PROC-SQL-Recursive-Common-Table-Expression-dealing-with/m-p/721606#M27797</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-02-24T16:09:46Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL, Recursive Common Table Expression, dealing with consecutive days</title>
      <link>https://communities.sas.com/t5/New-SAS-User/PROC-SQL-Recursive-Common-Table-Expression-dealing-with/m-p/721658#M27811</link>
      <description>&lt;P&gt;For up to 5 chained periods per sequence, in SAS/SQL you could do (example data from Stackoverflow reference):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines dsd;
input id (dStart dEnd) (:yymmdd10.);
format dStart dEnd yymmdd10.;
datalines;
5, 2007-12-03, 2011-08-26
5, 2013-05-02, .
30, 2006-10-02, 2011-01-16
30, 2011-01-17, 2012-08-12
30, 2012-08-13, .
66, 2007-09-24, .
;

proc sql;
select 
    a.id, 
    a.dStart,
    case 
        when e.id then e.dEnd
        when d.id then d.dEnd
        when c.id then c.dEnd
        when b.id then b.dEnd
        else a.dEnd 
    end as dEnd         format=yymmdd10.
from
    have as a left join
    have as b on a.id=b.id and a.dEnd+1=b.dStart left join
    have as c on a.id=c.id and b.dEnd+1=c.dStart left join
    have as d on a.id=d.id and c.dEnd+1=d.dStart left join
    have as e on a.id=e.id and d.dEnd+1=e.dStart
group by a.id, calculated dEnd
having a.dStart = min(a.dStart) ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="PGStats_0-1614191777122.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/55126iB1E5E936791CA175/image-size/medium?v=v2&amp;amp;px=400" role="button" title="PGStats_0-1614191777122.png" alt="PGStats_0-1614191777122.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 24 Feb 2021 18:39:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/PROC-SQL-Recursive-Common-Table-Expression-dealing-with/m-p/721658#M27811</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2021-02-24T18:39:43Z</dc:date>
    </item>
  </channel>
</rss>

