Question

Is there a way to get all the rows from tbl1 where a column contains the one of the values in tbl2's column?

So lets say tbl1.col1 are strings. Now I want the rows of tbl1, where col1 contains (as in x contains y in SQL syntax) one of the entries of tbl2.col2.

So I know that you can get all the rows in one table, that appear in another table by doing something like this:

proc sql; 
select tbl1.* 
from tbl1, tbl2
where tbl1.id = tbl2.id
;quit;

However when I tried:

proc sql; 
select tbl1.* 
from tbl1, tbl2
where tbl1.id contains tbl2.id
;quit;

I just get an empty table.

Was it helpful?

Solution

If I understand what you're doing, which is finding rows in t1 where there is at least one row in t2 such that that t2.id has t1.id as a substring, your problem is probably that t1.id needs to be compressed/trimmed. Otherwise, t1.id will have spaces after its actually useful value padding out to the full string length - in this example, that is 8 long (the default for strings read in the way I read them in for t1).

data t1;
input id $;
datalines;
123
456
789
;;;;
run;

data t2;
input @1 id $8.;
datalines;
123 456
234 567
;;;
run;

proc sql;
select t1.*
from t1,t2
where t2.id contains compress(t1.id);
quit;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top