![]() | ![]() | ![]() | ![]() |
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));
Product Family | Product | System | SAS Release | |
Reported | Fixed* | |||
SAS System | Base SAS | All | n/a |