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


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

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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;

View solution in original post

5 REPLIES 5
art297
Opal | Level 21

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;

Saravanan
Fluorite | Level 6

Thank you very much for the reply. Much appreciated.

Saravanan

Reeza
Super User

data want;

set have;

by id;

if first.id then flag=0;

if flag=1 then delete;

if entry=1 then flag=1;

run;

Saravanan
Fluorite | Level 6

Thank you very much for the reply.

Saravanan

Haikuo
Onyx | Level 15

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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
  • 664 views
  • 0 likes
  • 4 in conversation