Here is the full non working code. If im understanding you correctly then, I will have to set both up as a sub query or build separate tables before i do the join? proc sql; create table tmp_mat_sorted as select customer, gd, apg1, case when golden_usage_type = 'AA' then 'AA' else finance_contract_name end as finance_contract_name, case /*create period data groups*/ when "&min_date"d < datepart(transaction_date) <= "&max_date"d then 'Add_Period' when INTNX('YEAR',"&min_date"d,-3,'S') < datepart(transaction_date) <= INTNX('YEAR', "&min_date"d,-2,'S') then 'Drop_Period' else 'Overlap' end as period, golden_usage_type as usage_type, part_number, transaction_date, transaction_quantity, loco_count, yield from dfrin.tbl_material_txn_raw u Left join (select distinct customer, finance_contract_name, avg(no_of_locomotives) as loco_count, yield, case when &add_start < fiscal_month <= &add_end then 'Add_Period' when &drop_start < fiscal_month <= &drop_end then 'Drop_Period' when &olap_start < fiscal_month <= &olap_end then 'Overlap' end as period from tmp_loco_counts natural left join dfrin.tbl_yield_rates group by customer, finance_contract_name, yield, period having period ne '') c on u.customer=c.customer and u.finance_contract_name=c.finance_contract_name and u.period=c.period where INTNX('YEAR',"&min_date"d,-3,'S') < datepart(transaction_date) <= "&max_date"d and %_eg_WhereParam(CUSTOMER, Customer, IN, TYPE=S, IS_EXPLICIT=0 ) and %_eg_WhereParam(GD, GD, IN, TYPE=S, IS_EXPLICIT=0 ) and %_eg_WhereParam(usage_type,usage_type, IN, TYPE=S, IS_EXPLICIT=0 ) order by customer, gd, apg1, finance_contract_name, usage_type, part_number, period ; quit;
... View more