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

Qu. Fetch the customer details who have shopped only in Jan.

 

Data Jan;
input Customer$ Item$ Amount;
Datalines;
A1 Sandal 110
A3 Watch 210
A5 Shoes 310
;
run;


Data Feb;
input Customer$ Item$ Amount;
datalines;
A1 Dresses 120
A2 Bags 220
A4 Paintings 320
;
run;

 

Output should be :


Final
Customer Item Amount
A3             Watch 210
A5             Shoes 310

 

Can any one please help me to write better SQL query.

 

 

Regards,

Jai

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20
Data Jan;
input Customer$ Item$ Amount;
Datalines;
A1 Sandal 110
A3 Watch 210
A5 Shoes 310
;
run;


Data Feb;
input Customer$ Item$ Amount;
datalines;
A1 Dresses 120
A2 Bags 220
A4 Paintings 320
;
run;

proc sql;
   create table want as
   select * from Jan
   where Customer not in
   (select distinct Customer from Feb);
quit;

View solution in original post

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20
Data Jan;
input Customer$ Item$ Amount;
Datalines;
A1 Sandal 110
A3 Watch 210
A5 Shoes 310
;
run;


Data Feb;
input Customer$ Item$ Amount;
datalines;
A1 Dresses 120
A2 Bags 220
A4 Paintings 320
;
run;

proc sql;
   create table want as
   select * from Jan
   where Customer not in
   (select distinct Customer from Feb);
quit;
jaiganesh
Obsidian | Level 7

Thanks for Help.

 

By the time i have also figure out below query.

 

proc sql;

select * from Jan where customer  not IN (select customer from Jan intersect select customer from Feb);

run;

 

Thank you.

PeterClemmensen
Tourmaline | Level 20

@jaiganesh cool stuff 🙂 Glad you found your answer.

novinosrin
Tourmaline | Level 20
Data Jan;
input Customer$ Item$ Amount;
Datalines;
A1 Sandal 110
A3 Watch 210
A5 Shoes 310
;
run;


Data Feb;
input Customer$ Item$ Amount;
datalines;
A1 Dresses 120
A2 Bags 220
A4 Paintings 320
;
run;

proc sql;
create table want as
select a.*
from jan a left join feb b
on a.customer = b.customer
where b.customer=' ';
quit;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

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