<?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 Counting observations within a time interval in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Counting-observations-within-a-time-interval/m-p/742785#M232419</link>
    <description>&lt;P&gt;Below I have trade-level data. Each row represents the date in which an insider made a transaction. I am trying to determine the number of trades made by an insider within a 3 year interval before the transaction date.&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the example below we can see Bowen made a number of trades in multiple years.&amp;nbsp;&lt;/P&gt;&lt;P&gt;In 2004, he would have made 0 trades in the preceding 3 years (2001/2002/2003).&lt;/P&gt;&lt;P&gt;In 2007, he would have only made 1 trade in the preceding 3 years (2004/2005/2006).&lt;/P&gt;&lt;P&gt;I am trying to generate this information for all my observations.&lt;/P&gt;&lt;P&gt;I would like the frequency of trades within the preceding 3 years to be another column in my dataset because I am trying to eliminate observations in which there was less than 3 trades in the preceding 3 years.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Mistletoad_0-1621542489468.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/59637iE0EA198766E830DE/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Mistletoad_0-1621542489468.png" alt="Mistletoad_0-1621542489468.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 20 May 2021 20:32:22 GMT</pubDate>
    <dc:creator>Mistletoad</dc:creator>
    <dc:date>2021-05-20T20:32:22Z</dc:date>
    <item>
      <title>Counting observations within a time interval</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-observations-within-a-time-interval/m-p/742785#M232419</link>
      <description>&lt;P&gt;Below I have trade-level data. Each row represents the date in which an insider made a transaction. I am trying to determine the number of trades made by an insider within a 3 year interval before the transaction date.&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the example below we can see Bowen made a number of trades in multiple years.&amp;nbsp;&lt;/P&gt;&lt;P&gt;In 2004, he would have made 0 trades in the preceding 3 years (2001/2002/2003).&lt;/P&gt;&lt;P&gt;In 2007, he would have only made 1 trade in the preceding 3 years (2004/2005/2006).&lt;/P&gt;&lt;P&gt;I am trying to generate this information for all my observations.&lt;/P&gt;&lt;P&gt;I would like the frequency of trades within the preceding 3 years to be another column in my dataset because I am trying to eliminate observations in which there was less than 3 trades in the preceding 3 years.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Mistletoad_0-1621542489468.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/59637iE0EA198766E830DE/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Mistletoad_0-1621542489468.png" alt="Mistletoad_0-1621542489468.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 May 2021 20:32:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-observations-within-a-time-interval/m-p/742785#M232419</guid>
      <dc:creator>Mistletoad</dc:creator>
      <dc:date>2021-05-20T20:32:22Z</dc:date>
    </item>
    <item>
      <title>Re: Counting observations within a time interval</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-observations-within-a-time-interval/m-p/742786#M232420</link>
      <description>&lt;P&gt;Can you provide that "example" as text? Or better yet, provide some example data in the form of data step code? It is awful hard to code against a picture.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Second, are your "dates" actually SAS date values? That would be numeric variable with a yymmn8. format from what you show. If not the first thing will be to create such so the concept of "with in time interval" makes sense.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It might also help to show how you expect the result to appear. Not quite clear what you want.&lt;/P&gt;</description>
      <pubDate>Thu, 20 May 2021 20:44:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-observations-within-a-time-interval/m-p/742786#M232420</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-05-20T20:44:34Z</dc:date>
    </item>
    <item>
      <title>Re: Counting observations within a time interval</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-observations-within-a-time-interval/m-p/742788#M232422</link>
      <description>&lt;P&gt;Hey ballardw,&lt;/P&gt;&lt;P&gt;Apologies for the initial post, this is my first time posting on this forum.&lt;/P&gt;&lt;P&gt;Does this help?&lt;/P&gt;&lt;P&gt;And yes the dates are actually SAS date values.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV class="dgrid-header dgrid-header-row ui-widget-header"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="dgrid-scroller"&gt;&lt;DIV class="dgrid-content ui-widget-content"&gt;&lt;DIV class=" dgrid-row dgrid-row-even ui-state-default"&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2004&lt;/TD&gt;&lt;TD&gt;20041108&lt;/TD&gt;&lt;TD&gt;BOWEN KEVIN C&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;&lt;DIV class=" dgrid-row dgrid-row-odd ui-state-default"&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;2007&lt;/TD&gt;&lt;TD&gt;20070618&lt;/TD&gt;&lt;TD&gt;BOWEN KEVIN C&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;&lt;DIV class=" dgrid-row dgrid-row-even ui-state-default"&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;2007&lt;/TD&gt;&lt;TD&gt;20070919&lt;/TD&gt;&lt;TD&gt;BOWEN KEVIN C&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;&lt;DIV class=" dgrid-row dgrid-row-odd ui-state-default"&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;2007&lt;/TD&gt;&lt;TD&gt;20071116&lt;/TD&gt;&lt;TD&gt;BOWEN KEVIN C&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;&lt;DIV class=" dgrid-row dgrid-row-even ui-state-default"&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;2007&lt;/TD&gt;&lt;TD&gt;20071121&lt;/TD&gt;&lt;TD&gt;BOWEN KEVIN C&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;&lt;DIV class=" dgrid-row dgrid-row-odd ui-state-default"&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;TD&gt;2007&lt;/TD&gt;&lt;TD&gt;20071126&lt;/TD&gt;&lt;TD&gt;BOWEN KEVIN C&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;&lt;DIV class=" dgrid-row dgrid-row-even ui-state-default"&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;7&lt;/TD&gt;&lt;TD&gt;2008&lt;/TD&gt;&lt;TD&gt;20081129&lt;/TD&gt;&lt;TD&gt;BOWEN KEVIN C&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As for expected results, this would be what I am looking for. The right column represents the number of previous trades in the preceding 3 years from the transaction date&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV class="dgrid-header dgrid-header-row ui-widget-header"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="dgrid-scroller"&gt;&lt;DIV class="dgrid-content ui-widget-content"&gt;&lt;DIV class=" dgrid-row dgrid-row-even ui-state-default"&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2004&lt;/TD&gt;&lt;TD&gt;20041108&lt;/TD&gt;&lt;TD&gt;BOWEN KEVIN C&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;&lt;DIV class=" dgrid-row dgrid-row-odd ui-state-default"&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;2007&lt;/TD&gt;&lt;TD&gt;20070618&lt;/TD&gt;&lt;TD&gt;BOWEN KEVIN C&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;&lt;DIV class=" dgrid-row dgrid-row-even ui-state-default"&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;2007&lt;/TD&gt;&lt;TD&gt;20070919&lt;/TD&gt;&lt;TD&gt;BOWEN KEVIN C&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;&lt;DIV class=" dgrid-row dgrid-row-odd ui-state-default"&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;2007&lt;/TD&gt;&lt;TD&gt;20071116&lt;/TD&gt;&lt;TD&gt;BOWEN KEVIN C&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;&lt;DIV class=" dgrid-row dgrid-row-even ui-state-default"&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;2007&lt;/TD&gt;&lt;TD&gt;20071121&lt;/TD&gt;&lt;TD&gt;BOWEN KEVIN C&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;&lt;DIV class=" dgrid-row dgrid-row-odd ui-state-default"&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;TD&gt;2007&lt;/TD&gt;&lt;TD&gt;20071126&lt;/TD&gt;&lt;TD&gt;BOWEN KEVIN C&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;&lt;DIV class=" dgrid-row dgrid-row-even ui-state-default"&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;7&lt;/TD&gt;&lt;TD&gt;2008&lt;/TD&gt;&lt;TD&gt;20081129&lt;/TD&gt;&lt;TD&gt;BOWEN KEVIN C&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;&lt;DIV class=" dgrid-row dgrid-row-odd ui-state-default dgrid-selected ui-state-active"&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&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;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Thu, 20 May 2021 20:53:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-observations-within-a-time-interval/m-p/742788#M232422</guid>
      <dc:creator>Mistletoad</dc:creator>
      <dc:date>2021-05-20T20:53:18Z</dc:date>
    </item>
    <item>
      <title>Re: Counting observations within a time interval</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-observations-within-a-time-interval/m-p/742829#M232442</link>
      <description>&lt;P&gt;You really want the count of the preceding 3 calendar years, not the preceding rolling 36 months?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by name notsorted;

  array tr_year{2000:2020} _temporary_;
  if first.name then call missing(of tr_year{*});
  tr_year{year}+1;
  n_prior3=sum(0,tr_year{year-3},tr_year{year-2},tr_year{year-1});
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 21 May 2021 06:02:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-observations-within-a-time-interval/m-p/742829#M232442</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-05-21T06:02:44Z</dc:date>
    </item>
    <item>
      <title>Re: Counting observations within a time interval</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-observations-within-a-time-interval/m-p/742841#M232449</link>
      <description>&lt;P&gt;See the annotated code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* use a data step with datalines to present data */
data have;
input @1 trans_year @7 trans_date :yymmdd8. @17 name $30.;
format trans_date yymmdd10.;
datalines;
2004  20041108  BOWEN KEVIN C  
2007  20070618  BOWEN KEVIN C  
2007  20070919  BOWEN KEVIN C  
2007  20071116  BOWEN KEVIN C  
2007  20071121  BOWEN KEVIN C  
2007  20071126  BOWEN KEVIN C  
2008  20081129  BOWEN KEVIN C  
;

/* determine the size of the later array */
proc sql noprint;
select min(trans_year), max(trans_year) into :minyear, :maxyear
from have;
quit;

data want;
set have;
by name trans_date; /* trans_date because we want to force correct chronological order */
/* make the array start 3 years before any data, so we don't have wrong indexes later */
array years{%eval(&amp;amp;minyear. - 3):&amp;amp;maxyear.} _temporary_;
if first.name then call missing(of years{*});
years{trans_year} + 1;
prior_trans = sum(
  0, /* 0 because we want to have zero even if all array members are missing */
  years{trans_year - 3},
  years{trans_year - 2},
  years{trans_year - 1}
);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note how data is presented in a piece of code, so everybody can recreate the dataset by simply copying the code and running it, without having to make any guesses about attributes or content.&lt;/P&gt;
&lt;P&gt;Creating data on the fly with a data step is a useful SAS skill on its own, I recommend that you acquire that skill for your own toolbox.&lt;/P&gt;</description>
      <pubDate>Fri, 21 May 2021 08:09:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-observations-within-a-time-interval/m-p/742841#M232449</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-05-21T08:09:41Z</dc:date>
    </item>
    <item>
      <title>Re: Counting observations within a time interval</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-observations-within-a-time-interval/m-p/742892#M232472</link>
      <description>&lt;P&gt;Here is another solution, using proc sql:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data have;&lt;BR /&gt;input @1 trans_year @7 trans_date :yymmdd8. &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/153152"&gt;@17&lt;/a&gt; name $30.;&lt;BR /&gt;format trans_date yymmdd10.;&lt;BR /&gt;datalines;&lt;BR /&gt;2004 20041108 BOWEN KEVIN C&lt;BR /&gt;2007 20070618 BOWEN KEVIN C&lt;BR /&gt;2007 20070919 BOWEN KEVIN C&lt;BR /&gt;2007 20071116 BOWEN KEVIN C&lt;BR /&gt;2007 20071121 BOWEN KEVIN C&lt;BR /&gt;2007 20071126 BOWEN KEVIN C&lt;BR /&gt;2008 20081129 BOWEN KEVIN C&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;create table mix as&lt;BR /&gt;select a.name&lt;BR /&gt;,a.trans_date&lt;BR /&gt;,a.trans_year&lt;BR /&gt;,b.trans_date as prev_trans_date&lt;BR /&gt;,case b.trans_date&lt;BR /&gt;when . then 0&lt;BR /&gt;else 1&lt;BR /&gt;end as cnt&lt;BR /&gt;from have a&lt;BR /&gt;left join have b&lt;BR /&gt;on (a.name = b.name and&lt;BR /&gt;(b.trans_year &amp;lt; a.trans_year&lt;BR /&gt;and a.trans_year - b.trans_year &amp;lt;= 3)&lt;BR /&gt;or b.trans_date = .)&lt;BR /&gt;order by a.name, a.trans_date, b.trans_date&lt;BR /&gt;;&lt;BR /&gt;create table want as&lt;BR /&gt;select name&lt;BR /&gt;,trans_date&lt;BR /&gt;,sum(cnt) as cnt_trans&lt;BR /&gt;from mix&lt;BR /&gt;group by name, trans_date&lt;BR /&gt;;&lt;BR /&gt;quit;&lt;/P&gt;</description>
      <pubDate>Fri, 21 May 2021 13:28:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-observations-within-a-time-interval/m-p/742892#M232472</guid>
      <dc:creator>KlausBücher</dc:creator>
      <dc:date>2021-05-21T13:28:18Z</dc:date>
    </item>
  </channel>
</rss>

