BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
kumarK
Quartz | Level 8

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..

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

You didn't post the output you need yet .

Code: Program

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;
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

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

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
Astounding
PROC Star

Just to add a little variety:

sum_of_five + var1;

back5 = lag5(var1);

if back5 > . then sum_of_five = sum_of_five - back5;

Ksharp
Super User

You didn't post the output you need yet .

Code: Program

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;
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

PaigeMiller
Diamond | Level 26

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 905 views
  • 4 likes
  • 4 in conversation