Well, I would suggest creating a inter file first of the base1 and base2 datasets first, this can then be merged on directly to the main data, something like this (but note it is quite hard to ascertain what the best approach is without seeing any data - why do you have three datasets for example - what is the point of base1 and base2 - without seeing all the links its hard to re-structure):
proc sql;
create table BASE as
select CUSTOMER_REC,
(select 1 from BASE.BASE2 A where A.CUSTOMER_REC=CUSTOMER_REC and A.VAR2 in ('41','42','43')) as FLAG1,
(select 1 from BASE.BASE2 A where A.CUSTOMER_REC=CUSTOMER_REC and A.VAR2 in ('41','44')) as FLAG2
from BASE1;
create table WANT as
select A.CUSTOMER,
A.CUSTOMER_REC,
A.APPNO,
A.TYPE,
A.DATE1,
A.STATUS,
A.STATUS1,
A.STATUS2,
A.DATE,
A.TDAY
from DATASET A
left join CUSTOMER_REC B
on A.CUSTOMER_REC=B.CUSTOMER_REC
where (A.STATUS="A" or (A.STATUS="D" and B.FLAG1=1 and B.FLAG2=1))
and (A.STATUS in ("A","D") and A.STATUS1="C" and A.STATUS2 not in ("A","B"));
quit;
... View more