<?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: Counting observations within time intervals in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Counting-observations-within-time-intervals/m-p/502323#M134080</link>
    <description>&lt;P&gt;You could&amp;nbsp;just align your datetime values to 5 minutes intervals.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  format dttm datetime20.;
  do dttm=datetime() to datetime()+10000 by 1;
    output;
  end;
  stop;
run;

data want;
  set have;
  format groupDttm datetime20.;
  groupDttm=intnx('dtminute5',dttm,0,'b');
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 08 Oct 2018 08:13:21 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2018-10-08T08:13:21Z</dc:date>
    <item>
      <title>Counting observations within time intervals</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-observations-within-time-intervals/m-p/502318#M134078</link>
      <description>&lt;P&gt;Dear all,&lt;/P&gt;&lt;P&gt;I am trying to convert my data set into an time interval data, and I would be glad if I can get some help on this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The current data that I have is a set of trade (or transaction) record, and it includes datetime information looking like this:&lt;/P&gt;&lt;P&gt;01JAN2018 09:00:00.00&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There are many other variables in the dataset, but it doesn't matter. Basically, it is a list of trade records with its traded time.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I am trying to do is to transform this data into a 5 min time interval data, and get the number of observations for each interval.&lt;/P&gt;&lt;P&gt;For example, if there are 15 obs in the first 5 minutes of the data and 10 obs in the second,&lt;/P&gt;&lt;P&gt;then I would like to have a new dataset with list(or table) looking like this:&lt;/P&gt;&lt;P&gt;interval1&amp;nbsp; &amp;nbsp; 15&lt;/P&gt;&lt;P&gt;interval2&amp;nbsp; &amp;nbsp; 10&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The last time I tried this, there weren't many obs, and I could do it manually, one by one.&lt;/P&gt;&lt;P&gt;But this time, there are thousands of obs and the given total time span is too long (more than a month).&lt;/P&gt;&lt;P&gt;So I would be happy get some advice on how I can code this efficiently and properly.&lt;/P&gt;&lt;P&gt;I hope I've been clear about what I am trying to say...&lt;/P&gt;</description>
      <pubDate>Mon, 08 Oct 2018 07:57:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-observations-within-time-intervals/m-p/502318#M134078</guid>
      <dc:creator>LzEr23</dc:creator>
      <dc:date>2018-10-08T07:57:42Z</dc:date>
    </item>
    <item>
      <title>Re: Counting observations within time intervals</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-observations-within-time-intervals/m-p/502320#M134079</link>
      <description>&lt;P&gt;This breaks down into two steps,&lt;/P&gt;
&lt;P&gt;- assign a variable for time window&lt;/P&gt;
&lt;P&gt;- count items grouped by new var&lt;/P&gt;
&lt;P&gt;As you have not provided any test data in the form of a datastep or what the output should look like, this is just a shell:&lt;/P&gt;
&lt;PRE&gt;data want;
  set have;
  by idvar;
  retain lsttime timewindow;
  if first.idvar then do
    timewindow=1;
    lsttime=datetime;
  end;
  else do;
    if intck('minute',datetime,lsttime) &amp;gt; 5 then do;
      timewindow=timewindow+1;
      lsttime=datetime;
    end;
  end;
run;
 
proc sql;
  create table counts as
  select  idvar,
             timewindow,
             count(*) as result
  from    want
  group by idvar,timewindow;
quit;&lt;/PRE&gt;
&lt;P&gt;Of course you will need to change var names and such like as I have&amp;nbsp;&lt;U&gt;&lt;STRONG&gt;no test data in datastep to work with!!&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Oct 2018 08:08:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-observations-within-time-intervals/m-p/502320#M134079</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-10-08T08:08:05Z</dc:date>
    </item>
    <item>
      <title>Re: Counting observations within time intervals</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-observations-within-time-intervals/m-p/502323#M134080</link>
      <description>&lt;P&gt;You could&amp;nbsp;just align your datetime values to 5 minutes intervals.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  format dttm datetime20.;
  do dttm=datetime() to datetime()+10000 by 1;
    output;
  end;
  stop;
run;

data want;
  set have;
  format groupDttm datetime20.;
  groupDttm=intnx('dtminute5',dttm,0,'b');
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 08 Oct 2018 08:13:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-observations-within-time-intervals/m-p/502323#M134080</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2018-10-08T08:13:21Z</dc:date>
    </item>
    <item>
      <title>Re: Counting observations within time intervals</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-observations-within-time-intervals/m-p/502325#M134082</link>
      <description>&lt;P&gt;Thank a lot for the help!&lt;/P&gt;&lt;P&gt;I can see how it works, but somehow the timewindow variable appears to be 1 all the way down.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The datastep is too much to be posted here but for the var names,&amp;nbsp; it looks like:&lt;/P&gt;&lt;P&gt;trade_date&amp;nbsp; &amp;nbsp; trade_code&amp;nbsp; &amp;nbsp; trade_price&amp;nbsp; &amp;nbsp; &amp;nbsp; datetime&lt;/P&gt;&lt;P&gt;20180101&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;AAAAAA&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 30&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 01JAN2018 09:00:00.00&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;20180101&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;AAAAAA&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 30&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 01JAN2018 09:00:01.27&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;20180101&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;AAAAAA&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 31&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 01JAN2018 09:00:02.12&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;20180101&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;AAAAAA&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 31&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 01JAN2018 10:00:01.01&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;20180102&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;AAAAAA&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 32&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 01JAN2018 09:00:00.00&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;where code is just a random code for identification.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I'm not sure what you mean by 'idvar' there.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Oct 2018 08:38:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-observations-within-time-intervals/m-p/502325#M134082</guid>
      <dc:creator>LzEr23</dc:creator>
      <dc:date>2018-10-08T08:38:24Z</dc:date>
    </item>
    <item>
      <title>Re: Counting observations within time intervals</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-observations-within-time-intervals/m-p/502326#M134083</link>
      <description>&lt;P&gt;idvar would just be whatever variables group the data, possibly trade_code in your example.&amp;nbsp; Do note that test data should be in a datastep and just needs to be illustrative of the problem:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Oct 2018 08:46:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-observations-within-time-intervals/m-p/502326#M134083</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-10-08T08:46:00Z</dc:date>
    </item>
    <item>
      <title>Re: Counting observations within time intervals</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-observations-within-time-intervals/m-p/502628#M134203</link>
      <description>&lt;P&gt;Thank you for the help.&lt;/P&gt;&lt;P&gt;What you suggested does work.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Only, that it doesn't include the interval without observations.&lt;/P&gt;&lt;P&gt;in case there are 5 obs in first 5mins, 12 obs in second, 5 in third,&lt;/P&gt;&lt;P&gt;it successfully count 5, 12, 5.&lt;/P&gt;&lt;P&gt;but when there are 5 obs in first, 0 ob in second and 5 tin third,&lt;/P&gt;&lt;P&gt;instead of counting 5, 0, 5,&lt;/P&gt;&lt;P&gt;it simply gives out 5, 5.&lt;/P&gt;&lt;P&gt;Would there be anyway to solve this problem? to get 0 for the 5min interval without any obs?&lt;/P&gt;</description>
      <pubDate>Tue, 09 Oct 2018 09:41:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-observations-within-time-intervals/m-p/502628#M134203</guid>
      <dc:creator>LzEr23</dc:creator>
      <dc:date>2018-10-09T09:41:04Z</dc:date>
    </item>
    <item>
      <title>Re: Counting observations within time intervals</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-observations-within-time-intervals/m-p/502629#M134204</link>
      <description>&lt;P&gt;Thank you.&lt;/P&gt;&lt;P&gt;your codes seem to work as well.&lt;/P&gt;&lt;P&gt;But still having the same problem I posted above.&lt;/P&gt;&lt;P&gt;But thank you for your suggestion, it did help a lot.&lt;/P&gt;</description>
      <pubDate>Tue, 09 Oct 2018 09:42:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-observations-within-time-intervals/m-p/502629#M134204</guid>
      <dc:creator>LzEr23</dc:creator>
      <dc:date>2018-10-09T09:42:32Z</dc:date>
    </item>
  </channel>
</rss>

