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


Hello!

I would like to filter the duplicate records of data set A, this is I would like to kick out observations with Nr >=2 -  If (EoF) & (Nr ge 2) Then H.Output(Dataset:"Duplicates"); doesn't work.

The following program works but keeps single observations:

Data A;
  Do i=1 To 30;
    ID=Byte(Int(RanUni(1)*26)+65);
    Output;
  End;
Run;

Data _NULL_;
  Length Nr 3.;
  If _N_ eq 1 Then Do;
    Declare Hash H();
H.DefineKey("ID");
H.DefineData("ID", "i", "Nr");
H.DefineDone();
  End;
  Set A End=EoF;
  If H.Find() ne 0 Then Do;
    Nr=1;
H.Add();
  End;
  Else Do;
    Nr+1;
H.Replace();
  End;
  If EoF Then H.Output(Dataset:"Duplicates");
Run;

My 2nd question is, how can I find duplicate records (not count them) of dataset "A" using a hash object?

Thanks&kind regards

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

If I understood what you mean.



 
Data A;
  Do i=1 To 30;
    ID=Byte(Int(RanUni(1)*26)+65);
    Output;
  End;
Run;
data _null_;
 if _n_ eq 1 then do;
  if 0 then set a;
  declare hash h();
  h.definekey('id');
  h.definedata('id','n');
  h.definedone();
end;
set a end=last;
if h.find()=0 then do;n+1;h.replace();end;
 else do;n=1;h.replace();end;
if last then do;
 h.output(dataset:'singual(where=(n=1))');
 h.output(dataset:'duplicate(where=(n gt 1))');
end;
run;

Xia Keshan

View solution in original post

2 REPLIES 2
Ksharp
Super User

If I understood what you mean.



 
Data A;
  Do i=1 To 30;
    ID=Byte(Int(RanUni(1)*26)+65);
    Output;
  End;
Run;
data _null_;
 if _n_ eq 1 then do;
  if 0 then set a;
  declare hash h();
  h.definekey('id');
  h.definedata('id','n');
  h.definedone();
end;
set a end=last;
if h.find()=0 then do;n+1;h.replace();end;
 else do;n=1;h.replace();end;
if last then do;
 h.output(dataset:'singual(where=(n=1))');
 h.output(dataset:'duplicate(where=(n gt 1))');
end;
run;

Xia Keshan

user24feb
Barite | Level 11

Yes, that's exactly what a meant. I didn't think to put a where-statement after the dataset. Many thanks!

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
  • 2 replies
  • 665 views
  • 0 likes
  • 2 in conversation