| Date: | Fri, 4 May 2007 11:56:50 -0400 |
| Reply-To: | "data _null_;" <datanull@GMAIL.COM> |
| Sender: | "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU> |
| From: | "data _null_;" <datanull@GMAIL.COM> |
| Subject: | Re: cumulative product? |
|
| In-Reply-To: | <BAY123-F36FBDE1D3A260C8C60F058DE400@phx.gbl> |
| Content-Type: | text/plain; charset=ISO-8859-1; format=flowed |
Actually with the proper initialization of VARCC is it more straight forward.
data test2 ;
varcc = 1;
do until(last.varb);
set test;
by vara varb;
varcc = varc * varcc;
output;
end;
run;
On 5/4/07, toby dunn <tobydunn@hotmail.com> wrote:
> Barry ,
>
> Okay While I encourage you to read Pauls most excellent paper, I would also
> say this is a bad example for the use of the DoW.... why you ask? Well
> because it just creates more code than is neccessary. See Example below:
>
> Now One other thing before the example noone so far as I can tell has
> actually made sure thet VarC was in the sort order of the data set, and it
> makes a big difference in the final Cumulative value.
>
>
>
> Data Need ;
> Set Have ;
> By VarA VarB VarC ;
> Retain CumVar ;
>
> If First.VarB Then CumVar = VarC ;
> Else CumVar = CumVar * VarC ;
>
> Run ;
>
>
>
>
>
>
> Toby Dunn
>
> You can see a lot by just looking. ~Yogi Berra
>
> Do not seek to follow in the footsteps of the wise. Seek what they sought.
> ~Matsuo Basho
>
> You never know what is enough, until you know what is more than enough.
> ~William Blake, Proverbs of Hell
>
>
>
>
>
> From: barry.debenham@TALK21.COM
> Reply-To: barry.debenham@TALK21.COM
> To: SAS-L@LISTSERV.UGA.EDU
> Subject: Re: cumulative product?
> Date: Fri, 4 May 2007 06:46:03 -0700
>
> On 3 May, 12:41, Huang...@PRINCIPAL.COM ("Huang, JS") wrote:
> > Celia:
> >
> > See if this is what you are looking for. If not, write back for more
> > details.
> >
> > data Test;
> > input VarA $ VarB $ VarC;
> > datalines;
> > a A 3
> > a A 4
> > a A 5
> > a B 4
> > a B 5
> > a B 6
> > a B 7
> > a C 5
> > a D 6
> > a D 7
> > b A 4
> > b A 5
> > b A 6
> > b B 5
> > b B 6
> > b B 7
> > b B 8
> > b C 6
> > b D 7
> > b D 8
> > ;
> >
> > proc sort data=Test;
> > by VarA VarB;
> > run;
> >
> > data Result;
> > retain CumuProduct;
> > set Test;
> > by VarA VarB;
> > if first.VarB then do;
> > CumuProduct=1;
> > end;
> > CumuProduct=CumuProduct*VarC;
> > run;
> >
> > proc print data=Result;
> > var VarA VarB VarC CumuProduct;
> > run;
> >
> > ***** Output *****
> > The SAS System 06:22
> > Thursday, May 3, 2007 3
> >
> > Cumu
> > Obs VarA VarB VarC Product
> >
> > 1 a A 3 3
> > 2 a A 4 12
> > 3 a A 5 60
> > 4 a B 4 4
> > 5 a B 5 20
> > 6 a B 6 120
> > 7 a B 7 840
> > 8 a C 5 5
> > 9 a D 6 6
> > 10 a D 7 42
> > 11 b A 4 4
> > 12 b A 5 20
> > 13 b A 6 120
> > 14 b B 5 5
> > 15 b B 6 30
> > 16 b B 7 210
> > 17 b B 8 1680
> > 18 b C 6 6
> > 19 b D 7 7
> > 20 b D 8 56
> >
> >
> >
> > -----Original Message-----
> > From: SAS(r) Discussion [mailto:S...@LISTSERV.UGA.EDU] On Behalf Of
> >
> > celia...@GMAIL.COM
> > Sent: Thursday, May 03, 2007 6:20 AM
> > To: S...@LISTSERV.UGA.EDU
> > Subject: cumulative product?
> >
> > Hi
> >
> > I knew there's certain way to calculate subgroup cumulative sum in a
> > table...but how can I calculate the cumulative product of subgroups
> > within a dataset?
> >
> > Say, the ovservations are sorted by variable A,B and C (namely
> > subgrouped twice), and I want to find out the product of all Cs within
> > each B-group. How could I do that?
> >
> > celia
> >
> > -----Message Disclaimer-----
> >
> > This e-mail message is intended only for the use of the individual or
> > entity to which it is addressed, and may contain information that is
> > privileged, confidential and exempt from disclosure under applicable law.
> > If you are not the intended recipient, any dissemination, distribution or
> > copying of this communication is strictly prohibited. If you have
> > received this communication in error, please notify us immediately by
> > reply email to Conn...@principal.com and delete or destroy all copies of
> > the original message and attachments thereto. Email sent to or from the
> > Principal Financial Group or any of its member companies may be retained
> > as required by law or regulation.
> >
> > Nothing in this message is intended to constitute an Electronic signature
> > for purposes of the Uniform Electronic Transactions Act (UETA) or the
> > Electronic Signatures in Global and National Commerce Act ("E-Sign")
> > unless a specific statement to the contrary is included in this message.
> >
> > While this communication may be used to promote or market a transaction
> > or an idea that is discussed in the publication, it is intended to
> provide
> > general information about the subject matter covered and is provided with
> > the understanding that The Principal is not rendering legal, accounting,
> > or tax advice. It is not a marketed opinion and may not be used to avoid
> > penalties under the Internal Revenue Code. You should consult with
> > appropriate counsel or other advisors on all matters pertaining to legal,
> > tax, or accounting obligations and requirements.- Hide quoted text -
> >
> > - Show quoted text
>
> Celia,
>
> I've been reading Paul Dorfmans paper "The Magnificent Do" and this
> seems like a good place to use the 'DOW' loop described in that paper.
> In essence the read (set) is carried out in an explicit do loop rather
> than SAS's own implicit DO loop that reads in the data for you. The
> paper explains this, and much more, with greater eloquence than I can
> here. The following code will do as you ask however.
>
> data Test;
> input VarA $ VarB $ VarC;
> datalines;
> a A 3
> a A 4
> a A 5
> a B 4
> a B 5
> a B 6
> a B 7
> a C 5
> a D 6
> a D 7
> b A 4
> b A 5
> b A 6
> b B 5
> b B 6
> b B 7
> b B 8
> b C 6
> b D 7
> b D 8
> ;
> run;
>
> data test2 ;
> varcc = 0;
> do until(last.varb);
> set test;
> by vara varb;
> if varcc eq 0 then varcc = varc;
> else varcc = varc * varcc;
> output;
> end;
> return;
> stop;
> run;
>
> the printed dataset looks like:
>
> Obs varcc VarA VarB VarC
>
> 1 3 a A 3
> 2 12 a A 4
> 3 60 a A 5
> 4 4 a B 4
> 5 20 a B 5
> 6 120 a B 6
> 7 840 a B 7
> 8 5 a C 5
> 9 6 a D 6
> 10 42 a D 7
> 11 4 b A 4
> 12 20 b A 5
> 13 120 b A 6
> 14 5 b B 5
> 15 30 b B 6
> 16 210 b B 7
> 17 1680 b B 8
> 18 6 b C 6
> 19 7 b D 7
> 20 56 b D 8
>
> regards,
>
> Barry
>
> _________________________________________________________________
> Exercise your brain! Try Flexicon.
> http://games.msn.com/en/flexicon/default.htm?icid=flexicon_hmemailtaglineapril07
>
|