Usage Note 22135: Compute the number of permutations or combinations of n objects selected r at a time
Beginning in Version 7, 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);
Prior to Version 7, you can use the gamma or the exp and lgamma functions. 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 more information regarding factorials.) These formulas could be directly computed using the gamma or fact function. However, this works only when n is relatively small because the computations can overflow rather quickly. The exact limit before the gamma function overflows is machine dependent (it is, for example, greater than about 57! on an IBM mainframe and smaller on a VAX). Another approach is to use the exp and lgamma
functions by noting that in general:
A! B!
----- = EXP(LGAMMA(A+1)+LGAMMA(B+1)-LGAMMA(C+1)-LGAMMA(D+1)).
C! D!
Using this formula, the numbers of combinations and permutations of 5 things taken 2 at a time can be computed using these statements:
comb1 = gamma(6) / (gamma(3) * gamma(4));
perm1 = gamma(6) / gamma(4);
or
comb2 = exp(lgamma(6)-lgamma(3)-lgamma(4));
perm2 = exp(lgamma(6)-lgamma(4);
Operating System and Release Information
*
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.
| Type: | Usage Note |
| Priority: | low |
| Topic: | SAS Reference ==> DATA Step SAS Reference ==> Functions ==> Mathematical ==> COMB SAS Reference ==> Functions ==> Mathematical ==> PERM
|
| Date Modified: | 2007-10-23 12:50:04 |
| Date Created: | 2002-12-16 10:56:47 |