Help using Base SAS procedures

sql query

Reply
Frequent Contributor
Posts: 115

sql query

Hi,

I am using below sql query to extract the results for my requirement. I am able to get the reslts, but its taking 2 mins time for 10 input records (table: Ext_cus) also.

Could you please some one help me extract the same results in any efficient way.

Req: if macro variable '&reg_cnt is ne 0 then i have to extarct all the records from t1. else i have to extract the records by join with reg_list

%macro ext_final;
proc sql;
   create table work.ext_final as
select * from Ext_cus t1

where
%if &reg_cnt ne 0 %then
      (t1.region_id ne ' ');
%else (t1.customer_group_cd in (select region_nm1 from reg_list where region_nm1 ne ' ')) or
      (t1.customer_id in (select region_nm2 from reg_list where region_nm2 ne ' '))   or
      (t1.region_id in (select region_nm3 from reg_list where region_nm3 ne ' ')) ;
;
quit;
%mend;
%ext_final;

Super User
Posts: 17,828

Re: sql query

Try using a left join instead of a IN from the various tables.

Frequent Contributor
Posts: 115

Re: sql query

I am using same table in Multiple IN

Trusted Advisor
Posts: 2,113

Re: sql query

doesn't matter.  the left join is generally more efficient as it can take advantage of indesing and the IN cannot.

Super User
Posts: 17,828

Re: sql query

exactly so won't the table have to be read multiple times with the different conditions?

Try the left join and let me know how much time that takes.

Trusted Advisor
Posts: 2,113

Re: sql query

Do you have missing data in T1?  The NE operator is very inefficient, so avoid it if you can.  Even if you have some missing data, it might be more efficient to do the selects without the WHERE clauses and then delete the missing data later.

Ask a Question
Discussion stats
  • 5 replies
  • 217 views
  • 0 likes
  • 3 in conversation