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.
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;
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
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;
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;
Thank you for your responses! Proc sql worked great.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.