Help using Base SAS procedures

proc sql: how to

Accepted Solution Solved
Reply
Contributor
Posts: 60
Accepted Solution

proc sql: how to

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.


Accepted Solutions
Solution
‎11-14-2011 09:25 AM
Super User
Posts: 5,260

proc sql: how to

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


All Replies
Super User
Posts: 5,260

proc sql: how to

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
Contributor
Posts: 60

proc sql: how to

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;

Solution
‎11-14-2011 09:25 AM
Super User
Posts: 5,260

proc sql: how to

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
Contributor
Posts: 60

Re: proc sql: how to

It do works!

Many Thanks!

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

Super User
Posts: 5,260

Re: proc sql: how to

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
Contributor
Posts: 60

Re: proc sql: how to

I get it. Thank you!

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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