From: db on
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

From: Jerome on
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 <daronnebonn...(a)gmail.com> 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

From: Jerome on
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 <jvanhe...(a)gmail.com> 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 <daronnebonn...(a)gmail.com> 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 -

From: data _null_; on
On Mar 4, 2:53 am, db <daronnebonn...(a)gmail.com> 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

data have;
input x @@;
cards;
-1 -50 -2 -1 1 -1 3 2 2 -2 1 2 5 40 30 39 -2
;;;;
run;
data need;
set have end=eof;
s + x;
if mod(_n_,2) eq 0 or eof /*for obs 17*/ then do;
sum = s;
s = 0;
end;
output;
drop s;
run;
proc print;
run;
From: Joe Matise on
Lots of ways to do that.

One:

data have;
input n x;
datalines;
1 -1
2 -50
3 -2
4 -1
5 1
6 -1
7 3
8 2
9 2
10 -2
11 1
12 2
13 5
14 40
15 30
16 39
17 -2
;;;;
run;
data want;

do _n_ = 1 by 1 until (mod(n,2)=0 or eof);
set have end=eof;
if _n_ = 1 then sum_by2 =0 ;
sum_by2 = sum_by2 + x;
end;
do _n_ = 1 by 1 until (mod(n,2)=0 or eof);
set have end=eof;
output;
end;
run;

Gives you it on both rows actually, but you can trivially strip it off of
one if you prefer.

-Joe


On Thu, Mar 4, 2010 at 2:53 AM, db <daronnebonneau(a)gmail.com> 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
>
 |  Next  |  Last
Pages: 1 2
Prev: hwo to pull years of data..
Next: IF-THEN