Deleting single ID rows

Reply
Contributor
Posts: 30

Deleting single ID rows

Hi guys,

I have a very big dataset, i know hoe to delete rows based on certain values, but i want to know how to delete specific rows. For example take the following table below,

IDtimeincome
11..
12..
13..
21..
22..
23..
31..
41..
42..
43..
51..
61..
62..
63..

I want to be able to delete rows where ID has only one time value, i.e if driver_id occurs only once in the data, i want to delet that row! please advice!

jessica

Super Contributor
Posts: 297

Re: Deleting single ID rows

Use by group processing with if first.id and last.id then delete.

proc sort data=have;

by id;

run;

data want;

set have;

by id;

if first.id and last.id then delete;

run;

Contributor
Posts: 30

Re: Deleting single ID rows

Hi Scott,

This i could use if i want to remove ID's that have only one row, but i need to have a condition which says , if one particular ID has less than 3 rows (i.e repeated thrice in the ID column) then i want the ID and its associated rows out of the sample.

jessica

Super Contributor
Posts: 297

Re: Deleting single ID rows

Sorry I must have got confused about what you were looking for.  I thought you wanted only ID's with one OBS.

How about this DOW loop example?  Just customize the output statement to alter the number of obs you want to keep or remove, which will be contained in the count variable.

DATA WANT;

  DO UNTIL (LAST.ID);

  SET HAVE;

  BY ID;

  IF FIRST.ID THEN COUNT = 1;

  ELSE COUNT + 1;

  END;

  DO UNTIL (LAST.ID);

  SET HAVE;

  BY ID;

  IF COUNT ~= 2 THEN OUTPUT;

  END;

RUN;

Super User
Super User
Posts: 7,401

Re: Deleting single ID rows

Hi,

Something like (not tested):

proc sql;

     create table WANT as

     select     *

     from      HAVE

     having count(ID) <= 3;

quit;

Super Contributor
Posts: 275

Re: Deleting single ID rows

proc sql;

select * from have group by id having count(*)<=3;

quit;

Ask a Question
Discussion stats
  • 5 replies
  • 277 views
  • 3 likes
  • 4 in conversation