DATA Step, Macro, Functions and more

evaluate if observations is two time in the dataset

Accepted Solution Solved
Reply
Contributor
Posts: 23
Accepted Solution

evaluate if observations is two time in the dataset

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?


Accepted Solutions
Solution
‎11-13-2016 02:39 AM
Super User
Posts: 9,682

Re: evaluate if observations is two time in the dataset


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


All Replies
PROC Star
Posts: 552

Re: evaluate if observations is two time in the dataset

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;
PROC Star
Posts: 552

Re: evaluate if observations is two time in the dataset

Or do you need to delete both Obs1 observations?

PROC Star
Posts: 552

Re: evaluate if observations is two time in the dataset

In that case

 

data want;
   set have;
   by _LABEL_;
   if first._LABEL_ = last._LABEL_;
run;
Super User
Posts: 6,946

Re: evaluate if observations is two time in the dataset


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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super Contributor
Posts: 305

Re: evaluate if observations is two time in the dataset

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;
Solution
‎11-13-2016 02:39 AM
Super User
Posts: 9,682

Re: evaluate if observations is two time in the dataset


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;
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 231 views
  • 0 likes
  • 5 in conversation