[bit.listserv.sas-l] SAS code for generating permutations

BICKIS@SASK.USASK.CA (Mikelis Bickis) (01/12/90)

/*
          Here is some SAS code to generate a dataset containing all
          the permutations of the integers 1 to n.  Before invoking
          the code you must %LET n= the number you want, and if you
          want to give the dataset a name, you may %LET FILE= the name
          you want.  This really should be written up as a bona fide
          macro, but our version of SAS doesn't seem to have the full
          SAS MACRO capability---but that is another issue.

                                     Mik Bickis
                                     Dept. of Mathematics
                                     University of Saskatchewan
                                     Saskatoon
                                     BICKIS@SASK.BITNET
                                     bickis@sask.usask.ca

<( I use SAS 5.18 running on a VAX 6300 cluster under VMS 5.1 )>
*/

DATA &FILE;
ARRAY NUMBERS {*} I1-I&n;
ARRAY TALLIES {*} T1-T&n;
KEEP I1-I&n;

/* Generate list of integers in normal order */
DO J=1 TO &n; NUMBERS{J}=J; END;
OUTPUT;
MIX=1;

DO WHILE(MIX<&n);
/* cyclically permute first MIX+1 numbers */
HOLD=NUMBERS{1};
DO J=1 TO MIX;
NUMBERS{J}=NUMBERS{J+1};
END;
NUMBERS{MIX+1}=HOLD;

TALLIES{MIX}+1;
IF TALLIES{MIX}<MIX+1 THEN DO;    OUTPUT;      MIX=1; END;
               /* If back to original permutation increment MIX */
                      ELSE DO; TALLIES{MIX}=0; MIX+1; END;
END;