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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 3353 views
  • 3 likes
  • 4 in conversation