Not applicable
Posts: 1

# how to sum a value in row 1 with row 2

I have a table with 600 observations (rows) with a numeric value for each row.

I want to create a new column (B) where B1 is the value in A1, B2 = A1+A2, B3 = A1+A2+A3 and so forth

Can this be done in Enterprise Guide? I have looked for it but could not find any solution.

I am not yet familiarized with SAS code but if anyone has the solútion in code I will try and use that but will be very useful if it can be done in EG1.

Regards

Casper

Super User
Posts: 10,848

B+A;

Super User
Posts: 9,813

## Re: how to sum a value in row 1 with row 2

I would add that you probably want a retain on that:

have:

A

1

4

5

want:

A     B

1     1

4     5

5     10

If so then:

data want;

set have;

retain b;

if _n_=1 then b=a;

else b=b+a;

run;

Posts: 3,867

## Re: how to sum a value in row 1 with row 2

You don't need RETAIN with SUM statement.

The variable on the left is initialized to 0 and retained.

Super User
Posts: 9,813

## Re: how to sum a value in row 1 with row 2

Sorry, not sure I follow you.  I was referring to the OP who put: " A1, B2 = A1+A2, B3 = A1+A2+A3 " so B in row 3 should be 1 + 4 + 5, so the value from the the last observation is added to the one on the current observation.  These two want's give different results:

data have;

a=1; output;

a=4; output;

a=5; output;

run;

data want1;

set have;

b=sum(a,b);

run;

data want2;

set have;

retain b;

if _n_=1 then b=a;

else b=b+a;

run;

Super User
Posts: 8,214

## Re: how to sum a value in row 1 with row 2

RW9: I think DN was referring to the following form:

Correct but still more complex than needed:

data want;

set have;

if _n_=1 then b=a;

else b+a;

run;

Better (per data_null_'s subsequent post):

data want;

set have;

b+a;

run;

Super User
Posts: 9,813

Ah, ok

Posts: 3,867

## Re: how to sum a value in row 1 with row 2

No I am referring to sum statement the reply from xia that RW9 is saying doesn't work.  RW9 doesn't know sum statement.

Posts: 3,867

## Re: how to sum a value in row 1 with row 2

RTM Not SUM function SUM STATEMENT.

data have;
a=1; output;
a=
4; output;
a=
5; output;

run;
data want1;
set have;
b+a;

run;
proc print;

run;
data want2;
set have;
retain b;
if _n_=1 then b=a;
else b=b+a;
run;
proc print;

run;

Not applicable
Posts: 1

## Re: how to sum a value in row 1 with row 2

Try this

DATA data1;

INFILE datalines;

INPUT A ;

B + A;

datalines;

8

10

3

7

12

4

9

15

10

2

;run;

proc print data=data1;run;

Super User
Posts: 23,980

## Re: how to sum a value in row 1 with row 2

If you have the license for SAS ETS this is called a running sum and it should be under:

Analyze -> Time Series -> Prepare Time Series Data

I think it should work even if you don't have time series data but don't have EG to actually test it with.

New Contributor
Posts: 3

## Re: how to sum a value in row 1 with row 2

If you were building in Enterprise Guide are you using data step (Program) or query builder?

In query builder you would write a series of 'Advanced Expressions' in the 'Computed Column' menu

For B1 it would be a simple t1.A1

For B2 it would be sum(t1.A1,t1.A2)

For B3 it would be sum(t1.A1,t1.A2,t1.A3)

and you would need to do one computed column at a time until you have all your columns (you can press the validate button in the advanced expression menu to test that the logic is correct)

in data step it would be

DATA test; (<Data set you are creating)

SET question; (<Data set you are calculating from)

b1=a1;

b2=a1+a2;

b3=a1+a2+a3;

RUN;

PROC Star
Posts: 1,333

## Re: how to sum a value in row 1 with row 2

I don't think there's a reasonable way to do it using the EG facilities. I recommend you follow the advice in the other posts to write a short SAS program to do it.

Tom

SAS Employee
Posts: 27

## Re: how to sum a value in row 1 with row 2

The query builder use SQL so in general, you cannot access the value of a previous rows as SQL does not in general terms respect a concept of data row order.

Now with said, might your data have some column that can be used as a unique identiifier, and might the summing be based on the order of that "unique" column... say somethig like date?

Given this assumption, you could use a self-join with an inequality and group summary.

Take for example the SASHELP.AIR table... date and total number of international miles traveled.   Join the AIR table with itself, and modify the join operator in the TABLES window so that T1.DATE >= T2.DATE.  In the SELECT DATA window, add t1.DATE, t1.AIR, and sum(T2.AIR).  Edit the groups to include only T1.DATE.

PROC SQL;
CREATE TABLE WORK.QUERY_FOR_AIR AS

SELECT t1.DATE, t2.AIR,

/* SUM_of_AIR */ (SUM (t2.AIR)) AS SUM_of_AIR

FROM SASHELP.AIR t1

INNER JOIN SASHELP.AIR t2 ON (t1.DATE >= t2.DATE)

GROUP BY t1.DATE;

;

Discussion stats
• 13 replies
• 1923 views
• 0 likes
• 10 in conversation