DATA Step, Macro, Functions and more

Select Max date in where statement

Reply
Contributor
Posts: 47

Select Max date in where statement

I have a code as below, i need to take the select max(date) in seperate code. it takes so much time. How can i seperate them

This is the code

 

   proc sql;

create table aa as

select     a.vid,

             a.activity_date,

             a.hour,

             a.from_date,

            b.req,

          b.perehis_act_date

from stg.coa a,

        stg.wte  b 

where a.EMPLOYEE_PIDM = b.perehis_pidm

and b.perehis_act_date = (select max(x.perehis_act_date)

                                       from stg.wte x

                                       where x.perehis_pidm=b.perehis_pidm

                                    and x.perehis_Act_date <= a.from_date )

and datepart(a.from_date) <= (select max(datepart(e.end_date))

                                         from stg.comp e

                                        where today()GE datepart(e.end_date)+14);

quit;

 

 

I need this query to simple couple query. Thank you

Frequent Contributor
Posts: 83

Re: Select Max date in where statement

I think...

 


proc sql;
create table second as
select max(x.perehis_act_date) as max_prehis
from stg.wte x     inner join stg.wte  b on (x.perehis_pidm=b.perehis_pidm)
                inner join stg.coa a on (perehis_Act_date <= a.from_date);
create second2 as
select distinct b.*
from stg.wte b inner join second m on (b.perehis_act_date=m.max_prehis);
create table third as
select max(datepart(e.end_date)) as e_end_date
from stg.comp e
where today()GE datepart(e.end_date)+14);
create table aa as
select a.vid, a.activity_date, a.hour, a.from_date, b.req, b.perehis_act_date
from stg.coa a     inner join second2 b on (a.EMPLOYEE_PIDM = b.perehis_pidm)
                inner join third n on (datepart(a.from_date)<= n.e_end_date);

 

However some of those queries get rather convulted. The essential goal is to create three tables for the final join which have already executed the subqueries. So hopefully I figured out where each of them correctly line up to. Also some of the inner joins may not really need to be inner joins, they may need to be left joins, but the way it was written it looked like they should be inners. Hope this helps.

PROC Star
Posts: 1,760

Re: Select Max date in where statement

To speed up your query withuot chaging it, you can:

- Make the second subquery a permanent temporary table since it does not depend on other parameters. Even better, store the result in a macro variable.

- Index table STG.WTE on PEREHIS_PIDM (and possibly PEREHIS_ACT_DATE) to speed up the first subquery's join

 

 

 

Ask a Question
Discussion stats
  • 2 replies
  • 442 views
  • 0 likes
  • 3 in conversation