Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Using PROC SQL to simplify lookups which combine different tables

Using PROC SQL to simplify lookups which combine different tables

Using PROC SQL to simplify lookups which combine different tables

Imagine you want to have a list of products bought by customers, for instance the ones who live in the US.

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;

          BY customerid;



PROC SORT DATA=customerinfo (WHERE=(Country = "US"))

OUT= neededcustomerstable;

          BY customerid;



DATA dataIwant;

          MERGE neededsalestable neededcustomerstable (IN= neededcustomers);

          BY customerid;

          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.


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close