Functions and CALL Routines |
Category: | Combinatorial |
Syntax |
CALL ALLCOMB(count, k, variable-1, ..., variable-n); |
specifies an integer variable that is assigned from 1 to the number of combinations in a loop.
specifies an integer constant, variable, or expression between 1 and n, inclusive, that specifies the number of items in each combination.
specifies either all numeric variables, or all character variables that have the same length. The values of these variables are permuted.
Requirement: | Initialize these variables before calling the ALLCOMB routine. |
Restriction: | Specify no more than 33 items. If you need to find combinations of more than 33 items, use the CALL ALLCOMBI routine. |
Tip: | After calling the ALLCOMB routine, the first k variables contain the values in one combination. |
Details |
Use the CALL ALLCOMB routine in a loop where the first argument to CALL ALLCOMB accepts each integral value from 1 to the number of combinations, and where k is constant. The number of combinations can be computed by using the COMB function. On the first call, the argument types and lengths are checked for consistency. On each subsequent call, the values of two variables are interchanged.
If you call the ALLCOMB routine with the first argument out of sequence, the results are not useful. In particular, if you initialize the variables and then immediately call ALLCOMB with a first argument of j, then you will not get the j^{th} combination (except when j is 1). To get the j^{th} combination, you must call ALLCOMB j times, with the first argument taking values from 1 through j in that exact order.
You can call the ALLCOMB routine when you use the %SYSCALL macro. In this case, the variable arguments are not required to be the same type or length. If %SYSCALL identifies an argument as numeric, then %SYSCALL reformats the returned value.
If an error occurs during the execution of the CALL ALLCOMB 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 is set to zero, and &SYSINFO is set to one of the following values:
0 if count=1
j if the values of variable-j and variable-k were interchanged, where j<k
-1 if no values were interchanged because all distinct combinations were already generated
Comparisons |
SAS provides four functions or CALL routines for generating combinations:
ALLCOMB generates all possible combinations of the values, missing or non-missing, of n variables. The values can be any numeric or character values. Each combination is formed from the previous combination by removing one value and inserting another value.
LEXCOMB generates all distinct combinations of the non-missing values of several variables. The values can be any numeric or character values. The combinations are generated in lexicographic order.
ALLCOMBI generates all combinations of the indices of n items, where indices are integers from 1 to n. Each combination is formed from the previous combination by removing one index and inserting another index.
LEXCOMBI generates all combinations of the indices of n items, where indices are integers from 1 to n. The combinations are generated in lexicographic order.
ALLCOMBI is the fastest of these functions and CALL routines. LEXCOMB is the slowest.
Examples |
The following is an example of the CALL ALLCOMB routine that is used with the DATA step.
data _null_; array x[5] $3 ('ant' 'bee' 'cat' 'dog' 'ewe'); n=dim(x); k=3; ncomb=comb(n,k); do j=1 to ncomb+1; call allcomb(j, k, of x[*]); put j 5. +3 x1-x3; end; run;
SAS writes the following output to the log:
1 ant bee cat 2 ant bee ewe 3 ant bee dog 4 ant cat dog 5 ant cat ewe 6 ant dog ewe 7 bee dog ewe 8 bee dog cat 9 bee ewe cat 10 dog ewe cat 11 dog ewe cat
The following is an example of the CALL ALLCOMB routine that is used with macros. The output includes values for the %SYSINFO macro.
%macro test; %let x1=ant; %let x2=-.1234; %let x3=1e10; %let x4=hippopotamus; %let x5=zebra; %let k=2; %let ncomb=%sysfunc(comb(5,&k)); %do j=1 %to &ncomb+1; %syscall allcomb(j, k, x1, x2, x3, x4, x5); %let jfmt=%qsysfunc(putn(&j,5.)); %let pad=%qsysfunc(repeat(%str(),30-%length(&x1 &x2))); %put &jfmt: &x1 &x2 &pad sysinfo=&sysinfo; %end; %mend; %test
SAS writes the following output to the log:
1: ant -0.1234 sysinfo=0 2: ant zebra sysinfo=2 3: ant hippopotamus sysinfo=2 4: ant 10000000000 sysinfo=2 5: -0.1234 10000000000 sysinfo=1 6: -0.1234 zebra sysinfo=2 7: -0.1234 hippopotamus sysinfo=2 8: 10000000000 hippopotamus sysinfo=1 9: 10000000000 zebra sysinfo=2 10: hippopotamus zebra sysinfo=1 11: hippopotamus zebra sysinfo=-1
See Also |
Functions and CALL Routines: |
Copyright © 2011 by SAS Institute Inc., Cary, NC, USA. All rights reserved.