Question

I have two tables with multiple columns. Now I want to merge them using like operator. My code is like following

proc sql;

    select a.*,b.*
    from tera.SFTP as a,home.SFTP_1 as b
    where a.zip_c like ('%b.zip_extract%')

;
quit;

I am getting Page Break messages and no result is returned. The column types for a.zip_c is Char and length is 50 while for b.zip_extract its Char with length 6.

Was it helpful?

Solution

Merging on like is not a great idea; it doesn't use indices and doesn't use a lot of the optimizations you otherwise get. However, sometimes it's necessary.

In SAS, though, I'd do this differently [and in most other SQLs...]

proc sql;

    select a.*,b.*
    from tera.SFTP as a,home.SFTP_1 as b
    where find(a.zip_c,b.zip_extract)
;

quit;

That accomplishes the same thing as LIKE, but is more likely to allow SAS to use indices and optimizations, and is more clearly written.

To deal with possible column length issues, use TRIM:

data have_a;
 length zip_c $50;
 input @1 zip_c $50.;
 datalines;
12345 99999
67890 99999
88001 99999
37013 99999
60037 99999
;;;;
run;

data have_b;
 length zip_extract $7;
 input zip_extract $;
 datalines;
12345
37013
99998
;;;;
run;

proc sql;
  title "No Rows!";
    select a.*,b.*
    from have_a as a,have_b as b
    where find(a.zip_c,b.zip_extract)
;

quit;
proc sql;
  title "Rows!";
    select a.*,b.*
    from have_a as a,have_b as b
    where find(a.zip_c,trim(b.zip_extract))
;

quit;

OTHER TIPS

The problem is that you are matching on the string, b.zip_extract, not the column.

Try:

select a.*,b.*
from tera.SFTP as a,home.SFTP_1 as b
where a.zip_c like '%' || b.zip_extract || '%'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top