Date: Tue, 23 Apr 1996 18:26:29 PDT
Reply-To: TWB2%Rates%FAR@GO50.COMP.PGE.COM
Sender: "SAS(r) Discussion" <SAS-L@UGA.CC.UGA.EDU>
From: TWB2%Rates%FAR@GO50.COMP.PGE.COM
Subject: Re: Combining Records To $3.00
Regarding finding all combinations totaling some amount:
%let goal=300;
DATA SKINNY/VIEW=SKINNY;
INFILE 'your.raw.data';
INPUT @1 ACCOUNT $10. @11 ASDOLLAR 10.2;
ASPENNY=ROUND(ASDOLLAR*100);
SERIAL=_N_;
RUN;
PROC SORT DATA=SKINNY OUT=SORTED;
BY ACCOUNT;
RUN;
PROC TRANSPOSE DATA=SKINNY OUT=PENNY PREFIX=PENNY;
BY ACCOUNT;
VAR ASPENNY;
RUN;
PROC TRANSPOSE DATA=SKINNY OUT=SERIAL PREFIX=SERIAL;
BY ACCOUNT;
VAR SERIAL;
RUN;
DATA WIDE/VIEW=WIDE;
MERGE PENNY
SERIAL;
BY ACCOUNT;
RUN;
* WIDE has variables ACCOUNT, PENNY1-PENNYn, SERIAL1-SERIALn;
* Find the value of n--it must be less than 100;
PROC MEANS NWAY NOPRINT DATA=SASHELP.VCOLUMN;
WHERE LIBNAME EQ 'WORK' AND MEMTYPE EQ 'VIEW' AND MEMNAME EQ 'WIDE'
AND NAME EQ: 'SERIAL';
CLASS TYPE;
VAR VARNUM;
OUTPUT OUT=UGLY
N(VARNUM)=VARCOUNT;
RUN;
DATA _NULL_;
SET UGLY;
CALL SYMPUT('VARCOUNT',VARCOUNT);
RUN;
DATA BRUTE;
SET WIDE;
LENGTH BIT $&varcount;
ARRAY _PENNY PENNY1-PENNY&varcount;
ARRAY _SERIAL SERIAL1-SERIAL&varcount;
DO VERYBAD=0 TO 2**&varcount-1;
BIT=PUT(VERYBAD,$BINARY&varcount..);
TOTAL=0;
DO MAYUSE=1 TO &varcount;
IF SUBSTR(BIT,MAYUSE,1) EQ '1'
THEN TOTAL=TOTAL+_PENNY(MAYUSE);
END;
IF TOTAL EQ &goal
THEN DO;
* Output routine left to the student;
END;
END;
RUN;
I cannot promise it is right--I'd test it a few time putting out the limits of
the loops.
Tim Berryhill - Contract Programmer and General Wizard
TWB2@PGE.COM or http://www.lookup.com/Homepages/92062/home.html
Frequently at Pacific Gas & Electric Co., San Francisco
The correlation coefficient between their views and
my postings is slightly less than 0
----------------------[Reply - Original Message]----------------------
Sent by:Tony Gutschmidt <0002759027@MCIMAIL.COM>
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.
>I'll assume that all values are positive,
>and that you never want a four (or five or six or ...) number combination,
>and that you've premultiplied each number by 100,
>to eliminate the fractions, and, thus, the possibility of "round-off".
-----------------
Forwarded Message
Date: Thu Apr 18, 1996 8:12 pm CST
Source-Date: Thu, 18 Apr 1996 18:18:18 PDT
From: Melvin Klassen
EMS: INTERNET / MCI ID: 376-5414
MBX: KLASSEN@uvvm.uvic.ca
TO: * Tony Gutschmidt / MCI ID: 275-9027
Subject: Re: SAS Help
Message-Id: 64960419021246/0003765414DC3EM
Source-Msg-Id: <96109.181818KLASSEN@UVVM.UVic.CA>
Tony Gutschmidt <0002759027@MCIMAIL.COM> writes:
>Take a set of numbers and come up with every possible combination
>that will sum up to a given total. For instance, let's say I have
>the following numbers and need to determine which ones add up to 3.00.
Summing "fractional" numbers is fraught with the danger of
"round-off" problems, i.e., the computer's sum of '0.1+0.9'
will be *LESS* than '1.0'.
>rec
> # value
>--- -----
> 1 1.00
> 2 1.00
> 3 1.00
> 4 1.25
> 5 1.25
> 6 1.50
> 7 1.75
> 8 2.00
> 9 2.50
>10 3.00
>
>This will be part of a tool I need to research billing records.
>The number of records will be variable, but typically under 30.
=====================================================================