BookmarkSubscribeRSS Feed
lerdem
Quartz | Level 8

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

3 REPLIES 3
overmar
Obsidian | Level 7

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.

ChrisNZ
Tourmaline | Level 20

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

 

 

 

wolowicz
Fluorite | Level 6

Hey there lerdem, 

 

I see you building some Banner Payroll queries in here. shoot me an email at wolowicz@uvic.ca. We are doing some Banner Payroll things in SAS too. 

 

Cheers


Dave

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 4275 views
  • 0 likes
  • 4 in conversation