AFHood Group Blog The thoughtless yammerings of marketing junkies..

14Jun/120

Creating all possible pairs / the Cartesian product

Ever since you started with SQL or learned SAS, you've been warned about Cartesian products. But sometimes you need them. Shocking, we know.

Quick note of warning, when creating all possible pairs, make sure you've narrowed down your dataset to only contain a distinct list of those values you want to use in the pairs. Extra values make it more likely that you'll take the system down...

Here are some easy ways to produce all possible pairs of data.

example 1 - Using dataset and point

data all_pairs;

set dataset_w_distinct_values;

do _n_=1 to nobs;

set value_y nobs=nobs point=_n_;

output;

end;

run;

example 2 - Using proc sql

proc sql;

create table all_pairs as

select

value_x, value_y

from

value_list_1, value_list_2;

quit;

example 3 - Using hash

data all_pairs (drop=_:);

if _n_=1 then do;

set dataset_w_distinct_values (obs=1);

dcl hash h(dataset: 'dataset_w_distinct_values', ordered: 'a');

h.definekey('value_x');

h.definedata('value_x');

h.definedone();

dcl hiter hi('h');

end;

 

set dataset_w_distinct_values ;

do _rc=hi.first() by 0 while (_rc=0);

output;

_rc=hi.next();

end;

run;

 

 

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

No trackbacks yet.