Here is my solution using base sas, I through it together pretty sloppily and did not account for the 6th clue yet at the time I wrote this. I am going to work on a more elegant solution. data _ps; array sd[5]; do i=100 to 236; ps=i**2; do d=1 to dim(sd); sd =substrn(ps,d,1); end; if max(of sd1-sd5)<=5 and min(of sd1-sd5)>0 then output; end; keep ps; run; proc transpose data=_ps out=have(drop=_:); run; proc contents data=have out=_contents noprint; run; proc sql noprint; select max(varnum) into :vmax from _contents; quit; proc datasets library=work nolist; delete _:; quit; data want; set have; array col[&vmax]; do i=1 to fact(dim(col)); call allperm(i, of col ); output; end; keep col1-col3; run; proc sort data=want nodupkey; by _all_; run; data want; set want; call sortn(of col1-col3); run; proc sort data=want nodupkey; by _all_; run; data want; /* update - I add this for step 6 */ retain yes '1'; set want; array _a[5]; array _b[5]; array _c[5]; do _i=1 to 5; _a[_i]=substrn(col1,_i,1); _b[_i]=substrn(col2,_i,1); _c[_i]=substrn(col3,_i,1); end; _nbrs=cats(of _a _b _c ); count1=count(_nbrs,'1'); count2=count(_nbrs,'2'); count3=count(_nbrs,'3'); count4=count(_nbrs,'4'); count5=count(_nbrs,'5'); if count1 in (2,3,4,5) and count2 in (1,3,4,5) and count3 in (1,2,4,5) and count4 in (1,2,3,5) and count5 in (1,2,3,4); _d1=count1; _d2=count2; _d3=count3; _d4=count4; _d5=count5; call sortn(of _d1-_d5); if cats(of _d1-_d5) eq '12345'; drop _:; run; /* update - I add the following to enforce rule 6 */ data _null_; set want; by yes; array count[5]; if first.yes then do; array _f[5]; end; do _i=1 to 5; if count[_i]=1 then _f[_i]+1; end; retain key; if last.yes then do; do _i=1 to 5; if _f[_i]=1 then key=_i; end; call symput('key',strip(key)); end; run; data answer; set want; where count&key=1; keep col:; run; proc print noobs; run;
... View more