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! 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 1077 views
  • 4 likes
  • 5 in conversation