Skip to content
Advertisement

How to delete some observations in some groups based on a condition

I would like delete some observations in some groups based on a condition. My condition is each group : if (imput=”toto”) then delete but if a whole group has imput=”toto” then keep it

The output I want :

Many thanks in advance !

Advertisement

Answer

So you want to do two checks. Is the value toto and is the value only toto. Here is SQL that will do that. I had it explicitly create the checks as new variables so you can see what is happening. To see the check values for all observations remove the HAVING clause. If you are happy with it you can remove the check variables and just move the conditions into the having clause.

To do this with just a DATA step you will want add a “double DOW Loop” so you can calculate the overall flag for a group in the first loop and then process the individual rows in the second loop. Note this requires the input dataset is sorted (or at least grouped) by class.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement