Date: Tue, 18 Sep 2007 10:55:55 -0400 "Fehd, Ronald J. (CDC/CCHIS/NCPHI)" "SAS(r) Discussion" "Fehd, Ronald J. (CDC/CCHIS/NCPHI)" Re: problem of insertion sorting algorithms <1190125088.055151.273370@19g2000hsx.googlegroups.com> text/plain; charset=us-ascii

> From: yangsky66 > /*insertion sort algorithm*/ > /* 1. For j = 2 to length [A] do > 2. key = A[j] > 3. comment {Put A[j] into the sorted sequence A[1 . . j-1] > 4. i = j -1 > 5. while i > 0 and A[i] > key do > 6. A[i+1] = A[i] > 7. i = i-1 > /* 8. A[i+1] = key*/

the above is pseudo code

your translation of line 5 to sas 'do while' is incorrect.

see:

tip Knuth sorting by insertion

Ron Fehd the macro maven CDC Atlanta GA USA RJF2 at cdc dot gov

> proc iml; > A={5 2 4 6 1 3 }; > do j=2 to 6; > key=A[j]; > i=j-1; > do while((i>0)&& (A[i]>key)); > A[i+1]=A[i]; > i=i-1; > end; > A[i+1]=key; > end; > print a; > quit; > > > I try to a simple insertion sorting algorithms. SAS tells me > DO while is wrong. I still could not get results even though > I change to do while (A[i]>key); A[i+1]=A[i]; i=i-1; if i<=1 > then stop; end; > > > Could anybody tells me where is wrong in my code? > Thank you a lot > >

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