<?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: SQL in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SQL-Find-Consecutive-Records/m-p/791926#M253736</link>
    <description>&lt;P&gt;Hi Reeza,&lt;/P&gt;&lt;P&gt;I am aware of the data step solution, In this particular case its a explicit ask to use SQL. I am not getting anything as of now in SQL&lt;/P&gt;</description>
    <pubDate>Mon, 24 Jan 2022 17:13:03 GMT</pubDate>
    <dc:creator>gaurav85</dc:creator>
    <dc:date>2022-01-24T17:13:03Z</dc:date>
    <item>
      <title>SQL: Find Consecutive Records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Find-Consecutive-Records/m-p/791905#M253722</link>
      <description>&lt;P&gt;There is a table with name and year,&amp;nbsp; I want to fetch the name of the person who won consecutive award three years via SQL query.&lt;/P&gt;
&lt;P&gt;Below is the result set.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="gaurav85_1-1643042551333.png" style="width: 548px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/67679i3091C0B955C852B4/image-dimensions/548x344?v=v2" width="548" height="344" role="button" title="gaurav85_1-1643042551333.png" alt="gaurav85_1-1643042551333.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 26 Jan 2022 07:12:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Find-Consecutive-Records/m-p/791905#M253722</guid>
      <dc:creator>gaurav85</dc:creator>
      <dc:date>2022-01-26T07:12:09Z</dc:date>
    </item>
    <item>
      <title>Re: SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Find-Consecutive-Records/m-p/791913#M253726</link>
      <description>&lt;P&gt;Please post your data as text and include anything you've tried so far.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Happy to help but not really interested in typing out your data.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Ideally you would post it as a data step but for your first few even just text data is helpful.&amp;nbsp;&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/90568"&gt;@gaurav85&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;There is a table with name and year,&amp;nbsp; I want to fetch the name of the person who won consecutive award three years via SQL query.&lt;/P&gt;
&lt;P&gt;Below is the result set.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="gaurav85_1-1643042551333.png" style="width: 548px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/67679i3091C0B955C852B4/image-dimensions/548x344?v=v2" width="548" height="344" role="button" title="gaurav85_1-1643042551333.png" alt="gaurav85_1-1643042551333.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Jan 2022 16:57:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Find-Consecutive-Records/m-p/791913#M253726</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-01-24T16:57:52Z</dc:date>
    </item>
    <item>
      <title>Re: SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Find-Consecutive-Records/m-p/791922#M253732</link>
      <description>&lt;P&gt;Here is the data&lt;/P&gt;&lt;P&gt;Name Year&lt;BR /&gt;Raj 1999&lt;BR /&gt;Rahul 2000&lt;BR /&gt;Rahul 2001&lt;BR /&gt;Rahul 2002&lt;BR /&gt;Sachin 2003&lt;BR /&gt;Suraj 2004&lt;BR /&gt;Harish 2005&lt;BR /&gt;Nihal 2006&lt;BR /&gt;Jim 2007&lt;BR /&gt;Jack 2008&lt;BR /&gt;Jack 2009&lt;BR /&gt;Jack 2010&lt;BR /&gt;Madan 2011&lt;BR /&gt;Madan 2012&lt;BR /&gt;Madan 2013&lt;/P&gt;&lt;P&gt;output&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Name&lt;BR /&gt;Jack&lt;BR /&gt;Madan&lt;BR /&gt;Rahul&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Jan 2022 17:07:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Find-Consecutive-Records/m-p/791922#M253732</guid>
      <dc:creator>gaurav85</dc:creator>
      <dc:date>2022-01-24T17:07:49Z</dc:date>
    </item>
    <item>
      <title>Re: SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Find-Consecutive-Records/m-p/791925#M253735</link>
      <description>&lt;P&gt;And anything you've tried so far?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And why the SQL limitation? This is trivial in a data step whereas it's much more complicated in SQL.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Jan 2022 17:10:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Find-Consecutive-Records/m-p/791925#M253735</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-01-24T17:10:59Z</dc:date>
    </item>
    <item>
      <title>Re: SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Find-Consecutive-Records/m-p/791926#M253736</link>
      <description>&lt;P&gt;Hi Reeza,&lt;/P&gt;&lt;P&gt;I am aware of the data step solution, In this particular case its a explicit ask to use SQL. I am not getting anything as of now in SQL&lt;/P&gt;</description>
      <pubDate>Mon, 24 Jan 2022 17:13:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Find-Consecutive-Records/m-p/791926#M253736</guid>
      <dc:creator>gaurav85</dc:creator>
      <dc:date>2022-01-24T17:13:03Z</dc:date>
    </item>
    <item>
      <title>Re: SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Find-Consecutive-Records/m-p/792073#M253790</link>
      <description>&lt;P&gt;Forgive me, but this sounds kind of fabricated task, is this a home assignment?&lt;/P&gt;
&lt;P&gt;Otherwise the SQL requirement doesn't make sense.&lt;/P&gt;
&lt;P&gt;As&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;states, the data step is a far better solution for this task, not sure that you even can accomplish this with reasonable effort in &lt;EM&gt;&lt;STRONG&gt;SAS&lt;/STRONG&gt; &lt;/EM&gt;SQL. Without thinking it through I would start by grouping on name, compare min and max year with no records...not sure that would cover different scenarios though...?&lt;/P&gt;</description>
      <pubDate>Tue, 25 Jan 2022 07:26:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Find-Consecutive-Records/m-p/792073#M253790</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2022-01-25T07:26:24Z</dc:date>
    </item>
    <item>
      <title>Re: SQL: Find Consecutive Records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Find-Consecutive-Records/m-p/792129#M253802</link>
      <description>&lt;P&gt;If you are working with SAS, the data step is always available, so the SQL requirement makes NO sense AT ALL.&lt;/P&gt;
&lt;P&gt;If this needs to be done on a DBMS, that DBMS's SQL will most probably provide a tool to work with sequences.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In SAS SQL, you need to join the table with itself on name and (year - 1) and (year - 2) and then check for missing name in the joined tables:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
  select distinct t1.name
  from
    have t1
    left join have t2 on t1.name = t2.name and t2.year = t1.year - 1
    left join have t3 on t1.name = t3.name and t3.year = t1.year - 2
  where not missing(t2.name) and not missing(t3.name)
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This will, of course, not perform as well as a data step because of the necessary internal sort.&lt;/P&gt;
&lt;P&gt;I ran a test with some larger data (200k+ names), the SQL needed 6 seconds, a data step less than 1.&lt;/P&gt;</description>
      <pubDate>Tue, 25 Jan 2022 09:00:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Find-Consecutive-Records/m-p/792129#M253802</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-01-25T09:00:24Z</dc:date>
    </item>
    <item>
      <title>Re: SQL: Find Consqcutive Records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Find-Consecutive-Records/m-p/792170#M253814</link>
      <description>&lt;P&gt;As Reeza said why SQL, pick up the right tool .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Name $ Year;
cards;
Raj 1999
Rahul 2000
Rahul 2001
Rahul 2002
Sachin 2003
Suraj 2004
Harish 2005
Nihal 2006
Jim 2007
Jack 2008
Jack 2009
Jack 2010
Madan 2011
Madan 2012
Madan 2013
;

proc sql;
create table want as
select distinct name
 from have as a
  where (select count(distinct year) from have where name=a.name and year between a.year-2 and a.year)=3 ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 25 Jan 2022 13:09:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Find-Consecutive-Records/m-p/792170#M253814</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-01-25T13:09:35Z</dc:date>
    </item>
  </channel>
</rss>

