Creating a Cartesian Product in SAS

Posted by bkloss | SAS | Friday 13 February 2009 6:49 am

Here’s the first of several question/answer posts.

Feel free to write me with your SAS question and I’ll provide the answer in a post.

Question

Suppose I have two SAS datasets, set A contains 5 variables having 3000 unique records and set B contains just 1 variable having 500 unique records (just a bunch of ID numbers). Suppose I would like to create a new dataset where each of the records from set A would be duplicated so that the first record would be repeated 500 times where each of those would be paired with a unique ID from set B. The goal is to have the new dataset with 6 variables (the five from A and the one from B) containing a total of 1,500,000 records produced in this way.

Any help or suggestions you can provide is greatly appreciated.

Answer

What you are asking for is called a Cartesian Product.  All possible combination of rows in all tables.

This can be accomplished with the following SAS code

PROC SQL noprint;

Create Table <new data set> as

select *
from <data set A>, <data set B>;
quit ;

1 Comment »

  1. Comment by Aldwin Sendaydiego — May 9, 2009 @ 4:11 am

    I dont know this things. Thanks

RSS feed for comments on this post. TrackBack URI

Leave a comment