Sum of next observations

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 9
Accepted Solution

Sum of next observations

Dear all,

could please help me with the following problem:

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

Could you please help.

thank you.

regards.

Tou


Accepted Solutions
Solution
‎09-02-2013 12:08 PM
Respected Advisor
Posts: 3,124

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

View solution in original post


All Replies
Solution
‎09-02-2013 12:08 PM
Respected Advisor
Posts: 3,124

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.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 190 views
  • 1 like
  • 2 in conversation