BookmarkSubscribeRSS Feed
Jessica98
Calcite | Level 5

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

5 REPLIES 5
Scott_Mitchell
Quartz | Level 8

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;

Jessica98
Calcite | Level 5

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

Scott_Mitchell
Quartz | Level 8

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;

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

Something like (not tested):

proc sql;

     create table WANT as

     select     *

     from      HAVE

     having count(ID) <= 3;

quit;

slchen
Lapis Lazuli | Level 10

proc sql;

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

quit;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 2357 views
  • 3 likes
  • 4 in conversation