BookmarkSubscribeRSS Feed
bconner
Fluorite | Level 6

Hi,

 

I am working with some large datasets (billions of obs) and found there are duplicates of the key values which I want to remove all but the first.  The file is sorted by key variables (say A and B) and is indexed by them as well (call it myndx).

 

The first dataset I am working with I have a list of the key values which have dupes.  I was thinking I could use a MODIFY statement with the key= option to step through it, removing dupes.

 

The Statements Reference manual refers to using a DO loop to process successive copies but gives no examples.  Has anyone done this already and can share the details on the method used or suggest an alternate approach?  The preference is to use the existing file(s) in-place rather than conventional Data step replacement.

 

Thanks!

 

--Ben

5 REPLIES 5
SuryaKiran
Meteorite | Level 14

Does the table exists in database or is it a SAS Table?

If its a database tables then try using pass-through query by sending your table which contains duplicate values into database.

 

Thanks,
Suryakiran
bconner
Fluorite | Level 6

Everything is in SAS...

 

--Ben

Patrick
Opal | Level 21

Something like below should work.

data dupList;
  do id=2,5,7,9;
    output;
  end;
  stop;
run;

data big;
  var='some string';
  do id=2,7;
    output;
  end;
  do id=1 to 10;
    output;
  end;
  do id=2,5,7,9;
    output;
  end;
  stop;
run;

data big;
  if _n_=1 then
    do;
      dcl hash dupList(dataset:'dupList');
      _rc=dupList.defineKey('id');
      _rc=dupList.defineDone();
      dcl hash remList(dataset:'dupList(obs=0)');
      _rc=remList.defineKey('id');
      _rc=remList.defineDone();
    end;

  modify big;

  if remList.check()=0 then 
    do;
      remove;
    end;
  else if dupList.check()=0 then 
    do;
      _rc=remList.add();
      _rc=dupList.remove();
    end;

run;

proc print data=big;
run;

Patrick
Opal | Level 21

And should you have sufficient memory to store all the keys in memory then you could also take a more direct approach.

data big;
  if _n_=1 then
    do;
      dcl hash dupList();
      _rc=dupList.defineKey('id');
      _rc=dupList.defineDone();
    end;

  modify big;

  if dupList.check()=0 then 
    do;
      remove;
    end;
  else dupList.add();

run;

proc print data=big;
run;
s_lassen
Meteorite | Level 14

Using the index to get at the duplicates is a very good idea, you may try something like this:

 

data big;
  set dupes(keep=<key variables>);
  first=1;
  do until(0);
    modify big key=<your index name>;
    if _iorc_ then do;
       _error_=0; /* when _iorc_ is set, an error is provoked */
       leave;
       end;
    if not first then 
      remove;
    else 
      first=0; /* the next obs is not the first */
    end;
run;

 

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
  • 5 replies
  • 1331 views
  • 1 like
  • 4 in conversation