<?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: Trouble trying to Count Simultaneous Events in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Trouble-trying-to-Count-Simultaneous-Events/m-p/37680#M9620</link>
    <description>Thanks to Scott and Vasile for trying to help me with this.  &lt;BR /&gt;
&lt;BR /&gt;
After battIing this for days I just ended up just doing it in Excel.  If anyone ever stops by here and is fluent in both vba and sas maybe they could help me with the translation, I'd really like to know a way to tackle this in SAS.  I'd hoped SAS would put an end to jumping around between Access and Excel, but I guess they all have their place.&lt;BR /&gt;
&lt;BR /&gt;
Thanks, Mike&lt;BR /&gt;
&lt;BR /&gt;
Sub ActiveIncidents()&lt;BR /&gt;
&lt;BR /&gt;
Dim irow, lastrow As Integer&lt;BR /&gt;
Dim frmla As String&lt;BR /&gt;
&lt;BR /&gt;
lastrow = ActiveSheet.UsedRange.Rows.Count&lt;BR /&gt;
&lt;BR /&gt;
For irow = 2 To lastrow&lt;BR /&gt;
&lt;BR /&gt;
frmla = "=countif(M2:M" &amp;amp; irow &amp;amp; "," &amp;amp; """GT" &amp;amp; """&amp;amp;L" &amp;amp; irow &amp;amp; ")"&lt;BR /&gt;
ActiveSheet.Cells(irow, 15).Formula = frmla&lt;BR /&gt;
Next irow&lt;BR /&gt;
&lt;BR /&gt;
'for posting "GT" used instead of greater than sign&lt;BR /&gt;
&lt;BR /&gt;
End Sub&lt;BR /&gt;
&lt;BR /&gt;
Message was edited by: Mike D</description>
    <pubDate>Thu, 07 Jan 2010 22:37:03 GMT</pubDate>
    <dc:creator>Mike_D</dc:creator>
    <dc:date>2010-01-07T22:37:03Z</dc:date>
    <item>
      <title>Trouble trying to Count Simultaneous Events</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Trouble-trying-to-Count-Simultaneous-Events/m-p/37675#M9615</link>
      <description>Hi, I am still very new to SAS and am having a problem with figuring out the following problem.&lt;BR /&gt;
&lt;BR /&gt;
I’d be most appreciative for any guidance on how to tackle this problem.  I’ve tried using between…and, do…while, lag, and retain, but I’m missing something that’s preventing me from solving this.  &lt;BR /&gt;
&lt;BR /&gt;
Here’s a sample dataset I have:&lt;BR /&gt;
&lt;BR /&gt;
Id unit time1 time2&lt;BR /&gt;
&lt;BR /&gt;
1 E1 12:00:01 13:00:00   &lt;BR /&gt;
2 E2 12:15:00 13:01:00&lt;BR /&gt;
4 E3 12:25:00 13:15:00&lt;BR /&gt;
5 E1 13:20:00 13:45:00&lt;BR /&gt;
6 E4 13:30:00 14:15:00&lt;BR /&gt;
7 E2 13:50:00 14:30:00&lt;BR /&gt;
8 E4 15:00:00 15:10:00&lt;BR /&gt;
11 E1 15:05:00 16:00:00&lt;BR /&gt;
&lt;BR /&gt;
Below is the dataset I’d like to end up with.  &lt;BR /&gt;
&lt;BR /&gt;
The only thing that has changed is that there is now a fifth variable (events).  Events describes the number of events that are active.  The events count is explained below.&lt;BR /&gt;
&lt;BR /&gt;
Id Unit Time1 Time2 Events&lt;BR /&gt;
&lt;BR /&gt;
1 E1 12:00:01 13:00:00   1&lt;BR /&gt;
2 E2 12:15:00 13:19:00   2&lt;BR /&gt;
3 E3 12:25:00 13:15:00   3&lt;BR /&gt;
4 E1 13:20:00 13:45:00   1&lt;BR /&gt;
5 E4 13:30:00 14:15:00   2&lt;BR /&gt;
6 E2 13:50:00 14:30:00   2&lt;BR /&gt;
8 E4 15:00:00 15:10:00   1&lt;BR /&gt;
11 E1 15:05:00 16:00:00 2&lt;BR /&gt;
&lt;BR /&gt;
The first obs has an Events value of 1 because it is the only active event.&lt;BR /&gt;
&lt;BR /&gt;
The second Events has a value of 2 because when event 2 started event 1 was still in progress.&lt;BR /&gt;
&lt;BR /&gt;
The third Event has value of 3 because Events 1 and 2 were still active when obs 3 began. (Time1 for the third obs is &amp;lt; time2 for both obs 1 and obs2).&lt;BR /&gt;
&lt;BR /&gt;
Event four has a value of 1 because a check of the Time2(s) above show all have ended prior to time1 for the fourth obs.&lt;BR /&gt;
&lt;BR /&gt;
The goal is to know how many concurrent events are occurring during a given period, based on time1 being less than the time2 for all the obs prior.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
The following code works (kind of, but it is the closet I gotten)&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Data  new;&lt;BR /&gt;
 Set org;&lt;BR /&gt;
&lt;BR /&gt;
If time1 lt time2 then Events = ‘1’;&lt;BR /&gt;
If time1 lt time2 and time1 lt lag(time2) then Events = ‘2’;&lt;BR /&gt;
&lt;BR /&gt;
Run;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
But this is where I’m stuck.  It is possible that lag(time2) has concluded, but lag2(time2) and lag3(time2) have not, and the if…then statements don’t seem to be able to handle this.&lt;BR /&gt;
&lt;BR /&gt;
Ultimately what I need to be able to do for each time1 is to be able to compare it again against each time2 “above” it and ascertain how many active events I have going on.&lt;BR /&gt;
&lt;BR /&gt;
My attempts with loops, retain, sum, and lagX(time2) have not even come close, but my guess is they are all needed.  My thought is I need someway to create a loop that says I'm on the 10th obs and need to check  time2 obs 10, 9, 8, ... with sum+1 for each time it finds a time2 that is &amp;gt; the time1 for obs ten.&lt;BR /&gt;
&lt;BR /&gt;
I hope this explanation has been clear and someone can point me in the right direction.&lt;BR /&gt;
&lt;BR /&gt;
A couple of notes:  Not every 'id' is included in the dataset because not every 'id' resulted in an event.  Also, some 'id's could result in multiple events.  And the actual dataset has the following date time format 01Jan09:13:00:00.  I'm not sure if any of this matters for the solution, but I certainly want to make this as clear as possible.&lt;BR /&gt;
&lt;BR /&gt;
Thanks, Mike</description>
      <pubDate>Wed, 06 Jan 2010 19:13:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Trouble-trying-to-Count-Simultaneous-Events/m-p/37675#M9615</guid>
      <dc:creator>Mike_D</dc:creator>
      <dc:date>2010-01-06T19:13:00Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble trying to Count Simultaneous Events</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Trouble-trying-to-Count-Simultaneous-Events/m-p/37676#M9616</link>
      <description>Implicitly you will need to first sort your data to ensure that it is in proper sequence.  My recommendation is to track your "minimum concurrent event" time with a RETAIN and a temporary SAS variable rather than using LAG.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Wed, 06 Jan 2010 21:09:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Trouble-trying-to-Count-Simultaneous-Events/m-p/37676#M9616</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2010-01-06T21:09:56Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble trying to Count Simultaneous Events</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Trouble-trying-to-Count-Simultaneous-Events/m-p/37677#M9617</link>
      <description>Thanks Scott,&lt;BR /&gt;
&lt;BR /&gt;
Just to make sure I'm completely understand I should probably use Retain to capture and compare the current time1 and assign the time2 to a temporary variable and compare the temp vairable to the retain value?&lt;BR /&gt;
&lt;BR /&gt;
Thanks again, Mike</description>
      <pubDate>Wed, 06 Jan 2010 22:52:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Trouble-trying-to-Count-Simultaneous-Events/m-p/37677#M9617</guid>
      <dc:creator>Mike_D</dc:creator>
      <dc:date>2010-01-06T22:52:50Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble trying to Count Simultaneous Events</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Trouble-trying-to-Count-Simultaneous-Events/m-p/37678#M9618</link>
      <description>For whatever your "concurent event matching" criteria, keep track of the oldest start-time (and consider that I don't know what you're doing about date-change?) and compare that time to the TIME1 and TIME2 (range) values of subsequent observations, I suppose.&lt;BR /&gt;
&lt;BR /&gt;
You may want to map the process out on paper as a "stick figures" approach to developing a determination methodology and then apply that to the SAS language with your variables.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Wed, 06 Jan 2010 23:11:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Trouble-trying-to-Count-Simultaneous-Events/m-p/37678#M9618</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2010-01-06T23:11:55Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble trying to Count Simultaneous Events</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Trouble-trying-to-Count-Simultaneous-Events/m-p/37679#M9619</link>
      <description>For every observation, you want to get the number of events unfinished at &lt;I&gt;time1&lt;/I&gt;. I would sort the data after &lt;I&gt;time1&lt;/I&gt; variable. I don't think you could avoid keeping sequential the &lt;I&gt;time2&lt;/I&gt; of the unfinished events.</description>
      <pubDate>Thu, 07 Jan 2010 00:19:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Trouble-trying-to-Count-Simultaneous-Events/m-p/37679#M9619</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2010-01-07T00:19:27Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble trying to Count Simultaneous Events</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Trouble-trying-to-Count-Simultaneous-Events/m-p/37680#M9620</link>
      <description>Thanks to Scott and Vasile for trying to help me with this.  &lt;BR /&gt;
&lt;BR /&gt;
After battIing this for days I just ended up just doing it in Excel.  If anyone ever stops by here and is fluent in both vba and sas maybe they could help me with the translation, I'd really like to know a way to tackle this in SAS.  I'd hoped SAS would put an end to jumping around between Access and Excel, but I guess they all have their place.&lt;BR /&gt;
&lt;BR /&gt;
Thanks, Mike&lt;BR /&gt;
&lt;BR /&gt;
Sub ActiveIncidents()&lt;BR /&gt;
&lt;BR /&gt;
Dim irow, lastrow As Integer&lt;BR /&gt;
Dim frmla As String&lt;BR /&gt;
&lt;BR /&gt;
lastrow = ActiveSheet.UsedRange.Rows.Count&lt;BR /&gt;
&lt;BR /&gt;
For irow = 2 To lastrow&lt;BR /&gt;
&lt;BR /&gt;
frmla = "=countif(M2:M" &amp;amp; irow &amp;amp; "," &amp;amp; """GT" &amp;amp; """&amp;amp;L" &amp;amp; irow &amp;amp; ")"&lt;BR /&gt;
ActiveSheet.Cells(irow, 15).Formula = frmla&lt;BR /&gt;
Next irow&lt;BR /&gt;
&lt;BR /&gt;
'for posting "GT" used instead of greater than sign&lt;BR /&gt;
&lt;BR /&gt;
End Sub&lt;BR /&gt;
&lt;BR /&gt;
Message was edited by: Mike D</description>
      <pubDate>Thu, 07 Jan 2010 22:37:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Trouble-trying-to-Count-Simultaneous-Events/m-p/37680#M9620</guid>
      <dc:creator>Mike_D</dc:creator>
      <dc:date>2010-01-07T22:37:03Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble trying to Count Simultaneous Events</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Trouble-trying-to-Count-Simultaneous-Events/m-p/37681#M9621</link>
      <description>Hi Mike&lt;BR /&gt;
&lt;BR /&gt;
The following code should do what you asked for (using the example data provided):&lt;BR /&gt;
&lt;BR /&gt;
data have;&lt;BR /&gt;
  infile datalines truncover;&lt;BR /&gt;
  input Id unit $ time1 time. time2 time.;&lt;BR /&gt;
  format time1 time2 time8.;&lt;BR /&gt;
  datalines;&lt;BR /&gt;
1 E1 12:00:01 13:00:00 &lt;BR /&gt;
2 E2 12:15:00 13:01:00&lt;BR /&gt;
4 E3 12:25:00 13:15:00&lt;BR /&gt;
5 E1 13:20:00 13:45:00&lt;BR /&gt;
6 E4 13:30:00 14:15:00&lt;BR /&gt;
7 E2 13:50:00 14:30:00&lt;BR /&gt;
8 E4 15:00:00 15:10:00&lt;BR /&gt;
11 E1 15:05:00 16:00:00&lt;BR /&gt;
;&lt;BR /&gt;
&lt;BR /&gt;
proc sql;&lt;BR /&gt;
  select l.Id, l.unit, l.time1, l.time2, count(*) as Event&lt;BR /&gt;
    from have as l left join have as r&lt;BR /&gt;
      on l.id &amp;gt;= r.id and l.time1&lt;R.TIME2&gt;&lt;BR /&gt;
    group by l.Id, l.unit, l.time1, l.time2&lt;BR /&gt;
  ;&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
The assumptions are:&lt;BR /&gt;
- The id's are ascending based on ascending start times (time1)&lt;BR /&gt;
- There are no duplicate start times&lt;BR /&gt;
- Data is from 1 day (not passing midnight).&lt;BR /&gt;
&lt;BR /&gt;
Change of date: It would be safer to use datetime values instead of time values.&lt;BR /&gt;
&lt;BR /&gt;
HTH&lt;BR /&gt;
Patrick&lt;/R.TIME2&gt;</description>
      <pubDate>Fri, 08 Jan 2010 00:10:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Trouble-trying-to-Count-Simultaneous-Events/m-p/37681#M9621</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2010-01-08T00:10:56Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble trying to Count Simultaneous Events</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Trouble-trying-to-Count-Simultaneous-Events/m-p/37682#M9622</link>
      <description>Hi Patrick,&lt;BR /&gt;
&lt;BR /&gt;
Thanks I really do want to be able to figure this out in SAS.  I'll give it a shot.&lt;BR /&gt;
&lt;BR /&gt;
Mike</description>
      <pubDate>Fri, 08 Jan 2010 16:29:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Trouble-trying-to-Count-Simultaneous-Events/m-p/37682#M9622</guid>
      <dc:creator>Mike_D</dc:creator>
      <dc:date>2010-01-08T16:29:13Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble trying to Count Simultaneous Events</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Trouble-trying-to-Count-Simultaneous-Events/m-p/37683#M9623</link>
      <description>data times;&lt;BR /&gt;
	input id unit $ time1 $ time2 $;&lt;BR /&gt;
	length time1 $ 8 time2 $ 8;&lt;BR /&gt;
	datalines;&lt;BR /&gt;
1 E1 12:00:01 13:00:00 &lt;BR /&gt;
2 E2 12:15:00 13:01:00&lt;BR /&gt;
4 E3 12:25:00 13:15:00&lt;BR /&gt;
11 E1 13:06:00 13:09:00&lt;BR /&gt;
;&lt;BR /&gt;
proc sort data=times out=sort_times;&lt;BR /&gt;
	by time1;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
proc sql;&lt;BR /&gt;
	create table new like sort_times;&lt;BR /&gt;
	alter table new add events num;&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
%macro event(t, i);&lt;BR /&gt;
	%let t1=trim("&amp;amp;t");&lt;BR /&gt;
	data temp (keep= id unit time1 time2 events);&lt;BR /&gt;
		set sort_times;	&lt;BR /&gt;
		retain events 0;&lt;BR /&gt;
 if trim(time2)&amp;gt;= &amp;amp;t1 &amp;amp; trim(time1)&amp;lt; &amp;amp;t1 then events+1;&lt;BR /&gt;
		&lt;BR /&gt;
		if id=&amp;amp;i then do;&lt;BR /&gt;
			output temp;&lt;BR /&gt;
			stop;&lt;BR /&gt;
		end;&lt;BR /&gt;
&lt;BR /&gt;
	proc append base=new&lt;BR /&gt;
		data=temp; &lt;BR /&gt;
	run;&lt;BR /&gt;
%mend;&lt;BR /&gt;
&lt;BR /&gt;
data _null_;&lt;BR /&gt;
	set sort_times;&lt;BR /&gt;
	call symputx('timex', trim(time1));&lt;BR /&gt;
	call symputx('idx', id);&lt;BR /&gt;
	call execute ('%event(&amp;amp;timex, &amp;amp;idx)');	&lt;BR /&gt;
	proc print data=new;&lt;BR /&gt;
run;</description>
      <pubDate>Fri, 08 Jan 2010 17:13:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Trouble-trying-to-Count-Simultaneous-Events/m-p/37683#M9623</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2010-01-08T17:13:32Z</dc:date>
    </item>
  </channel>
</rss>

