Permutations

From sasCommunity
Jump to: navigation, search

These solution are harvested from a SAS-L thread of 2008-July

  • From: Kevin F. Spratt
  • To: SAS-L@LISTSERV.UGA.EDU
  • Sent: Tuesday, July 15, 2008 8:06 AM
  • Subject: How to create all possible response patterns?

Suppose I have a 3 item survey.

Each item can have a value of 1, 2, or 3.

If I wanted to create all possible response patterns in this case the possible patterns are 3^3 = 27

1 1 1
1 1 2
1 1 3
1 2 1
1 2 2
1 2 3
1 3 1
1 3 2
1 3 3
2 1 1
2 1 2
2 1 3
2 2 1
2 2 2
2 2 3
2 3 1
2 3 2
2 3 3
3 1 1
3 1 2
3 1 3
3 2 1
3 2 2
3 2 3
3 3 1
3 3 2
3 3 3
;


How do I automate this process in a fashion that scales to the case where the number of items is 10, thus generating 3^10 unique patterns?

I thought that there may be an SQL general solution, where you create 10 data sets each having a single variable (item name) with the full range of choices, but I have not been able to successfully create this table.

A Macro

A: by T Magnusson

This is an easy macro to generate the combinations:

%macro mdo(no,var,min,max);
%do i=1 %to &no.;
do &&var.&i.=&min. to &max.;
%end;
%mend mdo;

%macro mendl(no,var,min,max);
%do i=1 %to &no.;
end;
%end;
%mend mendl;


%macro genvar(colno,var,min,max);
%put This will generate %eval((&max.-&min.+1)**&colno.) obs.;
data Work.tomtom;
%mdo(&colno.,&var.,&min.,&max.);
output;
%mendl(&colno.);
run;
%mend genvar;

%genvar(colno=3,var=col,min=1,max=10);

Proc Print data = Work.tomtom;

Colno is the number of items in your survey Var is the column names that you want to have Min is the lowest response you can get of your items Max is the highest response you can get of your items (If you need different response values for each item, you have to give them as parameter as well...)


Proc Plan

A: by M Howard

Getting the number of permutations and then running it through Proc Plan will do this; I believe you'd want both parameters to be 3 rather than one to be 2.

%let Perms = %sysfunc(perm(3,2));
%put Perms: &Perms;

ods output Plan = Work.Out_Set;
PROC Plan; factors
           block   = &Perms. ordered
           treat   = 2 of 3 perm;
run;
Proc Print data = Work.Out_Set;


Proc Plan 2

Proc plan can easily produce the data set shown above with 3^3 = 27 observations.

proc plan ordered;
   factors a=3 b=3 c=3;
   output out=plan;
   run;