01-24-2012 08:38 AM
Imagine you want to have a list of products bought by customers, for instance the ones who live in theUS.
If your database has a somewhat reasonable structure, the transaction data and the customer’s country will be kept separately.
So there are 2 different tables in your SAS data warehouse, let’s say sales_2012_02 and customerinfo.
In order to get to the customers you want you could write regular SAS code, sorting the tables, merging and keeping the customers you need (example here is if you don't use formats or hash tables which often perform better than sorting and merging);
PROC SORT DATA=sales_2012_02 OUT= neededsalestable;
PROC SORT DATA=customerinfo (WHERE=(Country = "US"))
MERGE neededsalestable neededcustomerstable (IN= neededcustomers);
IF neededcustomers THEN OUTPUT;
this works but doesn't look nice and 2 proc sorts might take a bit of computing time.
You can do all this in one easy PROC SQL.
CREATE TABLE dataIwant AS
SELECT * FROM sales_2012_02 WHERE customerid IN (
SELECT customerid FROM customerinfo WHERE Country = "US");
Whether one will run faster than the other will depend on your system configuration (we will not discuss that here) but it sure is easier to read this way.
This technique is called SQL subsetting and you should use it wisely.