<?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: Cumulative sum in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Cumulative-sum/m-p/820416#M34815</link>
    <description>&lt;P&gt;Like this? If you try to place the cumulative totals into the same named variable you add a great deal of complexity for no real gain.&lt;/P&gt;
&lt;PRE&gt;data have;
input
id var1 var2;
datalines;
1 0 0
1 1 0
1 1 0
1 0 0
2 1 1
2 1 0
2 0 0
2 1 1
2 1 0
;

data want;
  set have;
  by id;
  retain cumvar1 cumvar2;
  if first.id then call missing(cumvar1,cumvar2);
  cumvar1+var1;
  cumvar2+var2;
run;&lt;/PRE&gt;
&lt;P&gt;Retain sets of variables that will keep values across iterations of the data step boundary, such as your cumulative totals.&lt;/P&gt;
&lt;P&gt;Use of By means that SAS will create automatic variables First. and Last. for each variable on the By statement that take values of 1 (or True) and 0 (or False) that indicate whether the current record is one of the boundaries, first or last, or not. So this tests if the current record is the first for an Id group. If so, then the Call Missing function sets the retained values to missing so the total is not carried into the new Id group. The Cumvar1+var1 means "add the current value of Var1 to the cumvar1.&lt;/P&gt;</description>
    <pubDate>Sun, 26 Jun 2022 06:27:27 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2022-06-26T06:27:27Z</dc:date>
    <item>
      <title>Cumulative sum</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Cumulative-sum/m-p/820411#M34814</link>
      <description>&lt;P&gt;I have data in following format:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;data have;&lt;BR /&gt;input&lt;BR /&gt;id var1 var2;&lt;BR /&gt;datalines;&lt;BR /&gt;1 0 0&lt;BR /&gt;1 1 0 &lt;BR /&gt;1 1 0&lt;BR /&gt;1 0 0&lt;BR /&gt;2 1 1 &lt;BR /&gt;2 1 0&lt;BR /&gt;2 0 0&lt;BR /&gt;2 1 1&lt;BR /&gt;2 1 0&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Trying to create cumulative value for different variables (var1 and var2 here) based on IDs. I am expecting something like this:&lt;/P&gt;
&lt;DIV class="branch"&gt;
&lt;DIV&gt;
&lt;DIV align="center"&gt;
&lt;TABLE class="table" summary="Procedure Print: Data Set WORK.WANT" frame="box" rules="all" cellspacing="0" cellpadding="5"&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="r header" scope="col"&gt;Obs&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;id&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;var1&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;var2&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TH class="r rowheader" scope="row"&gt;1&lt;/TH&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;TD class="r data"&gt;0&lt;/TD&gt;
&lt;TD class="r data"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="r rowheader" scope="row"&gt;2&lt;/TH&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;TD class="r data"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="r rowheader" scope="row"&gt;3&lt;/TH&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="r rowheader" scope="row"&gt;4&lt;/TH&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="r rowheader" scope="row"&gt;5&lt;/TH&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="r rowheader" scope="row"&gt;6&lt;/TH&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="r rowheader" scope="row"&gt;7&lt;/TH&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="r rowheader" scope="row"&gt;8&lt;/TH&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;3&lt;/TD&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="r rowheader" scope="row"&gt;9&lt;/TH&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;4&lt;/TD&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 26 Jun 2022 02:21:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Cumulative-sum/m-p/820411#M34814</guid>
      <dc:creator>dac_js</dc:creator>
      <dc:date>2022-06-26T02:21:18Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative sum</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Cumulative-sum/m-p/820416#M34815</link>
      <description>&lt;P&gt;Like this? If you try to place the cumulative totals into the same named variable you add a great deal of complexity for no real gain.&lt;/P&gt;
&lt;PRE&gt;data have;
input
id var1 var2;
datalines;
1 0 0
1 1 0
1 1 0
1 0 0
2 1 1
2 1 0
2 0 0
2 1 1
2 1 0
;

data want;
  set have;
  by id;
  retain cumvar1 cumvar2;
  if first.id then call missing(cumvar1,cumvar2);
  cumvar1+var1;
  cumvar2+var2;
run;&lt;/PRE&gt;
&lt;P&gt;Retain sets of variables that will keep values across iterations of the data step boundary, such as your cumulative totals.&lt;/P&gt;
&lt;P&gt;Use of By means that SAS will create automatic variables First. and Last. for each variable on the By statement that take values of 1 (or True) and 0 (or False) that indicate whether the current record is one of the boundaries, first or last, or not. So this tests if the current record is the first for an Id group. If so, then the Call Missing function sets the retained values to missing so the total is not carried into the new Id group. The Cumvar1+var1 means "add the current value of Var1 to the cumvar1.&lt;/P&gt;</description>
      <pubDate>Sun, 26 Jun 2022 06:27:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Cumulative-sum/m-p/820416#M34815</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-06-26T06:27:27Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative sum</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Cumulative-sum/m-p/820417#M34816</link>
      <description>&lt;P&gt;Use new, retained variables:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have (rename=(var1=_var1 var2=_var2));
by id;
if first.id
then do;
  var1 = _var1;
  var2 = _var2;
end;
else do;
  var1 + _var1;
  var2 + _var2;
end;
drop _:;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Use of a &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/n1dfiqj146yi2cn1maeju9wo7ijs.htm" target="_blank" rel="noopener"&gt;Sum Statement&lt;/A&gt;&amp;nbsp;causes an implicit RETAIN..&lt;/P&gt;</description>
      <pubDate>Sun, 26 Jun 2022 06:31:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Cumulative-sum/m-p/820417#M34816</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-06-26T06:31:16Z</dc:date>
    </item>
  </channel>
</rss>

