```Date: Wed, 24 Apr 1996 15:56:14 PDT Reply-To: Melvin Klassen Sender: "SAS(r) Discussion" From: Melvin Klassen Subject: Re: SAS Help Tony Gutschmidt <0002759027@MCIMAIL.COM> writes: >I can multiply the numbers by 100 to avoid round-off errors, and I >can go with no negative numbers but the assumption that I would never >want a four+ number combination is incorrect. The example I gave was >fairly simple, but there could be more than three numbers that add up >to the total I'm looking for. I could set an arbitrary limit like 8 >which would probably cover a lot of cases, but ideally the program could >handle any number. Again, I'd typically be looking at 30 records or less. Maybe SAS isn't the best programming-language for this type of problem ?! For example, the following FORTRAN program lists all combinations of up to 6 numbers which produce the specified sum. Given the specific sum, below, and 30 data-records, it ran in 0.2 seconds on an IBM mainframe computer, running the same Operating System as yours, and listed 598 combinations. Is one-fifth of a second "fast" enough for you? :-) Is 6 numbers enough for you? If not, just "extend" the program. It will take longer to run, by, let's guess, a factor of 300, but 300 times 0.2 seconds is just under 1 minute of CPU-time. No big deal. Lines marked with 'C*' are comments/benchmarking lines, and may be omitted. +++ C 30 data-points, taken up to 6 at a time, to total 1100. C Note that the data is sorted into ascending-order. This is essential! PROGRAM EIGHT C* INTEGER LC(6) PARAMETER (NOBS=30) INTEGER V(NOBS) DATA V/ * 100, 100, 100, 125, 125, * 150, 175, 200, 250, 300, * 300, 350, 400, 400, 425, * 450, 500, 550, 600, 625, * 650, 675, 700, 700, 725, * 750, 800, 825, 850, 875/ MAGIC = 1100 C* DO 50 K=1,6 C* 50 LC(K)=0 DO 100 L1=1,NOBS C* LC(1)=LC(1)+1 T=V(L1) IF (T .EQ. MAGIC) WRITE(*,*) V(L1) IF (T .GT. MAGIC) GOTO 101 DO 200 L2=(L1+1),NOBS C* LC(2)=LC(2)+1 T=V(L1)+V(L2) IF (T .EQ. MAGIC) WRITE(*,*) V(L1),V(L2) IF (T .GT. MAGIC) GOTO 201 DO 300 L3=(L2+1),NOBS C* LC(3)=LC(3)+1 T=V(L1)+V(L2)+V(L3) IF (T .EQ. MAGIC) WRITE(*,*) V(L1),V(L2),V(L3) IF (T .GT. MAGIC) GOTO 301 DO 400 L4=(L3+1),NOBS C* LC(4)=LC(4)+1 T=V(L1)+V(L2)+V(L3)+V(L4) IF (T .EQ. MAGIC) WRITE(*,*) V(L1),V(L2),V(L3),V(L4) IF (T .GT. MAGIC) GOTO 401 DO 500 L5=(L4+1),NOBS C* LC(5)=LC(5)+1 T=V(L1)+V(L2)+V(L3)+V(L4)+V(L5) IF (T .EQ. MAGIC) WRITE(*,*) V(L1),V(L2),V(L3),V(L4),V(L5) IF (T .GT. MAGIC) GOTO 501 DO 600 L6=(L5+1),NOBS C* LC(6)=LC(6)+1 T=V(L1)+V(L2)+V(L3)+V(L4)+V(L5)+V(L6) IF (T .EQ. MAGIC) WRITE(*,*) V(L1),V(L2),V(L3),V(L4),V(L5),V(L6) IF (T .GT. MAGIC) GOTO 601 600 CONTINUE 601 CONTINUE 500 CONTINUE 501 CONTINUE 400 CONTINUE 401 CONTINUE 300 CONTINUE 301 CONTINUE 200 CONTINUE 201 CONTINUE 100 CONTINUE 101 CONTINUE C* WRITE (*,*) LC END ```

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