Help using Base SAS procedures

using retain function to obtain cumulative sum

Reply
Occasional Contributor
Posts: 7

using retain function to obtain cumulative sum

Hi, I am trying calculate the cumulative sum of the equations below. The retain function works but I have two variables that are multiplied by each other and they are indexed in the opposite direction. The two variables are c and s. Any help would be appreciated. Ravi


Year Equation
1982 c_1982*s_0
1983 c_1982*s_1+c_1983*s_0
1984 c_1982*s_2+c_1983*s_1+c_1984*s_0
1985 c_1982*s_3+c_1983*s_2+c_1984*s_1+c_1985*s_0
SAS Super FREQ
Posts: 8,739

Re: using retain function to obtain cumulative sum

Hi:
The SUM statement:
[pre]
var+something;
[/pre]

does an automatic RETAIN because of how this form of the SUM statement works, as described here:
http://support.sas.com/documentation/cdl/en/lrdict/63026/HTML/default/viewer.htm#a000289454.htm

If you CHANGED the SUM statement to be:
[pre]
var=var+something;
[/pre]

Then you would lose the automatic RETAIN and you would also need to use an explicit RETAIN statment:
[pre]
retain var;
var=var+something;
[/pre]

As you can see from this list of SAS functions, there is no RETAIN function.
http://support.sas.com/documentation/cdl/en/lrdict/63026/HTML/default/viewer.htm#a000245860.htm

The description of the explicit RETAIN statement is at the link below and there are some examples in the documentation of using the RETAIN statement:
http://support.sas.com/documentation/cdl/en/lrdict/63026/HTML/default/viewer.htm#a000214163.htm

For more help, perhaps you could post more of your code and the LOG messages that you are seeing when you run your program.

cynthia
Occasional Contributor
Posts: 7

Re: using retain function to obtain cumulative sum

hi Cynthia,

I have tried the retain function, but the problem is that the two variables that I am retaining are not moving in synchronous fashion, they are moving in opposite direction to each other.

Ravi
SAS Super FREQ
Posts: 8,739

Re: using retain function to obtain cumulative sum

Hi:
Without seeing ALL of the actual code that you are submitting, it really is just a guessing game. Also useful would be to see a sample of your input data and a concrete example of exactly what you mean when you say that the two variables are "not moving in synchronous fashion" but are "moving in opposite direction to each other."

For example, if I run this program:
[pre]
proc sort data=sashelp.class out=class;
by sex;
run;

data cumage;
set class;
by sex;
retain cumtot cumcnt ovtot ovcnt;
if _n_ = 1 then do;
ovtot = 0;
ovcnt = 0;
end;
if first.sex then do;
cumtot = 0;
cumcnt = 0;
end;
cumtot + age;
cumcnt + 1;
ovtot + age;
ovcnt + 1;
run;

ods listing close;
ods html file='c:\temp\cumage.html' style=sasweb;
proc print data=cumage;
title 'Show cumulative variables';
var name sex age cumtot cumcnt ovtot ovcnt;
run;
ods html close;
[/pre]

Then the output clearly shows that the retained variables are not "moving in opposite directions to each other" -- CUMTOT does contain the cumulative values for age and CUMCNT does contain a cumulative frequency count -- for each gender and OVTOT and OVCNT -- which were not reset do contain the overall cumulative total for all the ages and the overal cumulative count.

cynthia
[pre]
Show cumulative variables

Obs Name Sex Age cumtot cumcnt ovtot ovcnt
1 Alice F 13 13 1 13 1
2 Barbara F 13 26 2 26 2
3 Carol F 14 40 3 40 3
4 Jane F 12 52 4 52 4
5 Janet F 15 67 5 67 5
6 Joyce F 11 78 6 78 6
7 Judy F 14 92 7 92 7
8 Louise F 12 104 8 104 8
9 Mary F 15 119 9 119 9
10 Alfred M 14 14 1 133 10
11 Henry M 14 28 2 147 11
12 James M 12 40 3 159 12
13 Jeffrey M 13 53 4 172 13
14 John M 12 65 5 184 14
15 Philip M 16 81 6 200 15
16 Robert M 12 93 7 212 16
17 Ronald M 15 108 8 227 17
18 Thomas M 11 119 9 238 18
19 William M 15 134 10 253 19
[/pre]
Ask a Question
Discussion stats
  • 3 replies
  • 249 views
  • 0 likes
  • 2 in conversation