BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Viktoreli
Obsidian | Level 7

Hi,

I could not find a solution for this problem on the board so I thought I would ask instead.


As the long subject line says,

If a variable A meets certain conditions then I want to delete all observations with the same variable B as the other observation.

 

If A  >10 and B is 5, then I want to remove all variables where B=5.

 

I want this

 

Obs A B

1 5 10

2 11 10

3 1 11

4 9 11

5 12 12

6 3 13

 

To become this

1 1 11

2 9 11

3 3 13

 

Thanks in advance!

 

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

Here you go:

data have;
  input a b;
  datalines;
5 10
11 10
1 11
9 11
12 12
3 13
;

data want;
  if _n_=1 then
    do;
      dcl hash h1(dataset:'have(where=(a>10))');
      h1.defineKey('b');
      h1.defineDone();
    end;
  set have;
  if h1.check()=0 then delete;
run;

proc print data=want;
run;

View solution in original post

6 REPLIES 6
Viktoreli
Obsidian | Level 7
Should have added that in my lines,
b=5 was just an example of a value.
I was not clear enough in what I was asking.

If an observation meets certain conditions in variable A,
I want to remove all observations with the same variable B as the observation that met the conditions for variable A.
Patrick
Opal | Level 21

Here you go:

data have;
  input a b;
  datalines;
5 10
11 10
1 11
9 11
12 12
3 13
;

data want;
  if _n_=1 then
    do;
      dcl hash h1(dataset:'have(where=(a>10))');
      h1.defineKey('b');
      h1.defineDone();
    end;
  set have;
  if h1.check()=0 then delete;
run;

proc print data=want;
run;
PeterClemmensen
Tourmaline | Level 20

Ok. I would go with the solution by @Patrick  then.

 

Alternatively you could do

 

data have;
  input a b;
  datalines;
5 10
11 10
1 11
9 11
12 12
3 13
;

data want;
    array _ {1000000} _temporary_;
    do until (lr1);
        set have(where=(a>10)) end=lr1;
        _[b]=1;
    end;

    do until (lr2);
        set have end=lr2;
        if _[b] ne 1 then output;
    end;
    stop;
run;
Viktoreli
Obsidian | Level 7
Worked great and yielded the same result as patricks!
Im very grateful!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 876 views
  • 3 likes
  • 3 in conversation