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

Hi, I'm obtained a error because my dataset have two equal observations. I need to delete it is thats the case. 

 

Example 

 

_LABEL_ X1 X2 X3 

 

Obs1       1   2   3 

Obs2       4   5   6 

Obs1       7    8  9 

Obs3       4    5  6 

 

I need to delete Obs1 because its repeat 

 

Any idea?

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

data have;
   input _LABEL_ $ X1 X2 X3;
   datalines;
Obs1 1 2 3 
Obs2 4 5 6 
Obs1 7 8 9 
Obs3 4 5 6 
;
run;

data want;
if _n_=1 then do;
 if 0 then set have;
 	declare hash h();
	h.definekey('_LABEL_');
	h.definedone();
end;
set have ;
if h.check()=0 then delete;
 else h.add();
run;

View solution in original post

6 REPLIES 6
PeterClemmensen
Tourmaline | Level 20

Like this?

 

data have;
   input _LABEL_ $ X1 X2 X3;
   datalines;
Obs1 1 2 3 
Obs2 4 5 6 
Obs1 7 8 9 
Obs3 4 5 6 
;

proc sort data = have;
   by _LABEL_;
run;

data want;
   set have;
   by _LABEL_;
   if first._LABEL_;
run;
PeterClemmensen
Tourmaline | Level 20

Or do you need to delete both Obs1 observations?

PeterClemmensen
Tourmaline | Level 20

In that case

 

data want;
   set have;
   by _LABEL_;
   if first._LABEL_ = last._LABEL_;
run;
Kurt_Bremser
Super User

@mariange8282 wrote:

Hi, I'm obtained a error because my dataset have two equal observations. I need to delete it is thats the case. 

 

Example 

 

_LABEL_ X1 X2 X3 

 

Obs1       1   2   3 

Obs2       4   5   6 

Obs1       7    8  9 

Obs3       4    5  6 

 

I need to delete Obs1 because its repeat 

 

Any idea?


Since the "Obs1" observations contain different data in the other columns, the question arises: which values need to be kept?

Simply sorting away with nodupkey might lose valuable information.

Loko
Barite | Level 11

Hello,

 


data have;
   input _LABEL_ $ X1 X2 X3;
   datalines;
Obs1 1 2 3 
Obs2 4 5 6 
Obs1 7 8 9 
Obs3 4 5 6 
;
run;

data want;
if _n_=1 then
 do;
 	declare hash h(dataset:"have", multidata :'Y');
	h.definekey('_LABEL_');
	h.definedata(all:'YES');
	h.definedone();
 end;

do until (last);
  set have end=last;
  rc=h.find();
  if h.find_next() ne 0 then output;
end;

drop rc;
run;
Ksharp
Super User

data have;
   input _LABEL_ $ X1 X2 X3;
   datalines;
Obs1 1 2 3 
Obs2 4 5 6 
Obs1 7 8 9 
Obs3 4 5 6 
;
run;

data want;
if _n_=1 then do;
 if 0 then set have;
 	declare hash h();
	h.definekey('_LABEL_');
	h.definedone();
end;
set have ;
if h.check()=0 then delete;
 else h.add();
run;

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
  • 6 replies
  • 1283 views
  • 0 likes
  • 5 in conversation