<?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: SAS assign group and accumulate in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/SAS-assign-group-and-accumulate/m-p/811857#M40642</link>
    <description>First, thank you for posting the sample starting data and expected results in working data steps.&amp;amp;nbsp; This made it possible for me to understand the objective, and to test my suggestion, which follows:&lt;BR /&gt;&amp;amp;nbsp;&lt;BR /&gt;data have;&lt;BR /&gt;length Event $15;&lt;BR /&gt;input Event $ Time;&lt;BR /&gt;datalines;&lt;BR /&gt;Event3_Start 0.2&lt;BR /&gt;Event2_Start 0.4&lt;BR /&gt;Event2_End 0.2&lt;BR /&gt;Event1_Stop 0.2&lt;BR /&gt;Event3_Start 0&lt;BR /&gt;Event4_Start 0.5&lt;BR /&gt;Event3_Stop 0.2&lt;BR /&gt;Event1_Start 0&lt;BR /&gt;Event4_Stop 0&lt;BR /&gt;Event4_Stop 0&lt;BR /&gt;Event1_Stop 0.3&lt;BR /&gt;Event3_Start 0.3&lt;BR /&gt;Event1_Start 0&lt;BR /&gt;Event3_Start 0.4&lt;BR /&gt;Event3_Stop 0&lt;BR /&gt;Event1_Stop 0.2&lt;BR /&gt;Event3_Start 0.2&lt;BR /&gt;Event2_Start 0.4&lt;BR /&gt;run;&lt;BR /&gt;data stop_to_start (keep=group cumulative);&lt;BR /&gt;  set have end=end_of_have;&lt;BR /&gt;  group+(event='Event1_Stop');&lt;BR /&gt;  if event='Event1_Stop' then  cumulative=0;&lt;BR /&gt;  cumulative+time;&lt;BR /&gt;  if end_of_have or event='Event1_Start' ;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;data want;&lt;BR /&gt;  set have;&lt;BR /&gt;  if _n_=1 or event='Event1_Start' then group=0;&lt;BR /&gt;  cumulative=0;&lt;BR /&gt;  if event='Event1_Stop' then set stop_to_start;&lt;BR /&gt;run;&lt;BR /&gt;The strategy here is to make an intermediate dataset, stop_to_start with one observation per time span , containing two variables: cumulative and group.&lt;BR /&gt;&amp;amp;nbsp;&lt;BR /&gt;Then just read HAVE, set cumulative to 0, and read a stop_to_start observation only when you encounter an Event1_Stop record from have.&amp;amp;nbsp; This updates cumulative and group.&lt;BR /&gt;&amp;amp;nbsp;&lt;BR /&gt;Because the group variable is read by a SET stop_to_start statement, it is automatically retained&amp;amp;nbsp; So resetting it to zero when you encounter an Event1_Start observation, it will be propagated as zero until the next Event1_Stop.&lt;BR /&gt;&amp;amp;nbsp;</description>
    <pubDate>Fri, 06 May 2022 15:59:30 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2022-05-06T15:59:30Z</dc:date>
    <item>
      <title>SAS assign group and accumulate</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/SAS-assign-group-and-accumulate/m-p/811835#M40641</link>
      <description>&lt;P&gt;Hi experts,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset which have columns Event and Time. I need to create columns Group and Cumulative. What I need to measure is the duration of the Event 'Event1_Stop' until an 'Event1_Start' appears. Last group should sum the time meaning that the stop is ongoing and no start for the event has entered.&lt;BR /&gt;My data sample is:&lt;/P&gt;&lt;PRE&gt;data have;
length Event $15;
input Event $ Time;
datalines;
Event3_Start 0.2
Event2_Start 0.4
Event2_End 0.2
Event1_Stop 0.2
Event3_Start 0
Event4_Start 0.5
Event3_Stop 0.2
Event1_Start 0
Event4_Stop 0
Event4_Stop 0
Event1_Stop 0.3
Event3_Start 0.3
Event1_Start 0
Event3_Start 0.4
Event3_Stop 0
Event1_Stop 0.2
Event3_Start 0.2
Event2_Start 0.4
run;&lt;/PRE&gt;&lt;P&gt;The result dataset that I need to obtain is:&lt;/P&gt;&lt;PRE&gt;data have;
length Event $15;
input Event $ Time Group Cumulative;
datalines;
Event3_Start 0.2 0 0
Event2_Start 0.4 0 0
Event2_End 0.2 0 0
Event1_Stop 0.2 1 0.9
Event3_Start 0 1 0
Event4_Start 0.5 1 0
Event3_Stop 0.2 1 0
Event1_Start 0 0 0
Event4_Stop 0 0 0
Event4_Stop 0 0 0
Event1_Stop 0.3 2 0.6
Event3_Start 0.3 2 0
Event1_Start 0 0 0
Event3_Start 0.4 0 0
Event3_Stop 0 0 0
Event1_Stop 0.2 3 0.8
Event3_Start 0.2 3 0
Event2_Start 0.4 3 0
run;&lt;/PRE&gt;&lt;P&gt;Thanks for your suggestions.&lt;BR /&gt;Regards.&lt;/P&gt;</description>
      <pubDate>Fri, 06 May 2022 10:55:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/SAS-assign-group-and-accumulate/m-p/811835#M40641</guid>
      <dc:creator>MM88</dc:creator>
      <dc:date>2022-05-06T10:55:21Z</dc:date>
    </item>
    <item>
      <title>Re: SAS assign group and accumulate</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/SAS-assign-group-and-accumulate/m-p/811857#M40642</link>
      <description>First, thank you for posting the sample starting data and expected results in working data steps.&amp;amp;nbsp; This made it possible for me to understand the objective, and to test my suggestion, which follows:&lt;BR /&gt;&amp;amp;nbsp;&lt;BR /&gt;data have;&lt;BR /&gt;length Event $15;&lt;BR /&gt;input Event $ Time;&lt;BR /&gt;datalines;&lt;BR /&gt;Event3_Start 0.2&lt;BR /&gt;Event2_Start 0.4&lt;BR /&gt;Event2_End 0.2&lt;BR /&gt;Event1_Stop 0.2&lt;BR /&gt;Event3_Start 0&lt;BR /&gt;Event4_Start 0.5&lt;BR /&gt;Event3_Stop 0.2&lt;BR /&gt;Event1_Start 0&lt;BR /&gt;Event4_Stop 0&lt;BR /&gt;Event4_Stop 0&lt;BR /&gt;Event1_Stop 0.3&lt;BR /&gt;Event3_Start 0.3&lt;BR /&gt;Event1_Start 0&lt;BR /&gt;Event3_Start 0.4&lt;BR /&gt;Event3_Stop 0&lt;BR /&gt;Event1_Stop 0.2&lt;BR /&gt;Event3_Start 0.2&lt;BR /&gt;Event2_Start 0.4&lt;BR /&gt;run;&lt;BR /&gt;data stop_to_start (keep=group cumulative);&lt;BR /&gt;  set have end=end_of_have;&lt;BR /&gt;  group+(event='Event1_Stop');&lt;BR /&gt;  if event='Event1_Stop' then  cumulative=0;&lt;BR /&gt;  cumulative+time;&lt;BR /&gt;  if end_of_have or event='Event1_Start' ;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;data want;&lt;BR /&gt;  set have;&lt;BR /&gt;  if _n_=1 or event='Event1_Start' then group=0;&lt;BR /&gt;  cumulative=0;&lt;BR /&gt;  if event='Event1_Stop' then set stop_to_start;&lt;BR /&gt;run;&lt;BR /&gt;The strategy here is to make an intermediate dataset, stop_to_start with one observation per time span , containing two variables: cumulative and group.&lt;BR /&gt;&amp;amp;nbsp;&lt;BR /&gt;Then just read HAVE, set cumulative to 0, and read a stop_to_start observation only when you encounter an Event1_Stop record from have.&amp;amp;nbsp; This updates cumulative and group.&lt;BR /&gt;&amp;amp;nbsp;&lt;BR /&gt;Because the group variable is read by a SET stop_to_start statement, it is automatically retained&amp;amp;nbsp; So resetting it to zero when you encounter an Event1_Start observation, it will be propagated as zero until the next Event1_Stop.&lt;BR /&gt;&amp;amp;nbsp;</description>
      <pubDate>Fri, 06 May 2022 15:59:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/SAS-assign-group-and-accumulate/m-p/811857#M40642</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-05-06T15:59:30Z</dc:date>
    </item>
    <item>
      <title>Re: SAS assign group and accumulate</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/SAS-assign-group-and-accumulate/m-p/812318#M40650</link>
      <description>Thanks &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;, your answer was very helpful. Sorry for the late reply, I was modifying your code to fullfill other restrictions on my data.</description>
      <pubDate>Tue, 10 May 2022 08:12:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/SAS-assign-group-and-accumulate/m-p/812318#M40650</guid>
      <dc:creator>MM88</dc:creator>
      <dc:date>2022-05-10T08:12:38Z</dc:date>
    </item>
  </channel>
</rss>

