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

Hi I am using a sub query  

FROM cars WHERE ID NOT IN (select distinct ID from cars_m1 where data_date = "31MAR2018"d ) 

is there a way I can use a left or right join to eliminate the sub query ?

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20

If you want a left join ( but you are really doing an inner join here), these 2 queries are equivalent:


data T; 
  set SASHELP.CARS; 
  DATA_DATE =ifn(ranuni(1) > .5, "31MAR2018"d,"01MAR2018"d );     
  ID=MODEL;
run;

proc sql;   

create table T1 as
select *
from T 
where ID not in (select distinct ID from T where DATA_DATE = "31MAR2018"d );     
 
create table T1 as
select T.*
from T 
     left join 
     (select distinct ID from T where DATA_DATE = "31MAR2018"d ) tt 
     on t.ID =tt.ID  
where tt.ID is null;

quit; 

 

 

 

View solution in original post

2 REPLIES 2
yabwon
Amethyst | Level 16

Hi,

 

Do you mean to get something like this:

 

ods html;
data cars(keep = id text) cars_m1(drop=text);
input ID data_date date9. text $;
cards;
1 31MAR2018 A
2 31MAR2018 B
3 31MAR2018 C
4 01APR2018 D
5 01APR2018 E
6 01APR2018 F
;
run;

proc sql;
select cars.* from
cars 
join
(select distinct ID from cars_m1 where NOT (data_date = "31MAR2018"d)) x
on cars.id = x.id
;


select distinct cars.* from
cars 
join
cars_m1 x
on cars.id = x.id
and NOT (x.data_date = "31MAR2018"d)
;

quit;

 

all the best

Bart

 

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



ChrisNZ
Tourmaline | Level 20

If you want a left join ( but you are really doing an inner join here), these 2 queries are equivalent:


data T; 
  set SASHELP.CARS; 
  DATA_DATE =ifn(ranuni(1) > .5, "31MAR2018"d,"01MAR2018"d );     
  ID=MODEL;
run;

proc sql;   

create table T1 as
select *
from T 
where ID not in (select distinct ID from T where DATA_DATE = "31MAR2018"d );     
 
create table T1 as
select T.*
from T 
     left join 
     (select distinct ID from T where DATA_DATE = "31MAR2018"d ) tt 
     on t.ID =tt.ID  
where tt.ID is null;

quit; 

 

 

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 2 replies
  • 1104 views
  • 1 like
  • 3 in conversation