Date: Fri, 11 May 2001 01:06:16 GMT Lou Pogoda "SAS(r) Discussion" Lou Pogoda Excite@Home - The Leader in Broadband http://home.com/faster Re: Permutations and combinations

Nalbo wrote in message >Hi all, > >Requirement: take a series of up to 9 letters and then work out every >permutation and output results to a data set for >later matching to a much larger data set of values. Code might also have to >work out combinations too. > >How the hell do I code something to work out combinations ? I can't see any >functions as such. Has anyone done anything like that and would you be >willing to share your code ? I've had a few abortive attempts but I'm >struggling. > The problem is essentially one of counting (in base 9), with perhaps some added twists. For instance, you don't say if, when you're starting with nine letters, whether or not an output consisting of only five letters is allowed, whether the selection is with or without replacement.

On the strength of the word "permutations" I'll assume that nine letters in means nine letters out, and that every letter must appear once and only once. The following code is simply a brute force "walk" through an array of nine elements, each of which holds a letter. It looks longer than it really is just because I like the pattern it makes. It'll grind on for quite a while and generate a lot of output. A quick eyeball of the output leads me to believe that it's correct, but no guarantees.

data letters; length l1 - l9 \$ 1; input l1 - l9 \$char1.; cards; a b c d e f g h i ; run; data combos (keep = j); length j \$ 9; set letters; array lets {9} l1 - l9; do a = 1 to 9; do b = 1 to 9; do c = 1 to 9; do d = 1 to 9; do e = 1 to 9; do f = 1 to 9; do g = 1 to 9; do h = 1 to 9; do i = 1 to 9; if a = b or a = c or a = d or a = e or a = f or a = g or a = h or a = i or b = c or b = d or b = e or b = f or b = g or b = h or b = i or c = d or c = e or c = f or c = g or c = h or c = i or d = e or d = f or d = g or d = h or d = i or e = f or e = g or e = h or e = i or f = g or f = h or f = i or g = h or g = i or h = i then ; else do; j = lets{a} || lets{b} || lets{c} || lets{d} || lets{e} || lets{f} || lets{g} || lets{h} || lets{i}; output; end; end; end; end; end; end; end; end; end; end; run;

Back to: Top of message | Previous page | Main SAS-L page