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

Is there an easy one (PROC or DATA) step method of sorting and deleting ALL observations that have duplicate by variables INCLUDING the first occurrence of the duplicate? I can do this with the following code, but is there an elegant one step way of doing this? May be with PROC SQL? The following code removes four obs from the "dirty" data set (2 obs have duplicate BY variables hence 2*2=4), leaving only three obs in the final "cleaned" data set.

data dirty;

input var1 var2 x;

cards;

1 1 4

1 2 5

1 3 6

1 1 5

2 2 5

2 4 6

2 2 1

; run;

proc sort data=dirty out=cleaner dupout=dirt nodupkey;

  by var1 var2; run;

data cleaned; merge cleaner dirt (keep=var1 var2 in=del);

  by var1 var2;

  if del then delete; run;

Thanks

Ian.

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

And for a SQL solution :

data dirty;
input var1 var2 x;
cards;
1 1 4
1 2 5
1 3 6
1 1 5
2 2 5
2 4 6
2 2 1
;

proc sql;
create table clean as
select * from dirty
group by var1, var2
having count(*)=1;
quit;

PG

PG

View solution in original post

3 REPLIES 3
PGStats
Opal | Level 21

A simpler non-sql solution :

data dirty;
input var1 var2 x;
cards;
1 1 4
1 2 5
1 3 6
1 1 5
2 2 5
2 4 6
2 2 1
;

proc sort data=dirty; by var1 var2; run;

data clean;
set dirty;
by var1 var2;
if first.var2 and last.var2;
run;

PG

PG
PGStats
Opal | Level 21

And for a SQL solution :

data dirty;
input var1 var2 x;
cards;
1 1 4
1 2 5
1 3 6
1 1 5
2 2 5
2 4 6
2 2 1
;

proc sql;
create table clean as
select * from dirty
group by var1, var2
having count(*)=1;
quit;

PG

PG
iank131
Quartz | Level 8

Thanks very much PG!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 3 replies
  • 791 views
  • 4 likes
  • 2 in conversation