Solved
Contributor
Posts: 60

# Deletion of rows

Hi,

I have the following table grouped by ID.

I have to keep all the rows till 1 is encounted in Entry variable for first time and delete the remaining rows which follows afterwards for that particular ID.

For example in the below table, value 1 for variable Entry is encountered for first time in 3rd row for the ID value 10, so delete the rows after that for the corresponding ID i.e delete rows4,5,6.

for ID value 11, Entry value 1 is encountered first time in 9th row. So, delete rows after that, i.e rows 10&11 should be deleted. Thanks. your help is much appreciated.

RowNo      ID          Entry

1                  10               2

2                  10               2

3                  10               1

4                  10               1

5                  10               2

6                  10               1

7                  11               2

8                  11               2

9                  11               1

10                11               2

11                11               1

Thanks,

Saravanan

Accepted Solutions
Solution
‎10-10-2012 12:32 PM
PROC Star
Posts: 8,167

## Re: Deletion of rows

One way would be:

data want (drop=_;

set have;

by id;

retain _keep;

if first.id then _keep=1;

if _keep then output;

if entry eq 1 then _keep=0;

run;

All Replies
Solution
‎10-10-2012 12:32 PM
PROC Star
Posts: 8,167

## Re: Deletion of rows

One way would be:

data want (drop=_;

set have;

by id;

retain _keep;

if first.id then _keep=1;

if _keep then output;

if entry eq 1 then _keep=0;

run;

Contributor
Posts: 60

## Re: Deletion of rows

Thank you very much for the reply. Much appreciated.

Saravanan

Super User
Posts: 23,773

## Re: Deletion of rows

data want;

set have;

by id;

if first.id then flag=0;

if flag=1 then delete;

if entry=1 then flag=1;

run;

Contributor
Posts: 60

## Re: Deletion of rows

Thank you very much for the reply.

Saravanan

Posts: 3,167

## Re: Deletion of rows

Since conveniently you have rowno, SQL can also be an option:

proc sql;

select a.* from have a,

(select id, min(rowno) as mrow from have group by id ,entry having entry=1) b

where a.id=b.id and a.rowno<=b.mrow

;

quit;

Haikuo

🔒 This topic is solved and locked.