BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
How to sum only a few values in sas

Sum every 3 values leaving the first one
a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

b
sum(2,3,4)=9(in this manner)
12
15
18

Like this
1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

If so, then Proc Expand is the right tool.

 

data have;
   do a = 1 to 20;
      output;
   end;
run;

proc expand data = have(where = (a ne 1)) out = want;
   id a;
   Convert a = b / transformout = (reverse movsum 3 reverse);
run;

 

Result:

 

a   b
2   9
3   12
4   15
5   18
6   21
7   24
8   27
9   30
10  33
11  36
12  39
13  42
14  45
15  48
16  51
17  54
18  57
19  39
20  20

View solution in original post

6 REPLIES 6
PeterClemmensen
Tourmaline | Level 20

If so, then Proc Expand is the right tool.

 

data have;
   do a = 1 to 20;
      output;
   end;
run;

proc expand data = have(where = (a ne 1)) out = want;
   id a;
   Convert a = b / transformout = (reverse movsum 3 reverse);
run;

 

Result:

 

a   b
2   9
3   12
4   15
5   18
6   21
7   24
8   27
9   30
10  33
11  36
12  39
13  42
14  45
15  48
16  51
17  54
18  57
19  39
20  20
Divyaanshu8285
SAS Employee

Hi yes, I need a running sum...for example 

Pd

0
0.0003
0
0
0
0
0
1.25E-05
4.32E-05
4.32E-05
4.43E-05
4.43E-05
4.43E-05
0.000132
0.000132
0.000132
0.000135
0.000135
0.000134
0.000139
0.000139
0.000139
0.000142
0.000142
0.000142
0.00021
0.00021
0.00021
0.000203
0.000202
0.000202
0.000194
0.000194
0.000194
0.000185
0.000185
0.000185

 

leaving the first value i need sum from 2nd position to 13 position basically i need sum of 12 months pd.

Then I need sum from 3rd position to 14th position then 4th to 15th position likewise till there are less than 12 values left 

Divyaanshu8285
SAS Employee

Thanks for your support!

Tom
Super User Tom
Super User

Perhaps you meant SKIPPING the first one?  You don't seem to have left it as you did not include it in the output.

Let's make on input dataset named HAVE with the variable A you describe.

data have;
  do a=1 to 20;
    output;
  end;
run;

Now let's make a new dataset with the sum groups of three observations starting from the second observation.

data want;
  do n=1 to 3;
    set have(firstobs=2);
    b=sum(b,a);
  end;
  drop n;
run;

That results that differ from your example output. 9 18 27 36 45 54

 

So perhaps you meant something other than what you said?  Perhaps you meant you want a running sum of the last three values?

data want;
  set have;
  b + a;
  b + -lag3(a);
  if _n_ >= 4;
 * put b @;
run;

That results in the series 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57

 

Divyaanshu8285
SAS Employee

Hi yes, I need a running sum...for example 

Pd

0
0.0003
0
0
0
0
0
1.25E-05
4.32E-05
4.32E-05
4.43E-05
4.43E-05
4.43E-05
0.000132
0.000132
0.000132
0.000135
0.000135
0.000134
0.000139
0.000139
0.000139
0.000142
0.000142
0.000142
0.00021
0.00021
0.00021
0.000203
0.000202
0.000202
0.000194
0.000194
0.000194
0.000185
0.000185
0.000185

 

leaving the first value i need sum from 2nd position to 13 position basically i need sum of 12 months pd.

Then I need sum from 3rd position to 14th position then 4th to 15th position likewise till there are less than 12 values left 

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 6 replies
  • 576 views
  • 1 like
  • 3 in conversation