BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
AmirSari
Quartz | Level 8

Hi 

 

I am trying to delete the first observations by group. Here is a sample of my dataset.

 

IDyear
11995
11995
11996
11996
11997
11997
11997
22000
22000
22000
22000
22001
22001
22001
22001

 

What I want:

IDyear
11996
11996
11997
11997
11997
22001
22001
22001
22001

 

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

Assuming the data is sorted by ID and year:

data want;
  set have;
  by  id;

  if not first.id;
run;

Code is untested.

EDIT:

The code won't create the requested dataset, i must have skipped that id and year combined identify groups. Please try:

data want;
   set have;
   by id;
   
   retain drop_year;
   drop drop_year;
   
   if first.id then drop_year = year;
   if year ^= drop_year;
run;

 

View solution in original post

5 REPLIES 5
andreas_lds
Jade | Level 19

Assuming the data is sorted by ID and year:

data want;
  set have;
  by  id;

  if not first.id;
run;

Code is untested.

EDIT:

The code won't create the requested dataset, i must have skipped that id and year combined identify groups. Please try:

data want;
   set have;
   by id;
   
   retain drop_year;
   drop drop_year;
   
   if first.id then drop_year = year;
   if year ^= drop_year;
run;

 

PeterClemmensen
Tourmaline | Level 20
data have;
input ID year;
datalines;
1 1995
1 1995
1 1996
1 1996
1 1997
1 1997
1 1997
2 2000
2 2000
2 2000
2 2000
2 2001
2 2001
2 2001
2 2001
;

data want;
   set have;
   by id;
   if first.id then _iorc_ = year;
   if year ne _iorc_;
run;

Result:

 

ID year 
1  1996 
1  1996 
1  1997 
1  1997 
1  1997 
2  2001 
2  2001 
2  2001 
2  2001 
Ksharp
Super User
data have;
input ID year;
datalines;
1 1995
1 1995
1 1996
1 1996
1 1997
1 1997
1 1997
2 2000
2 2000
2 2000
2 2000
2 2001
2 2001
2 2001
2 2001
;
data want;
 set have;
 by id year;
 if first.id then n=0;
 n+first.year;
 if n ne 1;
run;
novinosrin
Tourmaline | Level 20

data have;
input ID year;
datalines;
1 1995
1 1995
1 1996
1 1996
1 1997
1 1997
1 1997
2 2000
2 2000
2 2000
2 2000
2 2001
2 2001
2 2001
2 2001
;

proc sql;
 create table want as
 select * 
 from have
 group by id
 having min(year) ne year
 order by id,year;
quit;
AmirSari
Quartz | Level 8

Thanks everyone for your help! 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 5 replies
  • 1794 views
  • 4 likes
  • 5 in conversation