BookmarkSubscribeRSS Feed
gaurav85
Fluorite | Level 6

There is a table with name and year,  I want to fetch the name of the person who won consecutive award three years via SQL query.

Below is the result set.

gaurav85_1-1643042551333.png

 

7 REPLIES 7
Reeza
Super User

Please post your data as text and include anything you've tried so far. 

Happy to help but not really interested in typing out your data. 

 

Ideally you would post it as a data step but for your first few even just text data is helpful. 

 


@gaurav85 wrote:

There is a table with name and year,  I want to fetch the name of the person who won consecutive award three years via SQL query.

Below is the result set.

gaurav85_1-1643042551333.png

 


 

gaurav85
Fluorite | Level 6

Here is the data

Name Year
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

output

 


Name
Jack
Madan
Rahul

 

Reeza
Super User

And anything you've tried so far?

 

And why the SQL limitation? This is trivial in a data step whereas it's much more complicated in SQL. 

 

 

gaurav85
Fluorite | Level 6

Hi Reeza,

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

LinusH
Tourmaline | Level 20

Forgive me, but this sounds kind of fabricated task, is this a home assignment?

Otherwise the SQL requirement doesn't make sense.

As @Reeza states, the data step is a far better solution for this task, not sure that you even can accomplish this with reasonable effort in SAS 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...?

Data never sleeps
Kurt_Bremser
Super User

If you are working with SAS, the data step is always available, so the SQL requirement makes NO sense AT ALL.

If this needs to be done on a DBMS, that DBMS's SQL will most probably provide a tool to work with sequences.

 

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:

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;

This will, of course, not perform as well as a data step because of the necessary internal sort.

I ran a test with some larger data (200k+ names), the SQL needed 6 seconds, a data step less than 1.

Ksharp
Super User

As Reeza said why SQL, pick up the right tool .

 

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;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 1969 views
  • 4 likes
  • 5 in conversation