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 | +-----------+
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;
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;
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;
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;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.