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

 

Suppose that a website contains two tables, the Customers table and the Orders table. Use data step to find all customers who never order anything.

Table: Customers.

+----+-------+
| Id | Name  |
+----+-------+
| 1  | Joe   |
| 2  | Henry |
| 3  | Sam   |
| 4  | Max   |
+----+-------+

Table: Orders.

+----+------------+
| Id | CustomerId |
+----+------------+
| 1  | 3          |
| 2  | 1          |
+----+------------+

Using the above tables as example, return the following:

+-----------+
| Customers |
+-----------+
| Henry     |
| Max       |
+-----------+

 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

do like this

 

data customer;
input customer_id name$;
cards;
1   Joe   
2   Henry 
3   Sam   
4   Max   
;

data orders;
input id customer_id;
cards;
1   3
2   1
;

proc sort data=customer;by customer_id;run;
proc sort data=orders;by customer_id;run;

data want(keep=name);
   merge customer orders(in=ino);
   by customer_id;
   if not ino;
run;

View solution in original post

3 REPLIES 3
Jagadishkatam
Amethyst | Level 16
data customer;
input customer_id name$;
cards;
1   Joe   
2   Henry 
3   Sam   
4   Max   
;


data orders;
input id customer_id;
cards;
1   3
2   1
;

proc sql;
create table want as select a.* from customer as a where a.customer_id not in (select customer_id from orders);
quit;
Thanks,
Jag
PeterClemmensen
Tourmaline | Level 20

do like this

 

data customer;
input customer_id name$;
cards;
1   Joe   
2   Henry 
3   Sam   
4   Max   
;

data orders;
input id customer_id;
cards;
1   3
2   1
;

proc sort data=customer;by customer_id;run;
proc sort data=orders;by customer_id;run;

data want(keep=name);
   merge customer orders(in=ino);
   by customer_id;
   if not ino;
run;
Jagadishkatam
Amethyst | Level 16

Alternatively by merge step

 

proc sort data=customer;
by customer_id;
run;

proc sort data=orders;
by customer_id;
run;


data want;
merge customer(in=a) orders(in=b);
by customer_id ;
if a and not b ;
run;
Thanks,
Jag

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

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
  • 3 replies
  • 1412 views
  • 0 likes
  • 3 in conversation