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

Dear SAS Community;

I have two variables, A and B, and would like to display the instances of B that do not occur in A.  For example:

A     B

1      1

3      2

4      3

6      4

7      5

10    6

        7

        8

        9

       10

Could someone please suggest some code for me to display my desired output?  Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

A SQL approach:

proc sql;
  create table want as
  select B from mydata o
  where not exists 
    (select A from mydata i where o.b=i.a)
  ;
quit;


View solution in original post

4 REPLIES 4
MichelleHomes
Meteorite | Level 14

Hi There,

Using a hash object might help...

data mydata;
     input @1 A $3. @3 B $3.;
cards;
1  1
3  2
4  3
6  4
7  5
10 6
   7
   8
   9
   10
;
run;

data lookup;
     length B $3;
     set mydata(rename=(B=value));
     if _N_=1 then do;
          declare hash ht(dataset:"mydata");
          ht.defineKey("A");
          ht.defineData("B");
          ht.defineDone();
     end;
     rc = ht.find(key:value);
     if rc ne 0 then output;
     keep value;
run;


Cheers,

Michelle

//Contact me to learn how Metacoda software can help keep your SAS platform secure - https://www.metacoda.com
Patrick
Opal | Level 21

A SQL approach:

proc sql;
  create table want as
  select B from mydata o
  where not exists 
    (select A from mydata i where o.b=i.a)
  ;
quit;


Rick_SAS
SAS Super FREQ

A SAS/IML program:

proc iml;

A = {1,3,4,6,7,10};

B = T(1:10);

k = element(B, A);  /* indicator variable: is B in A? */

BInA = loc(k=1);

BNotInA = loc(k=0);

print BInA, BNotInA;

TJ87
Obsidian | Level 7

Thank you for your responses! Proc sql worked great. 

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

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
  • 4 replies
  • 1343 views
  • 7 likes
  • 4 in conversation