Date: Fri, 5 Jun 1998 06:25:10 -0400
Reply-To: mwooding@erols.com
Sender: "SAS(r) Discussion" <SAS-L@UGA.CC.UGA.EDU>
From: Martha Wooding <mwooding@EROLS.COM>
Subject: Re: Word jumble problem: How to get all possible combinations?
Content-Type: text/plain; charset=us-ascii
I agree with Eckhard that the loops don't give the intended result. Here's
another solution that appears to work (I got the proper number of obs and could
not find duplicate letters when I scanned the printout). And to those who don't
like "messy character-numeric" conversions, this is not a production program.
Nat Wooding
Virginia Power
Richmond, Va
DATA str5;
LENGTH word $5 c1 c2 c3 c4 c5 $1;
drop c1 - c5 n1-n5;
DO n1=1 to 5;
DO n2=1 to 5;
DO n3=1 to 5;
DO n4=1 to 5;
DO n5=1 to 5;
c1= n1 ;C2=N2;c3=n3; c4=n4; c5=n5;
word=c1!!c2!!c3 !!c4!!c5;
word=translate(word,'abcde','12345'); * instead of using formats;
if (n1*n1+n2*n2+n3*n3+n4*n4+n5*n5 =55 ) then output; * a solution to
finding duplicate
letters;
END;
END;
END;
END;
END;
RUN;
proc print;run;
Becker, Eckhard [IAW-04] wrote:
> Jingren,
>
> you're right, your code is faster but it's wrong :-(
> E.g. the last loop in your code will generate 'EEEEE', and this is not
> wanted.
> You get the same number of obs, so something is missing:
> All your word's are 'sorted': The first letter is le the second and so on.
> E.g. the desired value 'ECDAB' is miisng in your output.
> Eckhard
> >Your code has excellent ideas. However, if it could be changed a little, it
> >will be more efficient.
> >
> >The doloop will be 5*4*3*2*1 instead of 5*5*5*5*5 plus a if-then with your
> >looping.
> >
> >PROC format;
> > VALUE letter 1='a'
> > 2='b'
> > 3='c'
> > 4='d'
> > 5='e';
> >RUN;
> >
> >DATA str5;
> > LENGTH word $5 c1 c2 c3 c4 c5 $1;
> > DO n1=1 to 5;
> > DO n2=2 to 5;
> > DO n3=3 to 5;
> > DO n4=4 to 5;
> > DO n5=5 to 5;
> > c1=PUT(n1,letter.);
> > c2=PUT(n2,letter.);
> > c3=PUT(n3,letter.);
> > c4=PUT(n4,letter.);
> > c5=PUT(n5,letter.);
> > word=c1!!c2!!c3!!c4!!c5;
> > OUTPUT;
> > END;
> > END;
> > END;
> > END;
> > END;
> >RUN;
> >
> >>>It generates a dataset with 120 unique combinations of the letters
> a,b,c,d
> >>>and e.
> >>>
> >>>PROC format;
> >>> VALUE letter 1='a'
> >>> 2='b'
> >>> 3='c'
> >>> 4='d'
> >>> 5='e';
> >>>RUN;
> >>>
> >>>DATA str5;
> >>> LENGTH word $5 c1 c2 c3 c4 c5 $1;
> >>> DO n1=1 to 5;
> >>> DO n2=1 to 5;
> >>> DO n3=1 to 5;
> >>> DO n4=1 to 5;
> >>> DO n5=1 to 5;
> >>> c1=PUT(n1,letter.);
> >>> c2=PUT(n2,letter.);
> >>> c3=PUT(n3,letter.);
> >>> c4=PUT(n4,letter.);
> >>> c5=PUT(n5,letter.);
> >>> word=c1!!c2!!c3!!c4!!c5;
> >>> IF INDEX(word,'a')>0 AND
> >>> INDEX(word,'b')>0 AND
> >>> INDEX(word,'c')>0 AND
> >>> INDEX(word,'d')>0 AND
> >>> INDEX(word,'e')>0 THEN OUTPUT;
> >>> END;
> >>> END;
> >>> END;
> >>> END;
> >>> END;
> >>>RUN;
|