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.
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;
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
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;
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;
It do works!
Many Thanks!
well, still one question, when you mention job run time I am not sure what you mean
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
I get it. Thank you!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.