<?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 without PROC SQL in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-sum-without-PROC-SQL/m-p/620654#M182405</link>
    <description>&lt;P&gt;I'm not that familiar with NLMIXED, but I would suggest you simply create variable Y in a DATA step, and then use this newly create data set in NLMIXED.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary nway data=have;
    class id1;
	var x;
	output out=sums(drop=_:) sum=sum_x;
Run;
data want;
    merge have sums;
	by id1;
	prev_x=lag(x);
	if first.id1 then y=sum_x;
	else y+ (-prev_x);
	drop sum_x prev_x;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;By the way, SQL is a particularly poor choice for cumulative sums.&lt;/P&gt;</description>
    <pubDate>Tue, 28 Jan 2020 20:45:31 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2020-01-28T20:45:31Z</dc:date>
    <item>
      <title>Cumulative sum without PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-sum-without-PROC-SQL/m-p/620652#M182403</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am using PROC NLMIX and to calculate likelihoods, I need to take the sum of values in a column, for each unique ID. here is an example of my data:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id1 $  id2  $  x;
datalines;
A     z1   1    
A     z2   2
A     z3   3
B     y1   1
B     y2   3
C     w1   3
C     w2   2
C     w3   1
;
end;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I want to create the new column y that is the cumulative sum of values in X starting within each group identified by variable id1, in which the first value in the cumulative sum is the value of x in the same row. For example, in the first row, since the group is id1, and we have 3 observations with id1=A then we should have y= 1 + 2 + 3. This is the cumulative sum of x values in which the id1=A, staring from x1=1 which is the value of x in the first row.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now, in the second row, since Y=2+3 since I want the cumulative sum to start from the value of x in the same row, and finally, in the third row, Y=3.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The final data-set should look like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ID1 ID2  X   Y
A     z1   1   6
A     z2   2   5
A     z3   3   3
B     y1   1   4
B     y2   3   3
C     w1   3  6
C     w2   2  3
C     w3   1  1&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I need to achive this without proc sql because I am implementing it inside proc nlmixed. Maybe arrays is the way to go?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Jan 2020 20:36:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-sum-without-PROC-SQL/m-p/620652#M182403</guid>
      <dc:creator>niam</dc:creator>
      <dc:date>2020-01-28T20:36:47Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative sum without PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-sum-without-PROC-SQL/m-p/620654#M182405</link>
      <description>&lt;P&gt;I'm not that familiar with NLMIXED, but I would suggest you simply create variable Y in a DATA step, and then use this newly create data set in NLMIXED.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary nway data=have;
    class id1;
	var x;
	output out=sums(drop=_:) sum=sum_x;
Run;
data want;
    merge have sums;
	by id1;
	prev_x=lag(x);
	if first.id1 then y=sum_x;
	else y+ (-prev_x);
	drop sum_x prev_x;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;By the way, SQL is a particularly poor choice for cumulative sums.&lt;/P&gt;</description>
      <pubDate>Tue, 28 Jan 2020 20:45:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-sum-without-PROC-SQL/m-p/620654#M182405</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-01-28T20:45:31Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative sum without PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-sum-without-PROC-SQL/m-p/620655#M182406</link>
      <description>&lt;P&gt;I am unaware of this being possible entirely within NLMIXED.&amp;nbsp; But you can fairly easily precede your PROC NLMIXED with a data step producing the desired CUMSUM variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id1 $  id2  $  x;
datalines;
A     z1   1    
A     z2   2
A     z3   3
B     y1   1
B     y2   3
C     w1   3
C     w2   2
C     w3   1
;

data need;
  set have (in=firstpass)  have (in=secondpass);
  by id1;
  if first.id1 then cumsum=0;
  if firstpass=1 then cumsum+x;
  if secondpass;
  output;
  cumsum+ (-x);
run;

proc nlmixed data=need .... &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The "trick" here is the SET HAVE …; BY ID1: pair of statements.&amp;nbsp; It tells SAS to read all records from each ID1 once (the first pass), and then read those same records for the same ID1 once more (the second pass). &amp;nbsp; Use the first pass to build the CUMSUM for all of ID1, and then use the second pass to progressively decrement the CUMSUM.&lt;/P&gt;</description>
      <pubDate>Tue, 28 Jan 2020 20:53:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-sum-without-PROC-SQL/m-p/620655#M182406</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-01-28T20:53:46Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative sum without PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-sum-without-PROC-SQL/m-p/620657#M182407</link>
      <description>&lt;P&gt;A double DO Until() loop will do the job:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id1 $  id2  $  x;
datalines;
A     z1   1    
A     z2   2
A     z3   3
B     y1   1
B     y2   3
C     w1   3
C     w2   2
C     w3   1
;

data want;
do until(last.id1);
	set have; by id1;
	y = sum(y, x);
	end;
do until(last.id1);
	set have; by id1;
	output;
	y = y - x;
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 28 Jan 2020 20:56:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-sum-without-PROC-SQL/m-p/620657#M182407</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2020-01-28T20:56:08Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative sum without PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-sum-without-PROC-SQL/m-p/620665#M182414</link>
      <description>&lt;P&gt;data have;&lt;BR /&gt;input id1 $ id2 $ x;&lt;BR /&gt;datalines;&lt;BR /&gt;A z1 1&lt;BR /&gt;A z2 2&lt;BR /&gt;A z3 3&lt;BR /&gt;B y1 1&lt;BR /&gt;B y2 3&lt;BR /&gt;C w1 3&lt;BR /&gt;C w2 2&lt;BR /&gt;C w3 1&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;proc sort data=have;&lt;BR /&gt;by id1;&lt;BR /&gt;run;&lt;BR /&gt;data have2;&lt;BR /&gt;retain row;&lt;BR /&gt;set have;&lt;BR /&gt;row=_n_;&lt;BR /&gt;run;&lt;BR /&gt;proc sort data=have2;&lt;BR /&gt;by id1 descending row;&lt;BR /&gt;run;&lt;BR /&gt;data have3;&lt;BR /&gt;Retain id1 id2 x y;&lt;BR /&gt;set have2;&lt;BR /&gt;by id1;&lt;B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;TD&gt;w2&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;C&lt;/TD&gt;&lt;TD&gt;w3&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Tue, 28 Jan 2020 21:16:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-sum-without-PROC-SQL/m-p/620665#M182414</guid>
      <dc:creator>Sajid01</dc:creator>
      <dc:date>2020-01-28T21:16:34Z</dc:date>
    </item>
  </channel>
</rss>

