conditional loop

Solved
Frequent Contributor
Posts: 84

conditional loop

Hi,

i want sum of first 5 obs(1-5) then sum of next five obs(2-6) and so on...

data have;

input obs var1;

cards;

1 11

2 12

3 13

4 14

5 15

6 16

7 17

8 18

9 19

10 20

run;

Thanks..

Accepted Solutions
Solution
‎06-11-2015 08:05 AM
Super User
Posts: 10,787

Re: conditional loop

You didn't post the output you need yet .

Code: Program

`data have;input obs var1;cards;1 112 123 134 145 156 167 178 189 1910 20;run;proc sql; create table want as   select *,(select sum(var1) from have where obs between a.obs and a.obs+4) as sum   from have as a; quit;`

Xia Keshan

All Replies
Posts: 3,066

Re: conditional loop

data have;

input obs var1;

lag1=lag(var1);

lag2=lag2(var1);

lag3=lag3(var1);

lag4=lag4(var1);

sum_of_five=sum(var1,lag1,lag2,lag3,lag4);

cards;

1 11

2 12

3 13

4 14

5 15

6 16

7 17

8 18

9 19

10 20

run;

--
Paige Miller
Super User
Posts: 6,785

Re: conditional loop

Just to add a little variety:

sum_of_five + var1;

back5 = lag5(var1);

if back5 > . then sum_of_five = sum_of_five - back5;

Solution
‎06-11-2015 08:05 AM
Super User
Posts: 10,787

Re: conditional loop

You didn't post the output you need yet .

Code: Program

`data have;input obs var1;cards;1 112 123 134 145 156 167 178 189 1910 20;run;proc sql; create table want as   select *,(select sum(var1) from have where obs between a.obs and a.obs+4) as sum   from have as a; quit;`

Xia Keshan

Posts: 3,066

Re: conditional loop

Those are great ideas and certainly would be much less coding (especially if someone wanted the running average of 10 or 20 data points). Thanks!

--
Paige Miller
🔒 This topic is solved and locked.