<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: using retain function to obtain cumulative sum in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/using-retain-function-to-obtain-cumulative-sum/m-p/16360#M2985</link>
    <description>Hi:&lt;BR /&gt;
  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."&lt;BR /&gt;
 &lt;BR /&gt;
  For example, if I run this program:&lt;BR /&gt;
[pre]&lt;BR /&gt;
proc sort data=sashelp.class out=class;&lt;BR /&gt;
  by sex;&lt;BR /&gt;
run;&lt;BR /&gt;
                    &lt;BR /&gt;
data cumage;&lt;BR /&gt;
  set class;&lt;BR /&gt;
  by sex;&lt;BR /&gt;
  retain cumtot cumcnt ovtot ovcnt;&lt;BR /&gt;
  if _n_ = 1 then do;&lt;BR /&gt;
     ovtot = 0;&lt;BR /&gt;
     ovcnt = 0;&lt;BR /&gt;
  end;&lt;BR /&gt;
  if first.sex then do;&lt;BR /&gt;
     cumtot = 0;&lt;BR /&gt;
     cumcnt = 0;&lt;BR /&gt;
  end;&lt;BR /&gt;
  cumtot + age;&lt;BR /&gt;
  cumcnt + 1;&lt;BR /&gt;
  ovtot + age;&lt;BR /&gt;
  ovcnt + 1;&lt;BR /&gt;
run;&lt;BR /&gt;
                                      &lt;BR /&gt;
ods listing close;&lt;BR /&gt;
ods html file='c:\temp\cumage.html' style=sasweb;&lt;BR /&gt;
proc print data=cumage;&lt;BR /&gt;
  title 'Show cumulative variables';&lt;BR /&gt;
  var name sex age cumtot cumcnt ovtot ovcnt;&lt;BR /&gt;
run;&lt;BR /&gt;
ods html close;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
                                 &lt;BR /&gt;
  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.&lt;BR /&gt;
&lt;BR /&gt;
cynthia&lt;BR /&gt;
[pre]&lt;BR /&gt;
Show cumulative variables&lt;BR /&gt;
                            &lt;BR /&gt;
Obs    Name       Sex    Age    cumtot    cumcnt    ovtot    ovcnt&lt;BR /&gt;
  1    Alice       F      13       13        1        13        1&lt;BR /&gt;
  2    Barbara     F      13       26        2        26        2&lt;BR /&gt;
  3    Carol       F      14       40        3        40        3&lt;BR /&gt;
  4    Jane        F      12       52        4        52        4&lt;BR /&gt;
  5    Janet       F      15       67        5        67        5&lt;BR /&gt;
  6    Joyce       F      11       78        6        78        6&lt;BR /&gt;
  7    Judy        F      14       92        7        92        7&lt;BR /&gt;
  8    Louise      F      12      104        8       104        8&lt;BR /&gt;
  9    Mary        F      15      119        9       119        9&lt;BR /&gt;
 10    Alfred      M      14       14        1       133       10&lt;BR /&gt;
 11    Henry       M      14       28        2       147       11&lt;BR /&gt;
 12    James       M      12       40        3       159       12&lt;BR /&gt;
 13    Jeffrey     M      13       53        4       172       13&lt;BR /&gt;
 14    John        M      12       65        5       184       14&lt;BR /&gt;
 15    Philip      M      16       81        6       200       15&lt;BR /&gt;
 16    Robert      M      12       93        7       212       16&lt;BR /&gt;
 17    Ronald      M      15      108        8       227       17&lt;BR /&gt;
 18    Thomas      M      11      119        9       238       18&lt;BR /&gt;
 19    William     M      15      134       10       253       19&lt;BR /&gt;
[/pre]</description>
    <pubDate>Tue, 19 Oct 2010 23:45:28 GMT</pubDate>
    <dc:creator>Cynthia_sas</dc:creator>
    <dc:date>2010-10-19T23:45:28Z</dc:date>
    <item>
      <title>using retain function to obtain cumulative sum</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/using-retain-function-to-obtain-cumulative-sum/m-p/16357#M2982</link>
      <description>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&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Year	         Equation&lt;BR /&gt;
1982        c_1982*s_0&lt;BR /&gt;
1983	c_1982*s_1+c_1983*s_0&lt;BR /&gt;
1984	c_1982*s_2+c_1983*s_1+c_1984*s_0&lt;BR /&gt;
1985        c_1982*s_3+c_1983*s_2+c_1984*s_1+c_1985*s_0</description>
      <pubDate>Wed, 13 Oct 2010 18:30:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/using-retain-function-to-obtain-cumulative-sum/m-p/16357#M2982</guid>
      <dc:creator>ravi101</dc:creator>
      <dc:date>2010-10-13T18:30:42Z</dc:date>
    </item>
    <item>
      <title>Re: using retain function to obtain cumulative sum</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/using-retain-function-to-obtain-cumulative-sum/m-p/16358#M2983</link>
      <description>Hi:&lt;BR /&gt;
  The SUM statement:&lt;BR /&gt;
[pre]&lt;BR /&gt;
var+something;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
                      &lt;BR /&gt;
does an automatic RETAIN because of how this form of the SUM statement works, as described here:&lt;BR /&gt;
&lt;A href="http://support.sas.com/documentation/cdl/en/lrdict/63026/HTML/default/viewer.htm#a000289454.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/lrdict/63026/HTML/default/viewer.htm#a000289454.htm&lt;/A&gt;&lt;BR /&gt;
&lt;BR /&gt;
 If you CHANGED the SUM statement to be:&lt;BR /&gt;
[pre]&lt;BR /&gt;
var=var+something;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
                      &lt;BR /&gt;
Then you would lose the automatic RETAIN and you would also need to use an explicit RETAIN statment:&lt;BR /&gt;
[pre]&lt;BR /&gt;
retain var;&lt;BR /&gt;
var=var+something;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
         &lt;BR /&gt;
As you can see from this list of SAS functions, there is no RETAIN function.&lt;BR /&gt;
&lt;A href="http://support.sas.com/documentation/cdl/en/lrdict/63026/HTML/default/viewer.htm#a000245860.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/lrdict/63026/HTML/default/viewer.htm#a000245860.htm&lt;/A&gt; &lt;BR /&gt;
       &lt;BR /&gt;
 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:&lt;BR /&gt;
&lt;A href="http://support.sas.com/documentation/cdl/en/lrdict/63026/HTML/default/viewer.htm#a000214163.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/lrdict/63026/HTML/default/viewer.htm#a000214163.htm&lt;/A&gt;&lt;BR /&gt;
&lt;BR /&gt;
For more help, perhaps you could post more of your code and the LOG messages that you are seeing when you run your program.&lt;BR /&gt;
 &lt;BR /&gt;
cynthia</description>
      <pubDate>Wed, 13 Oct 2010 19:37:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/using-retain-function-to-obtain-cumulative-sum/m-p/16358#M2983</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2010-10-13T19:37:06Z</dc:date>
    </item>
    <item>
      <title>Re: using retain function to obtain cumulative sum</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/using-retain-function-to-obtain-cumulative-sum/m-p/16359#M2984</link>
      <description>hi Cynthia,&lt;BR /&gt;
&lt;BR /&gt;
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.&lt;BR /&gt;
&lt;BR /&gt;
Ravi</description>
      <pubDate>Tue, 19 Oct 2010 22:20:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/using-retain-function-to-obtain-cumulative-sum/m-p/16359#M2984</guid>
      <dc:creator>ravi101</dc:creator>
      <dc:date>2010-10-19T22:20:31Z</dc:date>
    </item>
    <item>
      <title>Re: using retain function to obtain cumulative sum</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/using-retain-function-to-obtain-cumulative-sum/m-p/16360#M2985</link>
      <description>Hi:&lt;BR /&gt;
  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."&lt;BR /&gt;
 &lt;BR /&gt;
  For example, if I run this program:&lt;BR /&gt;
[pre]&lt;BR /&gt;
proc sort data=sashelp.class out=class;&lt;BR /&gt;
  by sex;&lt;BR /&gt;
run;&lt;BR /&gt;
                    &lt;BR /&gt;
data cumage;&lt;BR /&gt;
  set class;&lt;BR /&gt;
  by sex;&lt;BR /&gt;
  retain cumtot cumcnt ovtot ovcnt;&lt;BR /&gt;
  if _n_ = 1 then do;&lt;BR /&gt;
     ovtot = 0;&lt;BR /&gt;
     ovcnt = 0;&lt;BR /&gt;
  end;&lt;BR /&gt;
  if first.sex then do;&lt;BR /&gt;
     cumtot = 0;&lt;BR /&gt;
     cumcnt = 0;&lt;BR /&gt;
  end;&lt;BR /&gt;
  cumtot + age;&lt;BR /&gt;
  cumcnt + 1;&lt;BR /&gt;
  ovtot + age;&lt;BR /&gt;
  ovcnt + 1;&lt;BR /&gt;
run;&lt;BR /&gt;
                                      &lt;BR /&gt;
ods listing close;&lt;BR /&gt;
ods html file='c:\temp\cumage.html' style=sasweb;&lt;BR /&gt;
proc print data=cumage;&lt;BR /&gt;
  title 'Show cumulative variables';&lt;BR /&gt;
  var name sex age cumtot cumcnt ovtot ovcnt;&lt;BR /&gt;
run;&lt;BR /&gt;
ods html close;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
                                 &lt;BR /&gt;
  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.&lt;BR /&gt;
&lt;BR /&gt;
cynthia&lt;BR /&gt;
[pre]&lt;BR /&gt;
Show cumulative variables&lt;BR /&gt;
                            &lt;BR /&gt;
Obs    Name       Sex    Age    cumtot    cumcnt    ovtot    ovcnt&lt;BR /&gt;
  1    Alice       F      13       13        1        13        1&lt;BR /&gt;
  2    Barbara     F      13       26        2        26        2&lt;BR /&gt;
  3    Carol       F      14       40        3        40        3&lt;BR /&gt;
  4    Jane        F      12       52        4        52        4&lt;BR /&gt;
  5    Janet       F      15       67        5        67        5&lt;BR /&gt;
  6    Joyce       F      11       78        6        78        6&lt;BR /&gt;
  7    Judy        F      14       92        7        92        7&lt;BR /&gt;
  8    Louise      F      12      104        8       104        8&lt;BR /&gt;
  9    Mary        F      15      119        9       119        9&lt;BR /&gt;
 10    Alfred      M      14       14        1       133       10&lt;BR /&gt;
 11    Henry       M      14       28        2       147       11&lt;BR /&gt;
 12    James       M      12       40        3       159       12&lt;BR /&gt;
 13    Jeffrey     M      13       53        4       172       13&lt;BR /&gt;
 14    John        M      12       65        5       184       14&lt;BR /&gt;
 15    Philip      M      16       81        6       200       15&lt;BR /&gt;
 16    Robert      M      12       93        7       212       16&lt;BR /&gt;
 17    Ronald      M      15      108        8       227       17&lt;BR /&gt;
 18    Thomas      M      11      119        9       238       18&lt;BR /&gt;
 19    William     M      15      134       10       253       19&lt;BR /&gt;
[/pre]</description>
      <pubDate>Tue, 19 Oct 2010 23:45:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/using-retain-function-to-obtain-cumulative-sum/m-p/16360#M2985</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2010-10-19T23:45:28Z</dc:date>
    </item>
  </channel>
</rss>

