<?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 cumulative sum with by grouping condition in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/cumulative-sum-with-by-grouping-condition/m-p/638011#M189718</link>
    <description>&lt;P&gt;Hi y'all i hope you are all safe and coping the lockdown,&lt;/P&gt;&lt;P&gt;I know this topic had been discussed way times than it should be, but i really tried and fetched over the solutions for a few times before coming to you.&lt;/P&gt;&lt;P&gt;I'm working on a simple covid-19 data set containing these variables :&amp;nbsp;&lt;/P&gt;&lt;P&gt;Date&amp;nbsp; - Cases - Deaths - Country - Population - TotalCases&lt;/P&gt;&lt;P&gt;My problem is that i want to sum the number of cases by Country but i obtain very weird results.&lt;/P&gt;&lt;P&gt;First I sorted my data by Country and Date (by default order (ascending))&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=covid.data2;
by Country Date;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Then I told sas to consider to start from the first case in cases and assign it to totalcases the sum until the last one is reached for each country :&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data covid.data2;
set covid.data2;
by Country;
if First.cases then TotalCases = first.cases;
TotalCases + cases;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;However the results are absolutely not the sum of each case daily by country ! I can't seem to find the error, can you help ?&lt;/P&gt;&lt;P&gt;here is a screen of the output (an example of afghanistan ) :&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 453px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/37969i77EAE8E552912772/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 07 Apr 2020 10:47:11 GMT</pubDate>
    <dc:creator>skavli</dc:creator>
    <dc:date>2020-04-07T10:47:11Z</dc:date>
    <item>
      <title>cumulative sum with by grouping condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/cumulative-sum-with-by-grouping-condition/m-p/638011#M189718</link>
      <description>&lt;P&gt;Hi y'all i hope you are all safe and coping the lockdown,&lt;/P&gt;&lt;P&gt;I know this topic had been discussed way times than it should be, but i really tried and fetched over the solutions for a few times before coming to you.&lt;/P&gt;&lt;P&gt;I'm working on a simple covid-19 data set containing these variables :&amp;nbsp;&lt;/P&gt;&lt;P&gt;Date&amp;nbsp; - Cases - Deaths - Country - Population - TotalCases&lt;/P&gt;&lt;P&gt;My problem is that i want to sum the number of cases by Country but i obtain very weird results.&lt;/P&gt;&lt;P&gt;First I sorted my data by Country and Date (by default order (ascending))&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=covid.data2;
by Country Date;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Then I told sas to consider to start from the first case in cases and assign it to totalcases the sum until the last one is reached for each country :&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data covid.data2;
set covid.data2;
by Country;
if First.cases then TotalCases = first.cases;
TotalCases + cases;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;However the results are absolutely not the sum of each case daily by country ! I can't seem to find the error, can you help ?&lt;/P&gt;&lt;P&gt;here is a screen of the output (an example of afghanistan ) :&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 453px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/37969i77EAE8E552912772/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Apr 2020 10:47:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/cumulative-sum-with-by-grouping-condition/m-p/638011#M189718</guid>
      <dc:creator>skavli</dc:creator>
      <dc:date>2020-04-07T10:47:11Z</dc:date>
    </item>
    <item>
      <title>Re: cumulative sum with by grouping condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/cumulative-sum-with-by-grouping-condition/m-p/638015#M189720</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data covid.data2;
set covid.data2;
by Country;
if first.country then TotalCases = 0;
TotalCases + cases; 
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You want first.country, not first.case. The first time a country appears in the data set, the cumulative count is set to zero.&lt;/P&gt;</description>
      <pubDate>Tue, 07 Apr 2020 10:53:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/cumulative-sum-with-by-grouping-condition/m-p/638015#M189720</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-04-07T10:53:52Z</dc:date>
    </item>
    <item>
      <title>Re: cumulative sum with by grouping condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/cumulative-sum-with-by-grouping-condition/m-p/638018#M189722</link>
      <description>&lt;P&gt;first.cases does not exist; first.country is an automatic boolean variable that can only have the values 0 (false) or 1 (true).&lt;/P&gt;
&lt;P&gt;You want&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data covid.data3;
/* do not use the input dataset name here, unless you like destroyed datasets
that need to be re-created if something happens in the data step */
set covid.data2;
by country;
if first.country
then TotalCases = cases;
else TotalCases + cases;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 07 Apr 2020 11:10:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/cumulative-sum-with-by-grouping-condition/m-p/638018#M189722</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-04-07T11:10:12Z</dc:date>
    </item>
  </channel>
</rss>

