<?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 summing a variable by condition and id in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/summing-a-variable-by-condition-and-id/m-p/741207#M231668</link>
    <description>&lt;P&gt;Hi ,&lt;BR /&gt;I am trying to sum one variable as long as another remains constant. I want to cumulative sum dur as long as a is constant. when a changes the sum restarts. when a new id, the sum restarts.&lt;BR /&gt;id a dur&lt;BR /&gt;1 1 30&lt;BR /&gt;1 1 30&lt;BR /&gt;1 2 10&lt;BR /&gt;2 1 20&lt;BR /&gt;2 2 10&lt;BR /&gt;2 2 10&lt;BR /&gt;3 2 10&lt;BR /&gt;3 1 10&lt;BR /&gt;&lt;BR /&gt;and I would like to do this:&lt;BR /&gt;id a dur sum&lt;BR /&gt;1 1 30 30&lt;BR /&gt;1 1 30 60&lt;BR /&gt;1 2 10 10&lt;BR /&gt;2 1 20 20&lt;BR /&gt;2 2 10 10&lt;BR /&gt;2 2 10 20&lt;BR /&gt;3 2 10 10&lt;BR /&gt;3 1 10 10&lt;BR /&gt;&lt;BR /&gt;Thanks&lt;/P&gt;</description>
    <pubDate>Thu, 13 May 2021 18:27:26 GMT</pubDate>
    <dc:creator>nataliavanessa0</dc:creator>
    <dc:date>2021-05-13T18:27:26Z</dc:date>
    <item>
      <title>summing a variable by condition and id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/summing-a-variable-by-condition-and-id/m-p/741207#M231668</link>
      <description>&lt;P&gt;Hi ,&lt;BR /&gt;I am trying to sum one variable as long as another remains constant. I want to cumulative sum dur as long as a is constant. when a changes the sum restarts. when a new id, the sum restarts.&lt;BR /&gt;id a dur&lt;BR /&gt;1 1 30&lt;BR /&gt;1 1 30&lt;BR /&gt;1 2 10&lt;BR /&gt;2 1 20&lt;BR /&gt;2 2 10&lt;BR /&gt;2 2 10&lt;BR /&gt;3 2 10&lt;BR /&gt;3 1 10&lt;BR /&gt;&lt;BR /&gt;and I would like to do this:&lt;BR /&gt;id a dur sum&lt;BR /&gt;1 1 30 30&lt;BR /&gt;1 1 30 60&lt;BR /&gt;1 2 10 10&lt;BR /&gt;2 1 20 20&lt;BR /&gt;2 2 10 10&lt;BR /&gt;2 2 10 20&lt;BR /&gt;3 2 10 10&lt;BR /&gt;3 1 10 10&lt;BR /&gt;&lt;BR /&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Thu, 13 May 2021 18:27:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/summing-a-variable-by-condition-and-id/m-p/741207#M231668</guid>
      <dc:creator>nataliavanessa0</dc:creator>
      <dc:date>2021-05-13T18:27:26Z</dc:date>
    </item>
    <item>
      <title>Re: summing a variable by condition and id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/summing-a-variable-by-condition-and-id/m-p/741215#M231670</link>
      <description>&lt;P&gt;You would need to sort your data and then use a data step.&lt;/P&gt;
&lt;P&gt;Here is an example.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data indata;&lt;BR /&gt;infile cards;&lt;BR /&gt;input id a dur;&lt;BR /&gt;cards;&lt;BR /&gt;1 1 30&lt;BR /&gt;1 1 30&lt;BR /&gt;1 2 10&lt;BR /&gt;2 1 20&lt;BR /&gt;2 2 10&lt;BR /&gt;2 2 10&lt;BR /&gt;3 2 10&lt;BR /&gt;3 1 10&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Proc sort data=indata ;&lt;BR /&gt;by id a;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Data outdata;&lt;BR /&gt;set indata;&lt;BR /&gt;by id a;&lt;BR /&gt;if first.a=1 then cumdur=0;&lt;BR /&gt;cumdur+dur;&lt;BR /&gt;if last.a;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Thu, 13 May 2021 18:34:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/summing-a-variable-by-condition-and-id/m-p/741215#M231670</guid>
      <dc:creator>CarmineVerrell</dc:creator>
      <dc:date>2021-05-13T18:34:59Z</dc:date>
    </item>
    <item>
      <title>Re: summing a variable by condition and id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/summing-a-variable-by-condition-and-id/m-p/741229#M231677</link>
      <description>thank you &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/23738"&gt;@CarmineVerrell&lt;/a&gt;&lt;BR /&gt;&lt;BR /&gt;But what if I don't want to combine all 1s and all 2s by ID, I just one to sum dur while a is constant but when it changes I want to restart the sum until it changes again?&lt;BR /&gt;something like this:&lt;BR /&gt;&lt;BR /&gt;and I would like to do this:&lt;BR /&gt;id a dur sum&lt;BR /&gt;1 1 30 30&lt;BR /&gt;1 1 30 60&lt;BR /&gt;1 2 10 10&lt;BR /&gt;1 1 10 10&lt;BR /&gt;&lt;BR /&gt;Thanks.</description>
      <pubDate>Thu, 13 May 2021 18:47:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/summing-a-variable-by-condition-and-id/m-p/741229#M231677</guid>
      <dc:creator>nataliavanessa0</dc:creator>
      <dc:date>2021-05-13T18:47:24Z</dc:date>
    </item>
    <item>
      <title>Re: summing a variable by condition and id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/summing-a-variable-by-condition-and-id/m-p/741239#M231681</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/123632"&gt;@nataliavanessa0&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;thank you &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/23738"&gt;@CarmineVerrell&lt;/a&gt;&lt;BR /&gt;&lt;BR /&gt;But what if I don't want to combine all 1s and all 2s by ID, I just one to sum dur while a is constant but when it changes I want to restart the sum until it changes again?&lt;BR /&gt;something like this:&lt;BR /&gt;&lt;BR /&gt;and I would like to do this:&lt;BR /&gt;id a dur sum&lt;BR /&gt;1 1 30 30&lt;BR /&gt;1 1 30 60&lt;BR /&gt;1 2 10 10&lt;BR /&gt;1 1 10 10&lt;BR /&gt;&lt;BR /&gt;Thanks.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That would be the NOTSORTED option on the BY statement.&lt;/P&gt;
&lt;PRE&gt;Data outdata;
   set indata;
   by notsorted id a;
   if first.a=1 then cumdur=0;
   cumdur+dur;
run;&lt;/PRE&gt;
&lt;P&gt;Notsorted aplies to all the the variables on the by statement regardless of the position it appears in the By statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 13 May 2021 18:54:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/summing-a-variable-by-condition-and-id/m-p/741239#M231681</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-05-13T18:54:31Z</dc:date>
    </item>
  </channel>
</rss>

