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 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 819 views
  • 7 likes
  • 4 in conversation