BookmarkSubscribeRSS Feed
Sille
Calcite | Level 5

Hi! I am looking for some help with a SAS-code. In one data set I have two columns (A and B). I would like to check, whether each value in Column A occurs somewhere in Column B. Can you help me out?

4 REPLIES 4
Quentin
Super User

Please provide some example data, and the output you would want from that data.  Also please show the code you have tried.  This will help people help you.

Ksharp
Super User
Check INTERSCEPT and EXCEPT in PROC SQL:

proc sql;
select A from have
intersect
select B from have;
quit;

proc sql;
select A from have
except
select B from have;
quit;

FreelanceReinh
Jade | Level 19

Hi @Sille and welcome to the SAS Support Communities!

 

You can also use a DATA step that stops as soon as an A value was not found in column B.

data _null_;
dcl hash h(dataset:'have(keep=b)');
h.definekey('b');
h.definedone();
do until(last);
  set have end=last;
  if h.check(key:a) then do;
    put A= 'does not occur in column B.';
    stop;
  end;
end;
put 'Success! All A values were found in column B.';
stop;
run;
ballardw
Super User

You likely will need to clearly define "occurs somewhere in column B".

Suppose you have a value for A of "apple".

Does that "occur" in B if B has a value of "Apple" (case difference)? If B is "apple  pear" (not equal to but is part of the string? If B is "APPLE pear" case and part of string? If B is "applesauce" part of the string but not a whole word? If B is "Applesauce Pear juice" case difference and part of a string but not a whole word?

 

If A is a more complex value such as "apple banana grape" then you get even more complex elements to consider such as all the components of A are in B but the order is different, in addition to the not word sub-string possibilities.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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
  • 1167 views
  • 2 likes
  • 5 in conversation