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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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