Date: Thu, 4 Mar 2010 04:02:15 -0800 Reply-To: Jerome Sender: "SAS(r) Discussion" From: Jerome Organization: http://groups.google.com Subject: Re: sum by every n rows. Comments: To: sas-l@uga.edu Content-Type: text/plain; charset=ISO-8859-1 Last version data tab1; input key val; datalines; 1 -1 2 -50 3 1 4 85 5 8 6 63 7 12 8 12 9 15 10 30 ; run; %let n_rows = 4; data out; set tab1 end=eof; retain _sum_by_n 0; retain i 1; _sum_by_n + val; if (i = &n_rows. or eof) then do; sum_by_n = _sum_by_n; _sum_by_n = 0; i=0; end; i = i +1; drop i _sum_by_n ; run; On Mar 4, 12:58 pm, Jerome wrote: > Hi, > > you can try this : > > data tab1; > input key val; > datalines; > 1 -1 > 2 -50 > 3 1 > 4 85 > 5 8 > 6 63 > 7 12 > 8 12 > 9 15 > 10 30 > ; > run; > %let n_rows = 4; > data out; > set tab1; > retain _sum_by_n 0; > retain i 1; > _sum_by_n + val; > if i = &n_rows. then do; > sum_by_n = _sum_by_n; > _sum_by_n = 0; > i=0; > end; > i = i +1; > drop i _sum_by_n ; > run; > > Bye. > Jerome. > > On Mar 4, 9:53 am, db wrote: > > > > > Hi, > > Could anyone help me on summing following dataset ? > > I have n, x variables and would like to calculate for sum by every 2 > > rows . > > > n x sum_by_every_2_rows > > 1 -1 > > 2 -50 -51 > > 3 -2 > > 4 -1 -3 > > 5 1 > > 6 -1 0 > > 7 3 > > 8 2 5 > > 9 2 > > 10 -2 0 > > 11 1 > > 12 2 3 > > 13 5 > > 14 40 45 > > 15 30 > > 16 39 69 > > 17 -2 -2 > > > Thanks, db- Hide quoted text - > > - Show quoted text -

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