In reply to GK.

You are asking for an exact permutation test. Do an internet search for the keywords "exact permutation test in sas iml"

Be careful to distinguish the permutations of the elements from the combinations of the indices. A permutation test takes all PERMUTATIONS of the samples, so there will certainly be duplicates among the labels/indices. For example, if G1={1,2,3} and G2={4,5}, then a valid permutation is G1={3,2,1} and G2={5,4}. There are 5!=120 different permutations, even though there are only "5 choose 2"=10 unique pairs of indices. You have to use the number of permutations to get the p-values to come out correctly. If you write a program that takes distinct combinations of indices, be sure to adjust the counts by the appropriate multiplicity (12, in this example).