Date: Tue, 24 Nov 2009 12:42:32 -0500
Reply-To: Gerhard Hellriegel <gerhard.hellriegel@T-ONLINE.DE>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Gerhard Hellriegel <gerhard.hellriegel@T-ONLINE.DE>
Subject: Re: Urgent - Please help with the code
not really that what's in the example.
Maybe something like that:
data a;
input Val1;
cards;
10
.
20
30
.
40
50
30
.
.
10
20
30
40
.
;
run;
data s;
set a;
retain sum 0 cc 0;
array nn(0:5) _temporary_;
if cc=5 then do;
cc=0;
sum=0;
end;
if val1 ne . then do;
nn(0)=val1;
nn(5)=nn(4);
nn(4)=nn(3);
nn(3)=nn(2);
nn(2)=nn(1);
nn(1)=nn(0);
cc+1;
end;
sum=sum(nn(1),nn(2),nn(3),nn(4),nn(5));
drop cc;
run;
to write it more compact you might use a loop instead of
nn(5)=nn(4);
nn(4)=nn(3);
nn(3)=nn(2);
nn(2)=nn(1);
but its only 1 line less...
Gerhard
On Tue, 24 Nov 2009 16:44:24 +0100, =?ISO-8859-1?Q?Daniel_Fern=E1ndez?=
<fdezdan@GMAIL.COM> wrote:
>Not the best method but it works if not condition for val1 column exists:
>
>data need;
>set have;
>sum_last_5 =sum(lag(val1),lag2(val1),lag3(val1),lag4(val1),lag5(val1));
>run;
>
>
>Daniel Fernandez.
>Barcelona
>
>
>
>
>2009/11/24 Zish <zishanpurple@gmail.com>:
>> The data set looks like
>>
>> Val1 Sum of last 5 (Non Blank)
>> 10 .
>> . 10
>> 20 10
>> 30 30
>> . 60
>> 40 60
>> 50 100
>> 30 150
>> . 170
>> . 170
>> 10 170
>> 20 160
>> 30 150
>> 40 130
>> . 130
>>
>> I need to write a code which calculates the second column, which is
>> the sum of last five values present in column1.
>>