🔒 This topic is solved and locked.
Need further help from the community? Please
sign in and ask a new question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Posted 08-17-2020 04:32 AM
(1541 views)
Hi
I am trying to delete the first observations by group. Here is a sample of my dataset.
ID | year |
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 |
What I want:
ID | year |
1 | 1996 |
1 | 1996 |
1 | 1997 |
1 | 1997 |
1 | 1997 |
2 | 2001 |
2 | 2001 |
2 | 2001 |
2 | 2001 |
Thank you!
1 ACCEPTED SOLUTION
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
5 REPLIES 5
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thanks everyone for your help!