Skip to content Skip to sidebar Skip to footer

Sas/proc Sql - Remove All Observations In By Group As Long As There Are Duplications (not Just Remove The Duplications)

I am new to SAS and I am trying to remove groups if they fulfil two conditions. I currently have this data set: ID ID_2 ID_3; A 1 1; A 1 1; A 1 1; A 2 0; A 2 1; B 3 0; B 3 0

Solution 1:

I believe this will accomplish what you want. The logic could be tweaked to be a little more clear, I guess, but it worked when I tested it.

data x;
    input id $ id_2 id_3;
cards;
A 1 1
A 1 1
A 1 1
A 2 0
A 2 1
B 3 0
B 3 0
;
run;

* I realize the data are already sorted, but I think it is better
* not to assume they are.;
proc sort data=x;
    by id id_2 id_3;
run;

* It is helpful to create a dataset for the duplicates as well as the 
* unduplicated observations.;
data nodups
     dups
     ;

    set x;
    by id id_2 id_3;

    * When FIRST.ID_3 and LAST.ID_3 at the same time, there is only
    * one obs in the group, so keep it;
    if first.id_3 and last.id_3
     then output nodups;

     * Otherwise, we know we have more than one obs. According to
     * the OP, we keep them, too, unless ID_3 = 1;
     else do;
        if id_3 = 1
         then output dups;
         else output nodups;
     end;

run;

Post a Comment for "Sas/proc Sql - Remove All Observations In By Group As Long As There Are Duplications (not Just Remove The Duplications)"