It would help if you explained the purpose of the code.
But let's go through it.
Why use a LEFT JOIN instead of just use IN operator of SQL with a subquery? The result should be the same unless your intent is to generate multiple copies of the observations from CM_EMAIL_SEND when the same email address appear in multiple observations from the CM_TEST_CUSTOMERS dataset.
But is seems you only want the observations with missing value of email (is that a mistake in your code?). So perhaps what you really meant was NOT in ?
create table AAA as
select t1.*
from CM_EMAIL_SEND t1
where lowcase(t1.email) not in
(select lowcase(t2.email) from CM_TEST_CUSTOMERS
where t2.email is not null)
;
Second there is no need to run another query to know how many observations were written to AAA because PROC SQL already wrote that number into the SQLOBS macro variable.
%let row_count=&sqlobs;
Also there is no need to use the %IF code since when AAA has zero observations appending it to CM_EMAIL_CHASOM will not add any observations. If you are worried that it will take too long to recopy all of the existing observation in CM_EMAIL_CHASOM then use some other method to append the records, such as PROC APPEND or a data step with a MODIFY statement.
Which implies why not just include the variable SINUN when generating the dataset AAA?
create table AAA as
select t1.*, 'CM' as SINUN
from CM_EMAIL_SEND t1 ...
And if there is no other need for the AAA dataset then why not just use PROC SQL INSERT statement to add the observations to CM_EMAIL_CHASOM?
insert into CM_EMAIL_CHASOM
select t1.*, 'CM' as SINUN
from CM_EMAIL_SEND t1
where lowcase(t1.email) not in
(select lowcase(t2.email) from CM_TEST_CUSTOMERS where
t2.email is not null)
;
And finally if the datasets are already sorted by EMAIL (and the values of EMAIL are already converted to lowercase) then forget the SQL and just use data steps.
data aaa;
merge CM_EMAIL_SEND(in=in1) CM_TEST_CUSTOMERS(in=in2 keep=email);
by email;
if in1 and not in2;
run;
Seriously? You're subsetting on:
where t2.email is null;
But then you want to also check:
lowcase(t1.email)=lowcase(t2.email
That's really the same as saying:
where t1.email is null and t2.email is null;
This looks like code that was written to confuse someone rather than a real attempt at writing a program. Can you explain in words what you are trying to make happen?
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.