<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Permutation in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Permutation/m-p/830095#M327994</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/129334"&gt;@dwarden3&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here are two other ways, both in base SAS:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;* Data Step
  Permutations as iterations over letter array and concatenation of elements;
data test1 (drop=letter: i j k);
  length permutation $3;
  array letter {3} $ ('A','B','C');
  do i = 1 to 3;
    do j = 1 to 3;
      do k = 1 to 3;
        permutation = catt(letter{i},letter{j},letter{k});
        output;
      end;
    end;
  end;
run;


* Proc SQL 
  Permutations as cartesian products of joining letter data set and concatenation of elements;
proc sql;
  create table t1 (letter char(1));

  insert into t1 
    set letter = 'A'
    set letter = 'B'
    set letter = 'C';

  create table t2 as
    select catt(a.letter,b.letter) as permutation
    from t1 as a, t1 as b;

  create table test2 as
    select catt(a.permutation,b.letter) as permutation
    from t2 as a, t1 as b;
quit;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 24 Aug 2022 14:57:45 GMT</pubDate>
    <dc:creator>ErikLund_Jensen</dc:creator>
    <dc:date>2022-08-24T14:57:45Z</dc:date>
    <item>
      <title>Permutation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Permutation/m-p/830023#M327954</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to code all of the permutations of given length such that I get the following in one dataset:&amp;nbsp;&lt;/P&gt;&lt;P&gt;a&lt;/P&gt;&lt;P&gt;b&lt;/P&gt;&lt;P&gt;c&lt;/P&gt;&lt;P&gt;..............&lt;/P&gt;&lt;P&gt;a a&amp;nbsp;&lt;/P&gt;&lt;P&gt;a b&lt;/P&gt;&lt;P&gt;b a&lt;/P&gt;&lt;P&gt;.............&lt;/P&gt;&lt;P&gt;a a a&amp;nbsp;&lt;/P&gt;&lt;P&gt;a a b&lt;/P&gt;&lt;P&gt;a a c&lt;/P&gt;&lt;P&gt;a a d&lt;/P&gt;&lt;P&gt;a a e&lt;/P&gt;&lt;P&gt;...........&lt;/P&gt;&lt;P&gt;etc.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there a way to do this iteratively in either base SAS or IML? I am able to make this using repetition in IML.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Current code example (but repeated out to however many times):&lt;/P&gt;&lt;P&gt;proc IML;&lt;BR /&gt;c_num=repeat(1,5);&lt;BR /&gt;perm1=T(1:5);&lt;BR /&gt;p_num=T(1:5);&lt;BR /&gt;index=T(1:5);&lt;BR /&gt;create OutData1_org var {c_num, perm1, p_num, index}; /* create Work.OutData for writing */&lt;BR /&gt;append; /* write data in x and y */&lt;BR /&gt;close OutData1_org;&lt;/P&gt;&lt;P&gt;data OutData1;&lt;BR /&gt;set OutData1_org;&lt;BR /&gt;format perm $18.;&lt;BR /&gt;perm=cats(perm1);&lt;BR /&gt;drop perm1;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;proc IML;&lt;BR /&gt;a=T(1:5);&lt;BR /&gt;c_num=repeat(2,25);&lt;BR /&gt;perm1=repeat(a, {5 5 5 5 5});&lt;BR /&gt;perm2=repeat(1:5,5);&lt;BR /&gt;p_num=T(6:30);&lt;BR /&gt;index=T(1:25);&lt;BR /&gt;create OutData2_old var {c_num, perm1, perm2,p_num, index}; /* create Work.OutData for writing */&lt;BR /&gt;append; /* write data in x and y */&lt;BR /&gt;close OutData2_old;&lt;/P&gt;&lt;P&gt;data OutData2;&lt;BR /&gt;set OutData2_old;&lt;BR /&gt;perm_test=put(perm1,$1.)||" "||put(perm2,$1.);&lt;BR /&gt;perm=put(perm_test, $18.);&lt;BR /&gt;drop perm1 perm2 perm_test;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data Perms;&lt;BR /&gt;set OutData1 OutData2;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Wed, 24 Aug 2022 03:57:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Permutation/m-p/830023#M327954</guid>
      <dc:creator>dwarden3</dc:creator>
      <dc:date>2022-08-24T03:57:35Z</dc:date>
    </item>
    <item>
      <title>Re: Permutation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Permutation/m-p/830070#M327979</link>
      <description>&lt;P&gt;Do you want permutations or combinations? Run the following program, which shows how to get all permutation (by using the ALLPERM function) or all combinations of k elements (by using the ALLCOMB function):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
set = 1:3;
/* generate all permutations of 3 elements */
p = allperm(set);
print p;

/* if you want letters, index into the set {A, B, C} */
letters = {A B C};
p2 = letters[p];
p3 = shape(p2, 0, 3);
print p3;

/* generate all combination of 3 elements taken k at a time */
do k = 1 to 3;
   ck = allcomb(3, k);
   print "----" k "----", ck;
end;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;For more information, see&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;How to use the ALLPERMI function in the DATA step:&amp;nbsp;&lt;A href="https://blogs.sas.com/content/iml/2010/10/13/generate-all-permutations-in-sas.html" target="_blank"&gt;https://blogs.sas.com/content/iml/2010/10/13/generate-all-permutations-in-sas.html&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;How to use the ALLCOMB function in IML or in the DATA step:&amp;nbsp;&lt;A href="https://blogs.sas.com/content/iml/2013/09/30/generate-combinations-in-sas.html" target="_blank"&gt;https://blogs.sas.com/content/iml/2013/09/30/generate-combinations-in-sas.html&lt;/A&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 24 Aug 2022 13:16:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Permutation/m-p/830070#M327979</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2022-08-24T13:16:45Z</dc:date>
    </item>
    <item>
      <title>Re: Permutation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Permutation/m-p/830095#M327994</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/129334"&gt;@dwarden3&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here are two other ways, both in base SAS:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;* Data Step
  Permutations as iterations over letter array and concatenation of elements;
data test1 (drop=letter: i j k);
  length permutation $3;
  array letter {3} $ ('A','B','C');
  do i = 1 to 3;
    do j = 1 to 3;
      do k = 1 to 3;
        permutation = catt(letter{i},letter{j},letter{k});
        output;
      end;
    end;
  end;
run;


* Proc SQL 
  Permutations as cartesian products of joining letter data set and concatenation of elements;
proc sql;
  create table t1 (letter char(1));

  insert into t1 
    set letter = 'A'
    set letter = 'B'
    set letter = 'C';

  create table t2 as
    select catt(a.letter,b.letter) as permutation
    from t1 as a, t1 as b;

  create table test2 as
    select catt(a.permutation,b.letter) as permutation
    from t2 as a, t1 as b;
quit;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 24 Aug 2022 14:57:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Permutation/m-p/830095#M327994</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2022-08-24T14:57:45Z</dc:date>
    </item>
    <item>
      <title>Re: Permutation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Permutation/m-p/830103#M328002</link>
      <description>&lt;P&gt;Some time ago I did this macro (tested up to 13), maybe you find it useful:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
  value perm
    1  = A
    2  = B
    3  = C
    4  = D
    5  = E
    6  = F
    7  = G
    8  = H
    9  = J
    10 = J
    11 = K
    12 = L
    13 = M
    other = "X"
  ;
run;
 
%macro perm(level,print=1);
  %do level = 1 %to &amp;amp;level.;
    %if 1 = &amp;amp;level. %then
      %do;
        data _1;
          _1_1 = 1;
        run;
      %end;
    %else
      %do;
        data _&amp;amp;level.;
          set _%eval(&amp;amp;level. - 1);
          array old[*] _%eval(&amp;amp;level. - 1)_:;
          array new[&amp;amp;level.] _%eval(&amp;amp;level.)_1 - _%eval(&amp;amp;level.)_&amp;amp;level.;
 
          do j = 1 to &amp;amp;level.;
            new[j] = &amp;amp;level.;
            do k = 1 to %eval(&amp;amp;level. - 1);
              new[k+(k&amp;gt;=j)] = old[k];               
            end;
            output;
          end;
        keep _%eval(&amp;amp;level.)_:;
        run;
 
        proc delete data = _%eval(&amp;amp;level. - 1);
        run;
      %end;
  %end;
 
  %if 1 = &amp;amp;print. %then
    %do;
      proc sort data = _%eval(&amp;amp;level. - 1);
        by _all_;
      run;
      proc print data = _%eval(&amp;amp;level. - 1);
        format _all_ perm.;
      run;
    %end;
%mend perm;
 
%perm(5);
 
%perm(8,print=0);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you comment out:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;        proc delete data = _%eval(&amp;amp;level. - 1);
        run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;you will get all intermediate datasets.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;[EDIT:]&lt;/P&gt;
&lt;P&gt;Format _perm_ is just for print out, but you can easily modify final dataset with it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Wed, 24 Aug 2022 15:32:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Permutation/m-p/830103#M328002</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2022-08-24T15:32:31Z</dc:date>
    </item>
    <item>
      <title>Re: Permutation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Permutation/m-p/830267#M328064</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro perm(n=);
proc plan seed=12345 ;
%do i=1 %to &amp;amp;n.;
factors
 %do j=1 %to &amp;amp;i.;
   x&amp;amp;j.=&amp;amp;n. perm
 %end;
/noprint;
output out=x&amp;amp;i.;
run;
%end;
quit;

data want;
 set x1-x&amp;amp;n.;
run;
%mend;


%perm(n=5)&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 25 Aug 2022 11:49:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Permutation/m-p/830267#M328064</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-08-25T11:49:57Z</dc:date>
    </item>
    <item>
      <title>Re: Permutation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Permutation/m-p/831110#M328426</link>
      <description>&lt;P&gt;Hi Rick,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Set p you made is correct except I also need: 000, 111, 444 etc. and things like 501,432, etc. I want all of the possible combinations that are x long from y items (0 to 5).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So when the set is length 1, I would have: 0, 1, 2, 3, 4, 5.&lt;/P&gt;&lt;P&gt;With two: 0 0, 0 1, 0 2 ,0 3, 0 4, 0 5, 1 0 , 1 1, etc.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;</description>
      <pubDate>Tue, 30 Aug 2022 15:10:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Permutation/m-p/831110#M328426</guid>
      <dc:creator>dwarden3</dc:creator>
      <dc:date>2022-08-30T15:10:18Z</dc:date>
    </item>
    <item>
      <title>Re: Permutation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Permutation/m-p/831111#M328427</link>
      <description>&lt;P&gt;I see. You don't want permutations nor do you want combinations. You are looking for the Cartesian product of a set with itself k times, where k=1,2, and 3.&amp;nbsp; In the SAS/IML language, you can form a Cartesian product by using the ExpandGrid function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to write this information to a rectangular data set, you need to decide how to combine the results that only have 1 column (k=1) with the results that have two or three columns (k=2 and k=3). In the following program, I used missing values for the columns that are not used.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
set = T( 0:5 );
/* generate all possible triplets (x1,x2,x3) where the x_i are in {0,1,2,3,4,5} */
p1 = set;
p2 = expandgrid(set, set);
p3 = expandgrid(set, set, set);


/* write to data set */
create Values var {Length, x1, x2, x3};

N = nrow(p1); /* N = 6 */
Length=j(N,1,1);
x1 = p1;  x2=j(N,1,.); x3=j(N,1,.); 
append;

N = nrow(p2); /* N = 6**2 = 36  */
Length=j(N,1,2);
x1 = p2[,1];  x2=p2[,2]; x3=j(N,1,.); 
append;

N = nrow(p3); /* N = 6**3 = 216 */
Length=j(N,1,3);
x1 = p3[,1];  x2=p3[,2]; x3=p3[,3];
append;

close;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 30 Aug 2022 15:42:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Permutation/m-p/831111#M328427</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2022-08-30T15:42:57Z</dc:date>
    </item>
    <item>
      <title>Re: Permutation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Permutation/m-p/831113#M328428</link>
      <description>&lt;P&gt;What you show as output does not look at all like a dataset.&amp;nbsp; Datasets have a fixed number of variables.&amp;nbsp; You cannot have 3 rows with only one variable and then another three rows with 2 variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So what form do you want the output to take?&lt;/P&gt;</description>
      <pubDate>Tue, 30 Aug 2022 15:44:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Permutation/m-p/831113#M328428</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-08-30T15:44:41Z</dc:date>
    </item>
    <item>
      <title>Re: Permutation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Permutation/m-p/831166#M328442</link>
      <description>Thank you so much! I knew there was a way to do this with IML.</description>
      <pubDate>Tue, 30 Aug 2022 22:14:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Permutation/m-p/831166#M328442</guid>
      <dc:creator>dwarden3</dc:creator>
      <dc:date>2022-08-30T22:14:25Z</dc:date>
    </item>
  </channel>
</rss>

