SAS Programming

DATA Step, Macro, Functions and more
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-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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