| Date: | Wed, 17 Jan 2001 17:25:30 -0500 |
| Reply-To: | 125241N@KNOTES.KODAK.COM |
| Sender: | "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU> |
| From: | John Hixon <125241N@KNOTES.KODAK.COM> |
| Subject: | Re: Combinations |
|
| Content-type: | text/plain; charset=us-ascii |
|---|
From: John Hixon
Terry Fudin asks:
>I have a dataset with a variable which contains integers 1 to 26. What is
>the best way to get all combinations of pair? i.e.:
>1, 1
>1, 2
>1, 3
.
.
.
.
>2, 1
>2, 2
>2, 3
>2, 4
.
.
.
>1, 26
.
.
.
>26, 26
>I know I can write a macro, but there must be an easier way.
>Thanks!
I answered a similar question recently, so have this handy.
You can use Proc Plan to generate combinations and permutations.
Proc Plan (which is in SAS STAT) and Proc Factex and Proc Optex
(which are in the QC package) make for a very complete suite of
tools for DOE (design of experiments). DOEs often require
constructing "candidate sets" for potential designs (which often
means doing combinations and permutations).
The commented and debugged code below should get you started using
Proc Plan. It will generate a dataset with the 325 combinations
that result from 26 objects taken two at a time.
I hope this is helpful.
%let m=2; * choose "m" of "n";
%let n=26; * Choose "m" of "n" ;
* this data step is just to put the Number of
* necessary combinations into a macro variable;
data _null_;
combos=comb(&n,&m); * get the numer of combinations;
put combos=;
call symput('N_Combos',put(combos,best12.0)); * put into a macro var;
run;
* Proc plan is useful for generating
"Choose m of n" combinations (or, also,
for choosing permutations);
proc plan;
factors Dummy=&N_Combos /* This is the Number of Combinations*/
FactorA=&m of &n comb ; /* Choose m of n combos...see Proc Plan
doc*/
output out=design /* name the 12 Factor levels */
FactorA nvals=(1 to 26 );
* print the dataset;
proc print data=design;
run;
* Notice that we need to rearrange the data set, so output to temp text
file;
data _null_;
file 'd:\temp\junk.txt';
set design;
put Dummy FactorA;
run;
* read back the temp text file and reformat;
data design2 (drop=i index Dummy);
infile 'd:\temp\junk.txt';
array A{&m} $ A1-A&m;
do i=1 to &m*&N_Combos;
index=mod(i,&m)+1;
if index=1 then do;
input Dummy$ A{index} @@;
output;
end;
if index ne 1 then input Dummy$ A{index} @@;
end;
run;
* desired output;
proc print data=design2;
run;
* To do permutations just make a slight modification as seen below:;
%let n=6; *this will yield 6!=720 permutations;
data _null_;
perms=perm(&n); * get the numer of cominations;
put perms=;
call symput('N_Perms',put(perms,best12.0)); * put into a macro var;
run;
* Proc plan is useful for generating
"Choose m of n" combinations (or, also,
for choosing permutations);
proc plan;
factors Dummy=&N_Perms /* This is the Number of Permutations*/
FactorA=&n perm ; /* Choose n! permutations...see Proc Plan doc*/
output out=design /* name the 6 Factor levels */
FactorA cvals=('a123' 'b123' 'c123' 'd111' 'e111' 'f111');
run;
* Notice that the required "list" is shown above as 6 arbitrary strings;
* print the dataset;
proc print data=design;
run;
* Notice that we need to rearrange the data set, so output to temp text
file;
data _null_;
file 'd:\temp\junk.txt';
set design;
put Dummy FactorA;
run;
* read back the temp text file and reformat;
data design2 (drop=i index Dummy);
infile 'd:\temp\junk.txt';
array A{&n} $ A1-A&n ;
do i=1 to &N_Perms;
index=mod(i,&n)+1;
if index=1 then do;
input Dummy$ A{index} @@;
output;
end;
if index ne 1 then input Dummy$ A{index} @@;
end;
run;
* desired output;
proc print data=design2;
run;
Best regards,
John Hixon
Eastman Kodak Company
Rochester, NY, USA
716-726-1229
|