PROC SQL; create table cadastro_cliente_jogos as select a.*, b.Total_contratos, c.Contratos_validos from alura.cadastro_cliente_v3 as A
left join (
select CPF, count(*) as Total_contratos
from alura.operacoes_201709
group by 1
) as B
on input(substr(a.CPF,1,11),COMMAX11.0) = b.CPF
left join (
select DISTINCT(CPF),
count(CPF) as quantidade_CPF_PRESENTE_BASE,
(DATA_RETORNO - DATA_RETIRADA) as DIAS_ENTREGA,
CUSTO_REPARO,
case when calculated DIAS_ENTREGA <= 30 AND CUSTO_REPARO = 0
THEN 1
ELSE 0
END as Contratos_validos
from alura.operacoes_201709 where calculated Contratos_validos = 1
group by 1) as C
on input(substr(a.CPF,1,11),COMMAX11.0) = c.CPF
where calculated Contratos_validos = 1;
QUIT;