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

前端 未结 8 1638
孤独总比滥情好
孤独总比滥情好 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: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;
    

提交回复
热议问题