Permutes the values of the arguments, and returns a random combination of k out of n values.

Category: Combinatorial


CALL RANCOMB(seed, k, variable-1<, variable–2, ...> );

Required Arguments


is a numeric variable that contains the random number seed. For more information about seeds, see Seed Values.


is the number of values that you want to have in the random combination.


specifies all numeric variables, or all character variables that have the same length. K values of these variables are randomly permuted.


The Basics

If there are n variables, CALL RANCOMB permutes the values of the variables in such a way that the first k values are sorted in ascending order and form a random combination of k out of the n values. That is, all n!/(k!(n-k)!) combinations of k out of the n values are equally likely to be returned as the first k values.
If an error occurs during the execution of the CALL RANCOMB routine, then both of the following values are set:
  • &SYSERR is assigned a value that is greater than 4.
  • &SYSINFO is assigned a value that is less than -100.
If there are no errors, then &SYSERR and &SYSINFO are set to zero.

Using CALL RANCOMB with Macros

You can call the CALL RANCOMB routine when you use the %SYSCALL macro. In this case, the variable arguments are not required to be the same type or length. However, if the first k values that are returned include both character and numeric values, then those values are not sorted. If %SYSCALL identifies an argument as numeric, then %SYSCALL reformats the returned value.


Example 1: Using CALL RANCOMB in a DATA Step

The following example shows how to generate random combinations of given values by using the CALL RANCOMB routine.
data _null_;
   array x x1-x5 (1 2 3 4 5);
   seed = 1234567890123;
   do n=1 to 10;
      call rancomb(seed, 3, of x1-x5);
      put seed= @20 ' x= ' x1-x3;
Log Output from Using the CALL RANCOMB Routine in a DATA Step
seed=1332351321     x= 2 4 5
seed=829042065      x= 1 3 4
seed=767738639      x= 2 3 5
seed=1280236105     x= 2 4 5
seed=670350431      x= 1 2 5
seed=1956939964     x= 2 3 4
seed=353939815      x= 1 3 4
seed=1996660805     x= 1 2 5
seed=1835940555     x= 2 4 5
seed=910897519      x= 2 3 4

Example 2: Using CALL RANCOMB with a Macro

The following is an example of the CALL RANCOMB routine that is used with macros.
%macro test;
   %let x1=ant;
   %let x2=-.1234;
   %let x3=1e10;
   %let x4=hippopotamus;
   %let x5=zebra;
   %let k=3;
   %let seed = 12345;
   %do j=1 %to 10;
      %syscall rancomb(seed, k, x1, x2, x3, x4, x5);
      %put j=&j   &x1 &x2 &x3;
SAS writes the following output to the log:
j=1   -0.1234 hippopotamus zebra
j=2   hippopotamus -0.1234 10000000000
j=3   hippopotamus ant zebra
j=4   -0.1234 zebra ant
j=5   -0.1234 ant hippopotamus
j=6   10000000000 hippopotamus ant
j=7   10000000000 hippopotamus ant
j=8   ant 10000000000 -0.1234
j=9   zebra -0.1234 10000000000
j=10   zebra hippopotamus 10000000000