I did not use recursion, only one do loop surprisingly. I think it would be handled better with recursion in C, because of array pointers, based on my own experience. I do this kind of thing, on paper by hand, so much when I play my favorite logic puzzles. It was fun and frustrating too.
(Thanks for all the other submissions.)
data want (keep=a01-a10);
array a[0:10] a00-a10;
array sum[0:10] s00-s10;
x=1;
a[0]=0;
a[x]=a[x-1];
sum[0]=0;
goal=20;
do while (a[x]<10 and x>0); a[x]+1;
Sum[x]=sum[x-1]+a[x];
Just_right=sum[x]=goal;
If Just_right
then output;
GoToNext = sum[x]>=goal or x>9 or a[x]>9;
Promote_up= not GoToNext;
If GoToNext then do;
a[x]=.;
sum[x]=.;
x=x-1;/*demote index*/
end;
if Promote_up then do; /*Promote up index*/
x=x+1;
a[x]=a[x-1];
end;
end;
run;
... View more