How to remove duplicated records\observations WITHOUT sorting in SAS?

前端 未结 8 1670
孤独总比滥情好
孤独总比滥情好 2021-02-08 14:21

I wonder if there is a way to unduplicate records WITHOUT sorting?Sometimes, I want to keep original order and just want to remove duplicated records.

I

相关标签:
8条回答
  • 2021-02-08 14:40
    /* Give each record in the original dataset and row number */
    data with_id ;
      set mydata ;
      _id = _n_ ;
    run ;
    
    /* Remove dupes */
    proc sort data=with_id nodupkey ;
      by var1 var2 var3 ;
    run ;
    
    /* Sort back into original order */
    proc sort data=with_id ;
      by _id ;
    run ;
    
    
    0 讨论(0)
  • 2021-02-08 14:47

    Depending on the number of variables in your data set, the following might be practical:

    data abc_nodup;
       set abc;
       retain _var1 _var2 _var3 _var4;
       if _n_ eq 1 then output;
       else do;
          if (var1 eq _var1) and (var2 eq _var2) and
             (var3 eq _var3) and (var4 eq _var4)
             then delete;
          else output;
       end;
       _var1 = var1;
       _var2 = var2;
       _var3 = var3;
       _var4 = var4;
       drop _var:;
    run;
    
    0 讨论(0)
  • 2021-02-08 14:54

    I think the short answer is no, there isn't, at least not a way that wouldn't have a much bigger performance hit than a method based on sorting.

    There may be specific cases where this is possible (a dataset where all variables are indexed? A relatively small dataset that you could reasonably load into memory and work with there?) but this wouldn't help you with a general method.

    Something along the lines of Chris J's solution is probably the best way to get the outcome you're after, but that's not an answer to your actual question.

    0 讨论(0)
  • 2021-02-08 14:55

    You could use a hash object to keep track of which values have been seen as you pass through the data set. Only output when you encounter a key that hasn't been observed yet. This outputs in the order the data was observed in the input data set.

    Here is an example using the input data set "sashelp.cars". The original data was in alphabetical order by Make so you can see that the output data set "nodupes" maintains that same order.

    data nodupes (drop=rc);;
      length Make $13.;
    
      declare hash found_keys();
        found_keys.definekey('Make');
        found_keys.definedone();
    
      do while (not done);
        set sashelp.cars end=done;
        rc=found_keys.check();
        if rc^=0 then do;      
          rc=found_keys.add(); 
          output;              
        end;
      end;
      stop;
    run;
    
    proc print data=nodupes;run;
    
    0 讨论(0)
  • 2021-02-08 14:58
    data output;
    set yourdata;
    by var notsorted;
    if first.var then output;
    run;
    

    This will not sort the data but will remove duplicates within each group.

    0 讨论(0)
  • 2021-02-08 15:01

    Please refer to Usage Note 37581: How can I eliminate duplicate observations from a large data set without sorting, http://support.sas.com/kb/37/581.html . Usage Note 37581 shows how PROC SUMMARY can be used to more efficiently remove duplicates without the use of sorting.

    0 讨论(0)
提交回复
热议问题