<?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: Calculate cumulative cases by age and ID in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculate-cumulative-cases-by-age-and-ID/m-p/890723#M351953</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards truncover expandtabs;
value=1;
input ID	age;
cards;
1	131
1	0
1	0
2	22
2	76
3	0
3	48
3	48
3	3
;
proc sort data=have out=temp nodupkey;
by age id;
run;
proc transpose data=temp out=temp2(drop=_name_) prefix=_;
by age;
id id;
var value;
run;
data temp2;
 set temp2;
 id=1;
run;
data temp3(drop=id);
 update temp2(obs=0) temp2;
 by id;
 output;
run;
data want;
 set temp3;
 Cumulative=sum(of _:);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 24 Aug 2023 11:42:17 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2023-08-24T11:42:17Z</dc:date>
    <item>
      <title>Calculate cumulative cases by age and ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-cumulative-cases-by-age-and-ID/m-p/890684#M351931</link>
      <description>&lt;P&gt;This is sample of a dataset in the long format:&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;age&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;131&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;22&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;76&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;48&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;48&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;We then ran the following to get the frequencies of every ID by age&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc freq data=dsin;&lt;/P&gt;&lt;P&gt;table ID*age;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;And the output looks like:&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;age&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;22&lt;/TD&gt;&lt;TD&gt;48&lt;/TD&gt;&lt;TD&gt;76&lt;/TD&gt;&lt;TD&gt;131&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;But now we also need to get the cumulative number of people who had an incident up until every 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 doesn't contribute to increasing the count anymore. Let's say for ID3 it presents an incident at age 0, 3 and 48. But this person can only contributes to the count at age 0. And also, if they have more than one incident at the same age, it counts as 1. The output should look like this:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Age&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;Cumulative&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;22&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;48&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;76&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;131&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 23 Aug 2023 22:35:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-cumulative-cases-by-age-and-ID/m-p/890684#M351931</guid>
      <dc:creator>ANKH1</dc:creator>
      <dc:date>2023-08-23T22:35:20Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate cumulative cases by age and ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-cumulative-cases-by-age-and-ID/m-p/890685#M351932</link>
      <description>&lt;P&gt;I can &lt;STRONG&gt;guess&lt;/STRONG&gt; that "had an incident" means every age recorded is an incident. What I can't decipher is what "up until every age" or " it is accounted for" means.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;"Let's say for ID3 it presents an incident at age 0, 3 and 48. But this person can only contributes to the count at age 0." WHY? Exactly what in the data tells us to stop considering at 0?&lt;/P&gt;</description>
      <pubDate>Wed, 23 Aug 2023 22:55:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-cumulative-cases-by-age-and-ID/m-p/890685#M351932</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-08-23T22:55:22Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate cumulative cases by age and ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-cumulative-cases-by-age-and-ID/m-p/890686#M351933</link>
      <description>So at each age you sum the IDs that have values not equal to zero. Values 1,2,3 at each age means, that at that age an incident was reported. We want to know at each age how many IDs have accumulated first incidents. But if an ID reports incidents at more than one age, this ID will be accounted when their first incident is reported.</description>
      <pubDate>Wed, 23 Aug 2023 23:05:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-cumulative-cases-by-age-and-ID/m-p/890686#M351933</guid>
      <dc:creator>ANKH1</dc:creator>
      <dc:date>2023-08-23T23:05:56Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate cumulative cases by age and ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-cumulative-cases-by-age-and-ID/m-p/890701#M351943</link>
      <description>&lt;P&gt;I am not sure, that is understand your request completely. The following steps seem to create what you expect. In future questions please post data in usable form.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   input ID age;
   datalines;
1 131
1 0
1 0
2 22
2 76
3 0
3 48
3 48
3 3
;

/* Get lowest age for each id */
proc sql;
   create table work.selection as
      select distinct Id, Age
         from work.have
         group by Id
         having Age = min(Age)
         order by Age
      ;
quit;

/* Get all ages */
proc sort data= work.have(keep= Age) out= work.ages nodupkey;
   by Age;
run;

/* Combine and count */
data want;
   merge work.ages work.selection(keep= Age in= inc);
   by Age;

   if inc then Cumulative + 1;

   if last.Age then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 24 Aug 2023 07:29:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-cumulative-cases-by-age-and-ID/m-p/890701#M351943</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2023-08-24T07:29:17Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate cumulative cases by age and ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-cumulative-cases-by-age-and-ID/m-p/890723#M351953</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards truncover expandtabs;
value=1;
input ID	age;
cards;
1	131
1	0
1	0
2	22
2	76
3	0
3	48
3	48
3	3
;
proc sort data=have out=temp nodupkey;
by age id;
run;
proc transpose data=temp out=temp2(drop=_name_) prefix=_;
by age;
id id;
var value;
run;
data temp2;
 set temp2;
 id=1;
run;
data temp3(drop=id);
 update temp2(obs=0) temp2;
 by id;
 output;
run;
data want;
 set temp3;
 Cumulative=sum(of _:);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 24 Aug 2023 11:42:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-cumulative-cases-by-age-and-ID/m-p/890723#M351953</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-08-24T11:42:17Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate cumulative cases by age and ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-cumulative-cases-by-age-and-ID/m-p/890748#M351970</link>
      <description>&lt;P&gt;So you seem to want to count the subjects at their first incident age.&amp;nbsp; So first reduce the data to just the minimum age per subject.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   input ID age;
datalines;
1 131
1 0
1 0
2 22
2 76
3 0
3 48
3 48
3 3
;

proc means data=have ;
  by id ;
  var age ;
  output out=cases(drop=_freq_ _type_ id) min=;
run;

proc sort data=cases ;
  by age;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You seem to want to include all of the other age points in your report so get that from the original data also and then combine the two.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc sort data=have(keep=age) out=age_points nodupkey;
  by age;
run;

data want;
  merge cases(in=incases) age_points;
  by age;
  cummulative + incases;
  if last.age;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;Obs    age    cummulative

 1       0         2
 2       3         2
 3      22         3
 4      48         3
 5      76         3
 6     131         3
&lt;/PRE&gt;</description>
      <pubDate>Thu, 24 Aug 2023 13:38:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-cumulative-cases-by-age-and-ID/m-p/890748#M351970</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-08-24T13:38:14Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate cumulative cases by age and ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-cumulative-cases-by-age-and-ID/m-p/890757#M351974</link>
      <description>&lt;P&gt;Thank you! I will post data in usable form in the future.&lt;/P&gt;</description>
      <pubDate>Thu, 24 Aug 2023 13:58:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-cumulative-cases-by-age-and-ID/m-p/890757#M351974</guid>
      <dc:creator>ANKH1</dc:creator>
      <dc:date>2023-08-24T13:58:23Z</dc:date>
    </item>
  </channel>
</rss>

