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

Hi: I have 280K records in left table and over a million in table 2. My join brings me 100K records only. I don't understand the problem as to why data is dropped in left join. I have tried it two ways but both bring back 100K records. Help will be greatly appreciated.

 

Methods1:
PROC SQL;
CREATE TABLE WORK.ASSIGNS AS
SELECT DISTINCT t1.ACCT_NO,
t1.ACCT_NO_DET,
t1.PROD_ONE,
t1.PROD_TWO,
t1.START_DATE,
t1.END_DATE,
t2.PROD_DETAILS

FROM WORK.CUSTOMER t1
LEFT JOIN SCHEMA.PRODUCTS t2 ON (t1.ACCT = t2.ACCT_NO) AND (t1.ACCT_NO_DET = t2.ACCT_NO_DET)
WHERE t2.PROD_DETAILS LIKE 'ACCESSORY' ;
QUIT;

 

Method 2:

PROC SQL;
CREATE TABLE WORK.ASSIGNS AS
SELECT DISTINCT t1.ACCT_NO,
t1.ACCT_NO_DET,
t1.PROD_ONE,
t1.PROD_TWO,
t1.START_DATE,
t1.END_DATE,
t2.PROD_DETAILS

FROM WORK.CUSTOMER t1
LEFT JOIN SCHEMA.PRODUCTS t2 ON (t1.ACCT = t2.ACCT_NO) AND (t1.ACCT_NO_DET = t2.ACCT_NO_DET)
WHERE t2.PROD_DETAILS LIKE 'ACCESSORY' or t2.PROD_DETAILS is  missing ;
QUIT;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

You must force the where clause to be evaluated before the join. Try this:

PROC SQL;
CREATE TABLE WORK.ASSIGNS AS
SELECT DISTINCT t1.ACCT_NO,
	t1.ACCT_NO_DET,
	t1.PROD_ONE,
	t1.PROD_TWO,
	t1.START_DATE,
	t1.END_DATE,
	t2.PROD_DETAILS
FROM 
	WORK.CUSTOMER as t1 LEFT JOIN 
	(	select * 
		from  SCHEMA.PRODUCTS
		WHERE PROD_DETAILS LIKE 'ACCESSORY'  ) as t2 
		ON t1.ACCT = t2.ACCT_NO AND t1.ACCT_NO_DET = t2.ACCT_NO_DET;
QUIT;
PG

View solution in original post

4 REPLIES 4
Reeza
Super User
Your WHERE clause is filtering out the records most likely.
sasuser_sk
Quartz | Level 8

I tried AND too instead of where and that bring me 320K records back.

PGStats
Opal | Level 21

You must force the where clause to be evaluated before the join. Try this:

PROC SQL;
CREATE TABLE WORK.ASSIGNS AS
SELECT DISTINCT t1.ACCT_NO,
	t1.ACCT_NO_DET,
	t1.PROD_ONE,
	t1.PROD_TWO,
	t1.START_DATE,
	t1.END_DATE,
	t2.PROD_DETAILS
FROM 
	WORK.CUSTOMER as t1 LEFT JOIN 
	(	select * 
		from  SCHEMA.PRODUCTS
		WHERE PROD_DETAILS LIKE 'ACCESSORY'  ) as t2 
		ON t1.ACCT = t2.ACCT_NO AND t1.ACCT_NO_DET = t2.ACCT_NO_DET;
QUIT;
PG
sasuser_sk
Quartz | Level 8

Thank you! It worked.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 4 replies
  • 3571 views
  • 4 likes
  • 3 in conversation