SUPPORT / SAMPLES & SAS NOTES
 

Support

Usage Note 22135: Compute the number of permutations or combinations of n objects selected r at a time

DetailsAboutRate It

Use the DATA step function comb to compute the number of combinations or perm to compute the number of permutations. For example, the following statements compute the number of permutations and the number of combinations of 5 things taken 2 at a time:

   c52=comb(5,2);
   p52=perm(5,2);

Note that the number of combinations of n objects selected r at a time is given by

   n! / [r!(n-r)!]

and the number of permutations by:

   n! / (n-r)!

See this note on computing factorials. These formulas can be directly computed using the gamma or fact function. Using the above example:

   c52 = fact(5) / (fact(2) * fact(3));
 or
   c52 = gamma(6) / (gamma(3) * gamma(4));

and

   p52 = fact(5) / fact(3);
 or
   p52 = gamma(6) / gamma(4);

However, this works only when n is relatively small because the computations can overflow rather quickly. The exact limit before overflow occurs is machine dependent. An approach to avoid overflows is to use the lcomb or lperm functions followed by the exp function, or to use the lgamma function followed by the exp function. Note that:

  (A! B!) / (C! D!) = EXP(LGAMMA(A+1)+LGAMMA(B+1)-LGAMMA(C+1)-LGAMMA(D+1)).

Again, using the above example:

   c52 = exp(lcomb(5,2));
   p52 = exp(lperm(5,2));

and

   c52 = exp(lgamma(6)-lgamma(3)-lgamma(4));
   p52 = exp(lgamma(6)-lgamma(4));


Operating System and Release Information

Product FamilyProductSystemSAS Release
ReportedFixed*
SAS SystemBase SASAlln/a
* For software releases that are not yet generally available, the Fixed Release is the software release in which the problem is planned to be fixed.