BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
rpg163
Calcite | Level 5

Hi,

     I wanna get data from database A in sql procedure.

     If the observation satisfy any one of  three statements, it is suppose to be choose, like follows. But I do know how to deal with that.

    

proc sql;

    create table C as

    select A.*

    from A, B

    where (A.stkcd not in(select stkcd from B)) or (A.stkcd=B.stkcd and A.year<B.year_s)  or  (A.stkcd=B.stkcd and A.year>B.year_e);

quit;

Anyone knows? Thanks in advance.

1 ACCEPTED SOLUTION

Accepted Solutions
LinusH
Tourmaline | Level 20

Do you have any problem with job run time?

If not, you could just append the select results using Outer Union Corresponding.

And yes, your could use OR, but SQL has a hard time optimizing those joins.

But you still have a problem mixing an sub-query filter and ordinary joins with or's.

I think that you can have condition 2 and 3 in the same select, but condition 1 in a separate select.

proc sql;

     create table C as

     select A.*

     from A, B

     where A.stkcd=B.stkcd and A.yearB.year_e

outer union corresponding

     select A.*

     from A

     where A.stkcd not in(select stkcd from B)

;

quit;

Data never sleeps

View solution in original post

6 REPLIES 6
LinusH
Tourmaline | Level 20

Conditions 2 and 3 are using regular inner join syntax, but condition 1 is a sub-query.

Try to use a full table specification in that sub-query instead of an alias (if you are using a such - your code aren't your original one I guess?).

/Linus

Data never sleeps
rpg163
Calcite | Level 5

no, and I don't know if "or" works in sql

what I am trying to do is incorporating the follow three queries into one

proc sql;

    create table C1 as

    select A.*

    from A

    where A.stkcd not in(select stkcd from B)

quit;

proc sql;

    create table C2 as

    select A.*

    from A,B

     where A.stkcd=B.stkcd and A.year<B.year_s ;

quit;

proc sql;

    create table C3 as

    select A.*

    from A,B

     where A.stkcd=B.stkcd and A.year>B.year_e;

quit;

LinusH
Tourmaline | Level 20

Do you have any problem with job run time?

If not, you could just append the select results using Outer Union Corresponding.

And yes, your could use OR, but SQL has a hard time optimizing those joins.

But you still have a problem mixing an sub-query filter and ordinary joins with or's.

I think that you can have condition 2 and 3 in the same select, but condition 1 in a separate select.

proc sql;

     create table C as

     select A.*

     from A, B

     where A.stkcd=B.stkcd and A.yearB.year_e

outer union corresponding

     select A.*

     from A

     where A.stkcd not in(select stkcd from B)

;

quit;

Data never sleeps
rpg163
Calcite | Level 5

It do works!

Many Thanks!

well, still one question, when you mention job run time I am not sure what you mean

LinusH
Tourmaline | Level 20

What I meant was that if you were having any perormance issues, i.e. if the job was taking too much time, or took too much of resources in the system (CPU/memory).

/Linus

Data never sleeps
rpg163
Calcite | Level 5

I get it. Thank you!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 1011 views
  • 1 like
  • 2 in conversation