<?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 Calculate cumulative sum by column in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculate-cumulative-sum-by-column/m-p/889702#M351536</link>
    <description>&lt;P&gt;Hi, there is the following dataset where each ageX column indicates if there was an incident or not.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data dsin;
input ID age1 age2 age3 age4 age5 age6;
datalines;
1 1 0 0 1 0 0
2 0 1 1 0 0 0
3 0 0 0 1 0 0
4 0 0 0 0 0 0
5 1 0 0 0 1 0
6 0 0 1 0 0 0
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;We would like to have the cumulative number of people who had an incident by age. The tricky part is that once a person has an incident and it is accounted for, if this person has incidents after that, it still counts only as 1. Let's say for ID1:it presents an incident at age1, and age4 as well. But this person can only be accounted once. The below table is the output we are looking for:&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Age&lt;/TD&gt;&lt;TD&gt;age1&lt;/TD&gt;&lt;TD&gt;age2&lt;/TD&gt;&lt;TD&gt;age3&lt;/TD&gt;&lt;TD&gt;age4&lt;/TD&gt;&lt;TD&gt;age5&lt;/TD&gt;&lt;TD&gt;age6&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Cumulative&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
    <pubDate>Thu, 17 Aug 2023 17:02:00 GMT</pubDate>
    <dc:creator>ANKH1</dc:creator>
    <dc:date>2023-08-17T17:02:00Z</dc:date>
    <item>
      <title>Calculate cumulative sum by column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-cumulative-sum-by-column/m-p/889702#M351536</link>
      <description>&lt;P&gt;Hi, there is the following dataset where each ageX column indicates if there was an incident or not.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data dsin;
input ID age1 age2 age3 age4 age5 age6;
datalines;
1 1 0 0 1 0 0
2 0 1 1 0 0 0
3 0 0 0 1 0 0
4 0 0 0 0 0 0
5 1 0 0 0 1 0
6 0 0 1 0 0 0
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;We would like to have the cumulative number of people who had an incident by age. The tricky part is that once a person has an incident and it is accounted for, if this person has incidents after that, it still counts only as 1. Let's say for ID1:it presents an incident at age1, and age4 as well. But this person can only be accounted once. The below table is the output we are looking for:&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Age&lt;/TD&gt;&lt;TD&gt;age1&lt;/TD&gt;&lt;TD&gt;age2&lt;/TD&gt;&lt;TD&gt;age3&lt;/TD&gt;&lt;TD&gt;age4&lt;/TD&gt;&lt;TD&gt;age5&lt;/TD&gt;&lt;TD&gt;age6&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Cumulative&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Thu, 17 Aug 2023 17:02:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-cumulative-sum-by-column/m-p/889702#M351536</guid>
      <dc:creator>ANKH1</dc:creator>
      <dc:date>2023-08-17T17:02:00Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate cumulative sum by column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-cumulative-sum-by-column/m-p/889709#M351542</link>
      <description>&lt;P&gt;Should be a faster way than this, but I'm sleep deprived today.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dsin;
input ID age1 age2 age3 age4 age5 age6;
datalines;
1 1 0 0 1 0 0
2 0 1 1 0 0 0
3 0 0 0 1 0 0
4 0 0 0 0 0 0
5 1 0 0 0 1 0
6 0 0 1 0 0 0
;
run;

data _dsin / view=_dsin;
set dsin;
array age age1-age6;
flag=0;
do i=1 to dim(age);
if age(i) = 1 and flag=0 then flag=1;
else if age(i) = 1 and flag=1 then age(i) = 0;
end;
keep id age:;
run;

proc means data=_dsin noprint;
output out=agg1 sum=;
run;

data want;
set agg1;
array age age1-age6;

do i=2 to dim(age);
age(i) = age(i) + age(i-1);
end;
keep age:;
run;
&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 17 Aug 2023 17:37:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-cumulative-sum-by-column/m-p/889709#M351542</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-08-17T17:37:18Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate cumulative sum by column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-cumulative-sum-by-column/m-p/889764#M351567</link>
      <description>&lt;P&gt;Hi! Thank you so much for your response. It worked perfectly. But I encountered that some of the datasets will have more than 1 incident for the same ID in the same age variable. Is it possible to account for this in the code? That is, the ID will only be accounted once, even though it reported 2 or more incidents for the same ID.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Aug 2023 00:45:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-cumulative-sum-by-column/m-p/889764#M351567</guid>
      <dc:creator>ANKH1</dc:creator>
      <dc:date>2023-08-18T00:45:26Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate cumulative sum by column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-cumulative-sum-by-column/m-p/889831#M351590</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dsin;
input ID age1 age2 age3 age4 age5 age6;
datalines;
1 1 0 0 1 0 0
2 0 1 1 0 0 0
3 0 0 0 1 0 0
4 0 0 0 0 0 0
5 1 0 0 0 1 0
6 0 0 1 0 0 0
;
run;

proc sql;
create table want as
select 'Cumulative' as Age ,
 sum(age1) as age1,
 sum(max(age1,age2)) as age2,
 sum(max(age1,age2,age3)) as age3,
 sum(max(age1,age2,age3,age4)) as age4,
 sum(max(age1,age2,age3,age4,age5)) as age5,
 sum(max(age1,age2,age3,age4,age5,age6)) as age6
 from dsin ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 18 Aug 2023 11:37:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-cumulative-sum-by-column/m-p/889831#M351590</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-08-18T11:37:53Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate cumulative sum by column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-cumulative-sum-by-column/m-p/889859#M351595</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dsin;
input ID age1 age2 age3 age4 age5 age6;
datalines;
1 1 0 0 1 0 0
2 0 1 1 0 0 0
3 0 0 0 1 0 0
4 0 0 0 0 0 0
5 1 0 0 0 1 0
6 0 0 1 0 0 0
run;

data want (keep=cumcount:);
  set dsin end=end_of_data;
  array age {6};
  array cumcount {6} (6*0);

  if whichn(1,of age{*})&amp;gt;0 then do _n_=whichn(1,of age{*}) to dim(age);
    cumcount{_n_}+1;
  end;

  if end_of_data;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 18 Aug 2023 13:13:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-cumulative-sum-by-column/m-p/889859#M351595</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-08-18T13:13:21Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate cumulative sum by column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-cumulative-sum-by-column/m-p/889866#M351598</link>
      <description>&lt;P&gt;It probably would be simpler not to have that wide structure to start with.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input ID @;
  do age=1 to 6;
     input incident  @;
     Cumulative=max(incident ,Cumulative);
     output;
  end;
datalines;
1 1 0 0 1 0 0
2 0 1 1 0 0 0
3 0 0 0 1 0 0
4 0 0 0 0 0 0
5 1 0 0 0 1 0
6 0 0 1 0 0 0
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then getting the sum is easy.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc means nway sum;
  class age;
  var cumulative;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 18 Aug 2023 13:26:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-cumulative-sum-by-column/m-p/889866#M351598</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-08-18T13:26:39Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate cumulative sum by column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-cumulative-sum-by-column/m-p/889908#M351602</link>
      <description>That would imply a different structure than above as you have only 1 variable per age, or that it's not just 0/1?</description>
      <pubDate>Fri, 18 Aug 2023 14:56:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-cumulative-sum-by-column/m-p/889908#M351602</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-08-18T14:56:28Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate cumulative sum by column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-cumulative-sum-by-column/m-p/889913#M351604</link>
      <description>Yes, these numbers 0,1,2 come from a frequency table.</description>
      <pubDate>Fri, 18 Aug 2023 15:15:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-cumulative-sum-by-column/m-p/889913#M351604</guid>
      <dc:creator>ANKH1</dc:creator>
      <dc:date>2023-08-18T15:15:41Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate cumulative sum by column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-cumulative-sum-by-column/m-p/889919#M351606</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dsin;
input ID age1 age2 age3 age4 age5 age6;
datalines;
1 1 0 0 1 0 0
2 0 1 1 0 0 0
3 0 0 0 1 0 0
4 0 0 0 0 0 0
5 1 0 0 0 1 0
6 0 0 1 0 0 0
;
run;

data _dsin / view=_dsin;
set dsin;
array age age1-age6;
flag=0;
do i=1 to dim(age);
if age(i) &amp;gt;= 1 and flag=0 then do; age(i)=1; flag=1; end
else if age(i) &amp;gt;= 1 and flag=1 then age(i) = 0;
end;
keep id age:;
run;

proc means data=_dsin noprint;
output out=agg1 sum=;
run;

data want;
set agg1;
array age age1-age6;

do i=2 to dim(age);
age(i) = age(i) + age(i-1);
end;
keep age:;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That modification should work &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Aug 2023 15:37:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-cumulative-sum-by-column/m-p/889919#M351606</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-08-18T15:37:04Z</dc:date>
    </item>
  </channel>
</rss>

