## Sum of next observations

Solved
Occasional Contributor
Posts: 9

# Sum of next observations

Dear all,

I want to create a code to sum up the three next months(observations) by Material: exemple:

Material               Time               Value          Solution

x                  Jan2013          12                    26 =10+11+5

x                  Feb2013          10                    46=11+5+30

x                  Mar2013          11                    35=5+11

x                  Apr2013            5                    30

x                  May2013          30

y                  Jan2013          12                    26 =10+11+5

y                  Feb2013          10                    46=11+5+30

y                  Mar2013          11                    35=5+11

y                  Apr2013            5                    30

y                  May2013          30

thank you.

regards.

Tou

Accepted Solutions
Solution
‎09-02-2013 12:08 PM
Posts: 3,167

## Re: Sum of next observations

data have;

input Material  :\$         Time  : monyy7.            Value     ;

format time monyy7.;

cards;

x                  Jan2013          12

x                  Feb2013          10

x                  Mar2013          11

x                  Apr2013            5

x                  May2013          30

y                  Jan2013          12

y                  Feb2013          10

y                  Mar2013          11

y                  Apr2013            5

y                  May2013          30

;

proc sort data=have;

by material descending time;

run;

data want;

do _n_=1 by 1 until (last.material);

set have;

by material descending time;

solution=sum(lag(value)*(_n_>1),lag2(value)*(_n_>2),lag3(value)*(_n_>3),0);

output;

end;

run;

proc sort data=want;

by material time;

run;

Haikuo

All Replies
Solution
‎09-02-2013 12:08 PM
Posts: 3,167

## Re: Sum of next observations

data have;

input Material  :\$         Time  : monyy7.            Value     ;

format time monyy7.;

cards;

x                  Jan2013          12

x                  Feb2013          10

x                  Mar2013          11

x                  Apr2013            5

x                  May2013          30

y                  Jan2013          12

y                  Feb2013          10

y                  Mar2013          11

y                  Apr2013            5

y                  May2013          30

;

proc sort data=have;

by material descending time;

run;

data want;

do _n_=1 by 1 until (last.material);

set have;

by material descending time;

solution=sum(lag(value)*(_n_>1),lag2(value)*(_n_>2),lag3(value)*(_n_>3),0);

output;

end;

run;

proc sort data=want;

by material time;

run;

Haikuo

Occasional Contributor
Posts: 9

## Re: Sum of next observations

thank you very much Haiku.

best regards.

Tou

🔒 This topic is solved and locked.