```Date: Sun, 13 May 2001 14:33:48 -0500 Reply-To: shiling@math.wayne.edu Sender: "SAS(r) Discussion" From: Shiling Zhang Organization: Wayne State University Subject: Permutations and combinations Content-Type: text/plain; charset=us-ascii Here is a general macro solution for the permutation. 1241 %macro perm(n=6,stg='abcdefghijklmnopqrstuvwxyz'); 1242 1243 data combos; 1244 array lets(&n) \$1 _temporary_; 1245 do i=1 to &n; 1246 lets(i)=substr(&stg,i,1); 1247 end; 1248 * 1st loop; 1249 do _1 = 1 to &n; 1250 %do i = 2 %to &n; 1251 %str(*) &i.th loop; 1252 do _&i=1 to &n; 1253 %let ifst=if; 1254 %do j=1 %to &i-1; 1255 %let ifst=&ifst _&j=_&i; 1256 %if &j+1=&i %then %let ifst=&ifst then continue%str(;); 1257 %else %let ifst=&ifst or; 1258 %end; 1259 &ifst 1260 %end; 1261 %do i=1 %to &n; 1262 %if &i=1 %then %let letter=lets(_&i); 1263 %else %let letter= &letter || lets(_&i); 1264 %end; 1265 letter=&letter; 1266 output; 1267 %do i = 1 %to &n; 1268 end; 1269 %end; 1270 1271 keep letter; 1272 run ; 1273 1274 %mend; 1275 1276 options mprint; 1277 %perm 1278 MPRINT(PERM): DATA COMBOS; MPRINT(PERM): ARRAY LETS(6) \$1 _TEMPORARY_; MPRINT(PERM): DO I=1 TO 6; MPRINT(PERM): LETS(I)=SUBSTR('abcdefghijklmnopqrstuvwxyz',I,1); MPRINT(PERM): END; MPRINT(PERM): * 1ST LOOP; MPRINT(PERM): DO _1 = 1 TO 6; MPRINT(PERM): * 2TH LOOP; MPRINT(PERM): DO _2=1 TO 6; MPRINT(PERM): IF _1=_2 THEN CONTINUE; MPRINT(PERM): * 3TH LOOP; MPRINT(PERM): DO _3=1 TO 6; MPRINT(PERM): IF _1=_3 OR _2=_3 THEN CONTINUE; MPRINT(PERM): * 4TH LOOP; MPRINT(PERM): DO _4=1 TO 6; MPRINT(PERM): IF _1=_4 OR _2=_4 OR _3=_4 THEN CONTINUE; MPRINT(PERM): * 5TH LOOP; MPRINT(PERM): DO _5=1 TO 6; MPRINT(PERM): IF _1=_5 OR _2=_5 OR _3=_5 OR _4=_5 THEN CONTINUE; MPRINT(PERM): * 6TH LOOP; MPRINT(PERM): DO _6=1 TO 6; MPRINT(PERM): IF _1=_6 OR _2=_6 OR _3=_6 OR _4=_6 OR _5=_6 THEN CONTINUE; MPRINT(PERM): LETTER=LETS(_1) || LETS(_2) || LETS(_3) || LETS(_4) || LETS(_5) || LETS(_6); MPRINT(PERM): OUTPUT; MPRINT(PERM): END; MPRINT(PERM): END; MPRINT(PERM): END; MPRINT(PERM): END; MPRINT(PERM): END; MPRINT(PERM): END; MPRINT(PERM): KEEP LETTER; MPRINT(PERM): RUN ; NOTE: The data set WORK.COMBOS has 720 observations and 1 variables. NOTE: The DATA statement used 0.22 seconds. ************************************************************** 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. Thank you in advance. Alistair. ```

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