<?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 column based on critera in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-column-based-on-critera/m-p/803475#M316367</link>
    <description>&lt;P&gt;It works now that I reran the code? I'm not sure why it didn't work before.&lt;/P&gt;&lt;PRE&gt;&lt;BR /&gt;&lt;CODE class=""&gt;data have;
  input ID weeks;
 datalines;
1   0.0
1   0.0
1   1.0
1   1.5
2   0.0
2   2.5
2   3.5
3   0.0
3   0.5
3   4.5
3   4.58

data new;
    set have;
    by ID;
    if first.id then do;
        cumul_wks=0;
    end;
    cumul_wks + weeks;
run;

proc print data=new;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Log&lt;/P&gt;&lt;P&gt;1 data have;&lt;BR /&gt;2 input ID weeks;&lt;BR /&gt;3 datalines;&lt;/P&gt;&lt;P&gt;NOTE: The data set WORK.HAVE has 11 observations and 2 variables.&lt;BR /&gt;NOTE: DATA statement used (Total process time):&lt;BR /&gt;real time 33.26 seconds&lt;BR /&gt;cpu time 0.93 seconds&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;15 data have;&lt;BR /&gt;16 input ID weeks;&lt;BR /&gt;17 datalines;&lt;/P&gt;&lt;P&gt;NOTE: SAS went to a new line when INPUT statement reached past the end of a line.&lt;BR /&gt;NOTE: The data set WORK.HAVE has 11 observations and 2 variables.&lt;BR /&gt;NOTE: DATA statement used (Total process time):&lt;BR /&gt;real time 0.00 seconds&lt;BR /&gt;cpu time 0.01 seconds&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;30 data new;&lt;BR /&gt;31 set have;&lt;BR /&gt;32 by ID;&lt;BR /&gt;33 if first.id then do;&lt;BR /&gt;34 cumul_wks=0;&lt;BR /&gt;35 end;&lt;BR /&gt;36 cumul_wks + weeks;&lt;BR /&gt;37 run;&lt;/P&gt;&lt;P&gt;NOTE: There were 11 observations read from the data set WORK.HAVE.&lt;BR /&gt;NOTE: The data set WORK.NEW has 11 observations and 3 variables.&lt;BR /&gt;NOTE: DATA statement used (Total process time):&lt;BR /&gt;real time 0.01 seconds&lt;BR /&gt;cpu time 0.01 seconds&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;38 proc print data=new;&lt;BR /&gt;NOTE: Writing HTML Body file: sashtml.htm&lt;BR /&gt;39 run;&lt;/P&gt;&lt;P&gt;NOTE: There were 11 observations read from the data set WORK.NEW.&lt;BR /&gt;NOTE: PROCEDURE PRINT used (Total process time):&lt;BR /&gt;real time 0.48 seconds&lt;BR /&gt;cpu time 0.21 seconds&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Output&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot 2022-03-22 191757.jpg" style="width: 336px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/69710iB4630947686A8BC7/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screenshot 2022-03-22 191757.jpg" alt="Screenshot 2022-03-22 191757.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Tue, 22 Mar 2022 23:19:35 GMT</pubDate>
    <dc:creator>nharuka</dc:creator>
    <dc:date>2022-03-22T23:19:35Z</dc:date>
    <item>
      <title>Cumulative column based on critera</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-column-based-on-critera/m-p/803198#M316265</link>
      <description>&lt;P&gt;I have a longitudinal dataset with several observations per person. I'm trying to make a cumulative weeks count for each person. How can I do this? I'm a SAS beginner so any help would be appreciated!&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;&lt;BR /&gt;data have;
  input ID weeks;
 datalines;
1   0.0
1   0.0
1   1.0
1   1.5
2   0.0
2   2.5
2   3.5
3   0.0
3   0.5
3   4.5
3   4.58&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I want the dataset to look like this&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; Weeks&amp;nbsp; Cuml&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; 0.0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0.0&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; 0.0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0.0&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; 1.0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1.0&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; 1.5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2.5&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; 0.0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0.0&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; 2.5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2.5&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; 3.5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 6.0&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; 0.0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0.0&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; 0.5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0.5&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; 4.5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 5.0&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; 4.58&amp;nbsp; &amp;nbsp; &amp;nbsp; 9.58&lt;/P&gt;</description>
      <pubDate>Mon, 21 Mar 2022 18:41:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-column-based-on-critera/m-p/803198#M316265</guid>
      <dc:creator>nharuka</dc:creator>
      <dc:date>2022-03-21T18:41:15Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative column based on critera</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-column-based-on-critera/m-p/803201#M316267</link>
      <description>&lt;P&gt;A recent example:&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Procedures/GROUP-BY/m-p/803182#M81670" target="_blank"&gt;https://communities.sas.com/t5/SAS-Procedures/GROUP-BY/m-p/803182#M81670&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 21 Mar 2022 19:14:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-column-based-on-critera/m-p/803201#M316267</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-03-21T19:14:32Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative column based on critera</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-column-based-on-critera/m-p/803207#M316268</link>
      <description>&lt;P&gt;Hi! I tried using that example, but the problem is that the count doesn't reset for each new ID number, which I need it to do. How can I modify the code for the to reset for each new ID number?&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data new;
    set have;
    by ID;
    if first.id then do;
        cumul_wks=0;
    end;
    cumul_wks + weeks;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 21 Mar 2022 19:39:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-column-based-on-critera/m-p/803207#M316268</guid>
      <dc:creator>nharuka</dc:creator>
      <dc:date>2022-03-21T19:39:56Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative column based on critera</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-column-based-on-critera/m-p/803209#M316269</link>
      <description>&lt;P&gt;Are there errors in the log?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If so, we need to see the log (all of it for this DATA step, every single character, every single line, nothing chopped out) copied as text and pasted into your reply in the window that appears after you click on the &amp;lt;/&amp;gt; icon.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Are there errors in the output?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If so, show us a screen capture. Make the screen capture and click on the "Insert Photos" icon.&lt;/P&gt;</description>
      <pubDate>Mon, 21 Mar 2022 19:44:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-column-based-on-critera/m-p/803209#M316269</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-03-21T19:44:07Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative column based on critera</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-column-based-on-critera/m-p/803216#M316271</link>
      <description>&lt;P&gt;Log?&lt;/P&gt;
&lt;P&gt;Is your data sorted by the ID variable? You might be seeing a previous version of the output data set that wasn't replaced because of a sort error.&lt;/P&gt;
&lt;P&gt;Or if you have a variable in the Have data set already named Cumul_wks. Retain will reset when the new record is brought into the data vector if that variable is in the source data.&lt;/P&gt;</description>
      <pubDate>Mon, 21 Mar 2022 19:52:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-column-based-on-critera/m-p/803216#M316271</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-03-21T19:52:25Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative column based on critera</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-column-based-on-critera/m-p/803217#M316272</link>
      <description>&lt;P&gt;I didn't have any errors in my log. I got it to work by using retain and modifying the code a bit. Thanks for the help!&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data new;
	set have;
	by ID;
	retain cumul_wks;
	if first.id then cumul_wks = weeks;
	else cumul_wks = cumul_wks + weeks;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 21 Mar 2022 19:57:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-column-based-on-critera/m-p/803217#M316272</guid>
      <dc:creator>nharuka</dc:creator>
      <dc:date>2022-03-21T19:57:14Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative column based on critera</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-column-based-on-critera/m-p/803246#M316286</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/420250"&gt;@nharuka&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I didn't have any errors in my log. I got it to work by using retain and modifying the code a bit. Thanks for the help!&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;data new;
	set have;
	by ID;
	retain cumul_wks;
	if first.id then cumul_wks = weeks;
	else cumul_wks = cumul_wks + weeks;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This code appears correct.&amp;nbsp; BUT, if for some reason&amp;nbsp;&lt;EM&gt;&lt;STRONG&gt;cumul_wks&lt;/STRONG&gt;&lt;/EM&gt; already exists in dataset HAVE (as suggested by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;), you will not get actual cumulative weeks.&amp;nbsp; This is because the RETAINed value will be overwritten by the value retrieved in the SET statement.&lt;/P&gt;</description>
      <pubDate>Mon, 21 Mar 2022 23:34:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-column-based-on-critera/m-p/803246#M316286</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-03-21T23:34:00Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative column based on critera</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-column-based-on-critera/m-p/803284#M316299</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/420250"&gt;@nharuka&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I didn't have any errors in my log. I got it to work by using retain and modifying the code a bit. Thanks for the help!&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;data new;
	set have;
	by ID;
	retain cumul_wks;
	if first.id then cumul_wks = weeks;
	else cumul_wks = cumul_wks + weeks;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Won't work properly with missing values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You should answer the question about exactly what was wrong in the code I provided (where I specifically asked you to SHOW US the results).&lt;/P&gt;</description>
      <pubDate>Tue, 22 Mar 2022 10:27:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-column-based-on-critera/m-p/803284#M316299</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-03-22T10:27:44Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative column based on critera</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-column-based-on-critera/m-p/803468#M316364</link>
      <description>I don't have any missing values in my dataset. When I used your example code, it kept on aggregating and not resetting for each new ID. It looked like this&lt;BR /&gt;&lt;BR /&gt;ID Weeks Cumul_wks&lt;BR /&gt;&lt;BR /&gt;1 0.0 0.0&lt;BR /&gt;&lt;BR /&gt;1 0.0 0.0&lt;BR /&gt;&lt;BR /&gt;1 1.0 1.0&lt;BR /&gt;&lt;BR /&gt;1 1.5 2.5&lt;BR /&gt;&lt;BR /&gt;2 0.0 2.5&lt;BR /&gt;&lt;BR /&gt;2 2.5 4.5&lt;BR /&gt;&lt;BR /&gt;2 3.5 12.8&lt;BR /&gt;&lt;BR /&gt;3 0.0 12.8&lt;BR /&gt;&lt;BR /&gt;3 0.5 13.3&lt;BR /&gt;&lt;BR /&gt;3 4.5 17.8&lt;BR /&gt;&lt;BR /&gt;3 4.58 22.4</description>
      <pubDate>Tue, 22 Mar 2022 22:51:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-column-based-on-critera/m-p/803468#M316364</guid>
      <dc:creator>nharuka</dc:creator>
      <dc:date>2022-03-22T22:51:55Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative column based on critera</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-column-based-on-critera/m-p/803470#M316365</link>
      <description>&lt;P&gt;&lt;STRONG&gt;Please&lt;/STRONG&gt; show the Log of the actual code that you ran. The data step and any messages. Copy the text from the log, on the forum open a text box with the &amp;lt;/&amp;gt; icon that appears above the message window and then paste the text.&lt;/P&gt;</description>
      <pubDate>Tue, 22 Mar 2022 23:03:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-column-based-on-critera/m-p/803470#M316365</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-03-22T23:03:19Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative column based on critera</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-column-based-on-critera/m-p/803475#M316367</link>
      <description>&lt;P&gt;It works now that I reran the code? I'm not sure why it didn't work before.&lt;/P&gt;&lt;PRE&gt;&lt;BR /&gt;&lt;CODE class=""&gt;data have;
  input ID weeks;
 datalines;
1   0.0
1   0.0
1   1.0
1   1.5
2   0.0
2   2.5
2   3.5
3   0.0
3   0.5
3   4.5
3   4.58

data new;
    set have;
    by ID;
    if first.id then do;
        cumul_wks=0;
    end;
    cumul_wks + weeks;
run;

proc print data=new;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Log&lt;/P&gt;&lt;P&gt;1 data have;&lt;BR /&gt;2 input ID weeks;&lt;BR /&gt;3 datalines;&lt;/P&gt;&lt;P&gt;NOTE: The data set WORK.HAVE has 11 observations and 2 variables.&lt;BR /&gt;NOTE: DATA statement used (Total process time):&lt;BR /&gt;real time 33.26 seconds&lt;BR /&gt;cpu time 0.93 seconds&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;15 data have;&lt;BR /&gt;16 input ID weeks;&lt;BR /&gt;17 datalines;&lt;/P&gt;&lt;P&gt;NOTE: SAS went to a new line when INPUT statement reached past the end of a line.&lt;BR /&gt;NOTE: The data set WORK.HAVE has 11 observations and 2 variables.&lt;BR /&gt;NOTE: DATA statement used (Total process time):&lt;BR /&gt;real time 0.00 seconds&lt;BR /&gt;cpu time 0.01 seconds&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;30 data new;&lt;BR /&gt;31 set have;&lt;BR /&gt;32 by ID;&lt;BR /&gt;33 if first.id then do;&lt;BR /&gt;34 cumul_wks=0;&lt;BR /&gt;35 end;&lt;BR /&gt;36 cumul_wks + weeks;&lt;BR /&gt;37 run;&lt;/P&gt;&lt;P&gt;NOTE: There were 11 observations read from the data set WORK.HAVE.&lt;BR /&gt;NOTE: The data set WORK.NEW has 11 observations and 3 variables.&lt;BR /&gt;NOTE: DATA statement used (Total process time):&lt;BR /&gt;real time 0.01 seconds&lt;BR /&gt;cpu time 0.01 seconds&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;38 proc print data=new;&lt;BR /&gt;NOTE: Writing HTML Body file: sashtml.htm&lt;BR /&gt;39 run;&lt;/P&gt;&lt;P&gt;NOTE: There were 11 observations read from the data set WORK.NEW.&lt;BR /&gt;NOTE: PROCEDURE PRINT used (Total process time):&lt;BR /&gt;real time 0.48 seconds&lt;BR /&gt;cpu time 0.21 seconds&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Output&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot 2022-03-22 191757.jpg" style="width: 336px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/69710iB4630947686A8BC7/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screenshot 2022-03-22 191757.jpg" alt="Screenshot 2022-03-22 191757.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 22 Mar 2022 23:19:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-column-based-on-critera/m-p/803475#M316367</guid>
      <dc:creator>nharuka</dc:creator>
      <dc:date>2022-03-22T23:19:35Z</dc:date>
    </item>
  </channel>
</rss>

