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

How to check if column in one table is part of column in another table .I have used Like keyword but it did not work.

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

 

data TB1;

COMP_KEY="AAA BBBB CCC";output;
COMP_KEY="DDD FF";output;
run;

data tb2;
COMP_KEY="BBB";output;
COMP_KEY="DDD";output;
run;

PROC SQL ;
CREATE TABLE GRP1 AS
SELECT C.COMP_KEY, B.COMP_KEY AS GRP_COMP_KEY
FROM TB1 B , TB2 C
where B.COMP_KEY like '%'||C.COMP_KEY||'%' ;
QUIT;

PROC SQL ;
CREATE TABLE GRP2 AS
SELECT C.COMP_KEY, B.COMP_KEY AS GRP_COMP_KEY
FROM TB1 B , TB2 C
where find( B.COMP_KEY, c.comp_key, 'it')>0 ;
QUIT;

Both sets of code work fine for me given your example data and code. 

Please generate a fully worked example, as above, that shows where this code isn't working. 

 

 

 

 

 

 

View solution in original post

8 REPLIES 8
Reeza
Super User
More details please.
Like/FIND is usually the way.
Sandhya55
Obsidian | Level 7
I have used like statement as below
PROC SQL ;
CREATE TABLE GRP1 AS
SELECT C.COMP_KEY, B.COMP_KEY AS GRP_COMP_KEY
FROM TB1 B , TB2 C
where B.COMP_KEY like '%'||C.COMP_KEY||'%' ;
QUIT;
But this is not working
Reeza
Super User
What does not working mean? What do you have - very specifically what does your input data look like?
Can't share it due to privacy, please make fake data that replicates your problem.
What do you want to do? If that was your input data what do you expect as results?

I see your code, but what does the log show?

What about the following instead? FIND with the i/t makes it so that there case differences (upper/lower case) are ignored as are trailing blanks.

PROC SQL ;
CREATE TABLE GRP1 AS
SELECT C.COMP_KEY, B.COMP_KEY AS GRP_COMP_KEY
FROM TB1 B , TB2 C
where find( B.COMP_KEY, c.comp_key, 'it')>0 ;
QUIT;

Sandhya55
Obsidian | Level 7
Below are the contents of
File A:
AAA BBBB CCC
DDD FF
File B :
BBB
DDD
Sandhya55
Obsidian | Level 7

Log:

NOTE: The execution of this query involves performing one or more Cartesian product joins that can not be optimized.
NOTE: Table  created, with 0 rows and 2 columns.

Reeza
Super User

 

data TB1;

COMP_KEY="AAA BBBB CCC";output;
COMP_KEY="DDD FF";output;
run;

data tb2;
COMP_KEY="BBB";output;
COMP_KEY="DDD";output;
run;

PROC SQL ;
CREATE TABLE GRP1 AS
SELECT C.COMP_KEY, B.COMP_KEY AS GRP_COMP_KEY
FROM TB1 B , TB2 C
where B.COMP_KEY like '%'||C.COMP_KEY||'%' ;
QUIT;

PROC SQL ;
CREATE TABLE GRP2 AS
SELECT C.COMP_KEY, B.COMP_KEY AS GRP_COMP_KEY
FROM TB1 B , TB2 C
where find( B.COMP_KEY, c.comp_key, 'it')>0 ;
QUIT;

Both sets of code work fine for me given your example data and code. 

Please generate a fully worked example, as above, that shows where this code isn't working. 

 

 

 

 

 

 

Sandhya55
Obsidian | Level 7

Thanks !!! Find function worked for me ..probably my issue is with the trailing or leading spaces 

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
  • 8 replies
  • 828 views
  • 1 like
  • 2 in conversation