<?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 Create a count variable that accumulates events and accounts for no events in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-a-count-variable-that-accumulates-events-and-accounts-for/m-p/914280#M360256</link>
    <description>&lt;P&gt;In SAS I have two datasets.&lt;BR /&gt;Dataset1: age_event variable is the age at which each ID reported an event. One same ID can have one or more than one event. Not all IDs from the sample reported events. IDs that did not reported events were not included in this dataset.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data ds1;
input ID age_event;
datalines;
a1	32
b2	54&lt;BR /&gt;
b2	67
c3	34
c3	45
c3	78
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;Dataset2: All IDs of the sample are reported. This dataset contains one row per ID. variable "last_agerecorded" is the age at which each reported their last record for the whole study.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data main;
input ID last_agerecorded;
datalines;
a1	56
a2	67
b1	68
b2	72
b3	132
c2	121
c3	124
c4	58
d1	89
d2	95
e2	74
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;We would like to create a count_event variable that counts the number of events per ID. However, if an ID reports more than one event, count_event variable will need to add the total of events up to the last "age_event" reported. See cases for ID=b2 and c3 below. Each event has one row and the "count_event" variable sums the number of event at the last age_event. If the ID did not report any event at all, then age_event should be equal to "last_agerecorded" from the main dataset and the count_event should be equal to zero.&lt;/P&gt;&lt;P&gt;This is the required output:&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_event&lt;/TD&gt;&lt;TD&gt;count_event&lt;/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&gt;89&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;d2&lt;/TD&gt;&lt;TD&gt;95&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;e2&lt;/TD&gt;&lt;TD&gt;74&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can we get the required output? Thanks!&lt;/P&gt;</description>
    <pubDate>Fri, 02 Feb 2024 19:10:34 GMT</pubDate>
    <dc:creator>ANKH1</dc:creator>
    <dc:date>2024-02-02T19:10:34Z</dc:date>
    <item>
      <title>Create a count variable that accumulates events and accounts for no events</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-count-variable-that-accumulates-events-and-accounts-for/m-p/914280#M360256</link>
      <description>&lt;P&gt;In SAS I have two datasets.&lt;BR /&gt;Dataset1: age_event variable is the age at which each ID reported an event. One same ID can have one or more than one event. Not all IDs from the sample reported events. IDs that did not reported events were not included in this dataset.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data ds1;
input ID age_event;
datalines;
a1	32
b2	54&lt;BR /&gt;
b2	67
c3	34
c3	45
c3	78
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;Dataset2: All IDs of the sample are reported. This dataset contains one row per ID. variable "last_agerecorded" is the age at which each reported their last record for the whole study.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data main;
input ID last_agerecorded;
datalines;
a1	56
a2	67
b1	68
b2	72
b3	132
c2	121
c3	124
c4	58
d1	89
d2	95
e2	74
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;We would like to create a count_event variable that counts the number of events per ID. However, if an ID reports more than one event, count_event variable will need to add the total of events up to the last "age_event" reported. See cases for ID=b2 and c3 below. Each event has one row and the "count_event" variable sums the number of event at the last age_event. If the ID did not report any event at all, then age_event should be equal to "last_agerecorded" from the main dataset and the count_event should be equal to zero.&lt;/P&gt;&lt;P&gt;This is the required output:&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_event&lt;/TD&gt;&lt;TD&gt;count_event&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;a1&lt;/TD&gt;&lt;TD&gt;32&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;a2&lt;/TD&gt;&lt;TD&gt;67&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;b1&lt;/TD&gt;&lt;TD&gt;68&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;b2&lt;/TD&gt;&lt;TD&gt;54&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;b2&lt;/TD&gt;&lt;TD&gt;67&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;b3&lt;/TD&gt;&lt;TD&gt;132&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;c2&lt;/TD&gt;&lt;TD&gt;121&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;c3&lt;/TD&gt;&lt;TD&gt;34&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;c3&lt;/TD&gt;&lt;TD&gt;45&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;c3&lt;/TD&gt;&lt;TD&gt;78&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;c4&lt;/TD&gt;&lt;TD&gt;58&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;d1&lt;/TD&gt;&lt;TD&gt;89&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;d2&lt;/TD&gt;&lt;TD&gt;95&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;e2&lt;/TD&gt;&lt;TD&gt;74&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can we get the required output? Thanks!&lt;/P&gt;</description>
      <pubDate>Fri, 02 Feb 2024 19:10:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-count-variable-that-accumulates-events-and-accounts-for/m-p/914280#M360256</guid>
      <dc:creator>ANKH1</dc:creator>
      <dc:date>2024-02-02T19:10:34Z</dc:date>
    </item>
    <item>
      <title>Re: Create a count variable that accumulates events and accounts for no events</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-count-variable-that-accumulates-events-and-accounts-for/m-p/914363#M360294</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ds1;
input ID $ age_event;
datalines;
a1 32
b2 54
b2 67
c3 34
c3 45
c3 78
;
run;

data main;
input ID $ last_agerecorded;
datalines;
a1 56
a2 67
b1 68
b2 72
b3 132
c2 121
c3 124
c4 58
d1 89
d2 95
e2 74
;
run;

proc sort data=ds1;
 by ID age_event;
run;

data ds1;
 set ds1; 
  by ID;
   if first.ID then count_event = 0;
    count_event + 1;
run;

proc sql;
 create table want as 
  select ID
  ,	     age_event
  ,	     count_event
  from ds1
   outer union corresponding
  select ID
  ,	     last_agerecorded as age_event
  ,      0 as count_event
  from main
  where ID not in(select distinct ID from ds1)
 order by ID, count_event;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 02 Feb 2024 23:17:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-count-variable-that-accumulates-events-and-accounts-for/m-p/914363#M360294</guid>
      <dc:creator>ChanceTGardener</dc:creator>
      <dc:date>2024-02-02T23:17:54Z</dc:date>
    </item>
    <item>
      <title>Re: Create a count variable that accumulates events and accounts for no events</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-count-variable-that-accumulates-events-and-accounts-for/m-p/914376#M360299</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assuming the data are sorted by ID (main) or ID/age_event (DS1), you can use the interleaving process of SET with BY, as in:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ds1;
input ID $ age_event;
datalines;
a1 32
b2 54
b2 67
c3 34
c3 45
c3 78
;
run;

data main;
input ID $ last_agerecorded;
datalines;
a1 56
a2 67
b1 68
b2 72
b3 132
c2 121
c3 124
c4 58
d1 89
d2 95
e2 74
;

data want;
  set main (rename=last_agerecorded=age_event) ds1 (in=inds1);
  by id;
  if first.id then count=0;
  count+inds1;
  if inds1=1 or last.id;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 03 Feb 2024 12:57:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-count-variable-that-accumulates-events-and-accounts-for/m-p/914376#M360299</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-02-03T12:57:55Z</dc:date>
    </item>
  </channel>
</rss>

