<?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 a certain period in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/counting-observations-within-a-certain-period/m-p/752236#M236928</link>
    <description>&lt;P&gt;After correcting a mistake, and adapting the variable names to your data, this works and creates the expected result:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Id year cf;
datalines; 
1 2000  . 
1 2001  . 
1 2002  100
1 2003  200
1 2004  300
1 2005  150
1 2006  900    
1 2007   .        
1 2008   .
;

%let start = 1800;
%let end = 2100;

data want;
array years {&amp;amp;start.:&amp;amp;end.} _temporary_;
do _n_ = &amp;amp;start. to &amp;amp;end.;
  years{_n_} = 0;
end;
do until (last.id);
  set have;
  by id;
  years{year} + (cf ne .);
end;
count_cf = 0;
do until (last.id);
  set have;
  by id;
  count_cf = 0;
  do fyear = year - 5 to year - 1;
    count_cf + years{fyear};
  end;
  output;
end;
drop fyear;
run;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;Id	year	cf	count_cf
1	2000	.	0
1	2001	.	0
1	2002	100	0
1	2003	200	1
1	2004	300	2
1	2005	150	3
1	2006	900	4
1	2007	.	5
1	2008	.	4&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/270067"&gt;@JKCho&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you again.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I realized this programming code is not for what I want.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I just want to have is... if my raw data is...&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Id year cf&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1 2000&amp;nbsp; .&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1 2001&amp;nbsp; .&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1&amp;nbsp;2002&amp;nbsp; 100&lt;/P&gt;
&lt;P&gt;1&amp;nbsp;2003&amp;nbsp; 200&lt;/P&gt;
&lt;P&gt;1&amp;nbsp;2004&amp;nbsp; 300&lt;/P&gt;
&lt;P&gt;1&amp;nbsp;2005&amp;nbsp; 150&lt;/P&gt;
&lt;P&gt;1 2006&amp;nbsp; 900&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1 2007&amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1 2008&amp;nbsp; &amp;nbsp;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;then....&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Id year cf&amp;nbsp; &amp;nbsp; &amp;nbsp;n_cf&lt;/P&gt;
&lt;P&gt;1 2000&amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;
&lt;P&gt;1 2001&amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;
&lt;P&gt;1&amp;nbsp;2002&amp;nbsp; 100&amp;nbsp; &amp;nbsp;0&lt;/P&gt;
&lt;P&gt;1&amp;nbsp;2003&amp;nbsp; 200&amp;nbsp; &amp;nbsp;1&lt;/P&gt;
&lt;P&gt;1&amp;nbsp;2004&amp;nbsp; 300&amp;nbsp; &amp;nbsp;2&lt;/P&gt;
&lt;P&gt;1 2005&amp;nbsp; 150&amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1 2006&amp;nbsp; 900&amp;nbsp; &amp;nbsp;4&lt;/P&gt;
&lt;P&gt;1 2007&amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 5&lt;/P&gt;
&lt;P&gt;1 2008&amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;n_cf is the number of non-missing cf variables between t-1 year and t-5 year.&lt;/P&gt;
&lt;P&gt;I thought&amp;nbsp;&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;where a.fyear between b.fyear-1 and b.fyear-5;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;the above part can do what I want but it actually did is first count "cf" of all observations In my dataset and then group by gvkey. I instead want to count cf observations within t-1 and t-5 by firm(gvkey).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Does any have thoughts on that? Thank you so much!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;</description>
    <pubDate>Tue, 06 Jul 2021 09:19:00 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2021-07-06T09:19:00Z</dc:date>
    <item>
      <title>counting observations within a certain period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/counting-observations-within-a-certain-period/m-p/751016#M236330</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table comp2_1 as
 select *,(select count(cf) from comp2 where fyear between a.fyear-1 and a.fyear-5 and gvkey=a.gvkey) as count_cf
  from comp2 as a;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I first used the code above but as you can easily expect, it takes so long...&lt;/P&gt;&lt;P&gt;What I want is to obtain the number(count_cf) of observations(cf) between t-5 year and t-1 year for each firm(gvkey).&lt;/P&gt;&lt;P&gt;Since some "cf" observations are missing, I only want to use non-missing data, so I need to sort them out.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It is very sure that I am very close to hit the answer by searching for in this community but I have not reached that yet.&lt;/P&gt;&lt;P&gt;Please share your thoughts!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Jun 2021 17:04:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/counting-observations-within-a-certain-period/m-p/751016#M236330</guid>
      <dc:creator>JKCho</dc:creator>
      <dc:date>2021-06-29T17:04:59Z</dc:date>
    </item>
    <item>
      <title>Re: counting observations within a certain period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/counting-observations-within-a-certain-period/m-p/751045#M236350</link>
      <description>&lt;P&gt;Run a double DO loop, in the first loop populate an array (indexed by year). In the second loop, calculate the value.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let start = 1800;
%let end = 2100;

data want;
array years {&amp;amp;start.:&amp;amp;end.} _temporary_;
do _n_ = &amp;amp;start. to &amp;amp;end.;
  years{_n_} = 0;
end;
do until (last.gvkey);
  set have;
  by gvkey;
  years{fyear} + (cf ne .);
end;
count_cf = 0;
do until (last.gvkey);
  set have;
  by gvkey;
  count_cf = sum(of years{fyear- 5:fyear - 1});
  output;
end;
run;
  &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Untested, posted from my tablet.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Jun 2021 19:10:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/counting-observations-within-a-certain-period/m-p/751045#M236350</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-06-29T19:10:02Z</dc:date>
    </item>
    <item>
      <title>Re: counting observations within a certain period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/counting-observations-within-a-certain-period/m-p/751069#M236366</link>
      <description>A self join may even be slightly faster than a subquery though....have you tried that instead?</description>
      <pubDate>Tue, 29 Jun 2021 21:11:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/counting-observations-within-a-certain-period/m-p/751069#M236366</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-06-29T21:11:31Z</dc:date>
    </item>
    <item>
      <title>Re: counting observations within a certain period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/counting-observations-within-a-certain-period/m-p/751076#M236370</link>
      <description>&lt;P&gt;Nope. I am trying now. I know the code down there does not work. Can you suggest some? I am not sure where I need to create n(cf) in this self join.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql ;
  create table step5 as 
    select a.*, b.n(cf)
    from step4 a left join step4 b
    on a.gvkey = b.gvkey
    where a.fyear between a.fyear-1 and a.fyear-5;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Jun 2021 21:59:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/counting-observations-within-a-certain-period/m-p/751076#M236370</guid>
      <dc:creator>JKCho</dc:creator>
      <dc:date>2021-06-29T21:59:01Z</dc:date>
    </item>
    <item>
      <title>Re: counting observations within a certain period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/counting-observations-within-a-certain-period/m-p/751079#M236371</link>
      <description>Functions go around the variable reference, n(b.cf)&lt;BR /&gt;&lt;BR /&gt;proc sql ;&lt;BR /&gt;  create table step5 as &lt;BR /&gt;    select a.*, n(b.cf)&lt;BR /&gt;    from step4 a left join step4 b&lt;BR /&gt;    on a.gvkey = b.gvkey&lt;BR /&gt;    where a.fyear between a.fyear-1 and a.fyear-5;&lt;BR /&gt;quit;</description>
      <pubDate>Tue, 29 Jun 2021 22:00:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/counting-observations-within-a-certain-period/m-p/751079#M236371</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-06-29T22:00:45Z</dc:date>
    </item>
    <item>
      <title>Re: counting observations within a certain period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/counting-observations-within-a-certain-period/m-p/751081#M236373</link>
      <description>You also need to change your reference on the between to b from a. ie a.fyear between b.fyear-1 and b.fyear-5. &lt;BR /&gt;Check that the a/b references are what you want.</description>
      <pubDate>Tue, 29 Jun 2021 22:06:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/counting-observations-within-a-certain-period/m-p/751081#M236373</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-06-29T22:06:14Z</dc:date>
    </item>
    <item>
      <title>Re: counting observations within a certain period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/counting-observations-within-a-certain-period/m-p/751084#M236375</link>
      <description>&lt;P&gt;Thank you Reeze!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql ;
  create table step5 as 
    select a.*, n(b.cf) as n_cf
	from step4 a left join step4 b
	on a.gvkey = b.gvkey
    where a.fyear between b.fyear-1 and b.fyear-5;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I now use this code but there is an issue. the value of n_cf is the whole number of observations of cf, so it is like 2,000,000. ( I have a similar number of observations)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It seems that the number of n_cf should be bounded by&amp;nbsp;&lt;CODE class=" language-sas"&gt;on a.gvkey = b.gvkey where a.fyear between b.fyear-1 and b.fyear-5;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;But this is not the case.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any ideas?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Jun 2021 22:20:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/counting-observations-within-a-certain-period/m-p/751084#M236375</guid>
      <dc:creator>JKCho</dc:creator>
      <dc:date>2021-06-29T22:20:25Z</dc:date>
    </item>
    <item>
      <title>Re: counting observations within a certain period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/counting-observations-within-a-certain-period/m-p/751105#M236392</link>
      <description>If you want the count by firm you would have to do group by firm.</description>
      <pubDate>Wed, 30 Jun 2021 02:33:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/counting-observations-within-a-certain-period/m-p/751105#M236392</guid>
      <dc:creator>tarheel13</dc:creator>
      <dc:date>2021-06-30T02:33:54Z</dc:date>
    </item>
    <item>
      <title>Re: counting observations within a certain period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/counting-observations-within-a-certain-period/m-p/751260#M236463</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql ;
  create table step5 as 
    select a.*, n(b.cf) as n_cf
	from step4 a left join step4 b
	on a.gvkey = b.gvkey
    where a.fyear between b.fyear-1 and b.fyear-5
group by gvkey;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/270067"&gt;@JKCho&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you Reeze!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql ;
  create table step5 as 
    select a.*, n(b.cf) as n_cf
	from step4 a left join step4 b
	on a.gvkey = b.gvkey
    where a.fyear between b.fyear-1 and b.fyear-5;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I now use this code but there is an issue. the value of n_cf is the whole number of observations of cf, so it is like 2,000,000. ( I have a similar number of observations)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It seems that the number of n_cf should be bounded by&amp;nbsp;&lt;CODE class=" language-sas"&gt;on a.gvkey = b.gvkey where a.fyear between b.fyear-1 and b.fyear-5;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;But this is not the case.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any ideas?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 30 Jun 2021 14:12:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/counting-observations-within-a-certain-period/m-p/751260#M236463</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-06-30T14:12:44Z</dc:date>
    </item>
    <item>
      <title>Re: counting observations within a certain period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/counting-observations-within-a-certain-period/m-p/751346#M236509</link>
      <description>&lt;P&gt;Thank you again.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I realized this programming code is not for what I want.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I just want to have is... if my raw data is...&amp;nbsp;&lt;/P&gt;&lt;P&gt;Id year cf&amp;nbsp;&lt;/P&gt;&lt;P&gt;1 2000&amp;nbsp; .&amp;nbsp;&lt;/P&gt;&lt;P&gt;1 2001&amp;nbsp; .&amp;nbsp;&lt;/P&gt;&lt;P&gt;1&amp;nbsp;2002&amp;nbsp; 100&lt;/P&gt;&lt;P&gt;1&amp;nbsp;2003&amp;nbsp; 200&lt;/P&gt;&lt;P&gt;1&amp;nbsp;2004&amp;nbsp; 300&lt;/P&gt;&lt;P&gt;1&amp;nbsp;2005&amp;nbsp; 150&lt;/P&gt;&lt;P&gt;1 2006&amp;nbsp; 900&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1 2007&amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1 2008&amp;nbsp; &amp;nbsp;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;then....&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Id year cf&amp;nbsp; &amp;nbsp; &amp;nbsp;n_cf&lt;/P&gt;&lt;P&gt;1 2000&amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;&lt;P&gt;1 2001&amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;&lt;P&gt;1&amp;nbsp;2002&amp;nbsp; 100&amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;1&amp;nbsp;2003&amp;nbsp; 200&amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;1&amp;nbsp;2004&amp;nbsp; 300&amp;nbsp; &amp;nbsp;2&lt;/P&gt;&lt;P&gt;1 2005&amp;nbsp; 150&amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1 2006&amp;nbsp; 900&amp;nbsp; &amp;nbsp;4&lt;/P&gt;&lt;P&gt;1 2007&amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 5&lt;/P&gt;&lt;P&gt;1 2008&amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;n_cf is the number of non-missing cf variables between t-1 year and t-5 year.&lt;/P&gt;&lt;P&gt;I thought&amp;nbsp;&lt;/P&gt;&lt;PRE class="language-sas"&gt;&lt;CODE&gt;where a.fyear between b.fyear-1 and b.fyear-5;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;the above part can do what I want but it actually did is first count "cf" of all observations In my dataset and then group by gvkey. I instead want to count cf observations within t-1 and t-5 by firm(gvkey).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Does any have thoughts on that? Thank you so much!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 30 Jun 2021 19:07:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/counting-observations-within-a-certain-period/m-p/751346#M236509</guid>
      <dc:creator>JKCho</dc:creator>
      <dc:date>2021-06-30T19:07:39Z</dc:date>
    </item>
    <item>
      <title>Re: counting observations within a certain period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/counting-observations-within-a-certain-period/m-p/751353#M236512</link>
      <description>Note the GROUP BY in my last response.</description>
      <pubDate>Wed, 30 Jun 2021 19:37:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/counting-observations-within-a-certain-period/m-p/751353#M236512</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-06-30T19:37:09Z</dc:date>
    </item>
    <item>
      <title>Re: counting observations within a certain period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/counting-observations-within-a-certain-period/m-p/751374#M236524</link>
      <description>well. I know you said that. What I said is... what you said also does not work. Seems no way and I have to approach in a different way. Thx!</description>
      <pubDate>Wed, 30 Jun 2021 23:30:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/counting-observations-within-a-certain-period/m-p/751374#M236524</guid>
      <dc:creator>JKCho</dc:creator>
      <dc:date>2021-06-30T23:30:54Z</dc:date>
    </item>
    <item>
      <title>Re: counting observations within a certain period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/counting-observations-within-a-certain-period/m-p/751377#M236527</link>
      <description>&lt;P&gt;A query like this will get you the result you want, assuming you've already got the required HAVE input dataset&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create table want as
  select ID
        ,year
        ,cf
        ,count(cf)
  from have
  group by id
          ,year
          ,cf
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 01 Jul 2021 00:27:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/counting-observations-within-a-certain-period/m-p/751377#M236527</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2021-07-01T00:27:00Z</dc:date>
    </item>
    <item>
      <title>Re: counting observations within a certain period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/counting-observations-within-a-certain-period/m-p/752236#M236928</link>
      <description>&lt;P&gt;After correcting a mistake, and adapting the variable names to your data, this works and creates the expected result:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Id year cf;
datalines; 
1 2000  . 
1 2001  . 
1 2002  100
1 2003  200
1 2004  300
1 2005  150
1 2006  900    
1 2007   .        
1 2008   .
;

%let start = 1800;
%let end = 2100;

data want;
array years {&amp;amp;start.:&amp;amp;end.} _temporary_;
do _n_ = &amp;amp;start. to &amp;amp;end.;
  years{_n_} = 0;
end;
do until (last.id);
  set have;
  by id;
  years{year} + (cf ne .);
end;
count_cf = 0;
do until (last.id);
  set have;
  by id;
  count_cf = 0;
  do fyear = year - 5 to year - 1;
    count_cf + years{fyear};
  end;
  output;
end;
drop fyear;
run;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;Id	year	cf	count_cf
1	2000	.	0
1	2001	.	0
1	2002	100	0
1	2003	200	1
1	2004	300	2
1	2005	150	3
1	2006	900	4
1	2007	.	5
1	2008	.	4&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/270067"&gt;@JKCho&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you again.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I realized this programming code is not for what I want.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I just want to have is... if my raw data is...&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Id year cf&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1 2000&amp;nbsp; .&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1 2001&amp;nbsp; .&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1&amp;nbsp;2002&amp;nbsp; 100&lt;/P&gt;
&lt;P&gt;1&amp;nbsp;2003&amp;nbsp; 200&lt;/P&gt;
&lt;P&gt;1&amp;nbsp;2004&amp;nbsp; 300&lt;/P&gt;
&lt;P&gt;1&amp;nbsp;2005&amp;nbsp; 150&lt;/P&gt;
&lt;P&gt;1 2006&amp;nbsp; 900&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1 2007&amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1 2008&amp;nbsp; &amp;nbsp;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;then....&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Id year cf&amp;nbsp; &amp;nbsp; &amp;nbsp;n_cf&lt;/P&gt;
&lt;P&gt;1 2000&amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;
&lt;P&gt;1 2001&amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;
&lt;P&gt;1&amp;nbsp;2002&amp;nbsp; 100&amp;nbsp; &amp;nbsp;0&lt;/P&gt;
&lt;P&gt;1&amp;nbsp;2003&amp;nbsp; 200&amp;nbsp; &amp;nbsp;1&lt;/P&gt;
&lt;P&gt;1&amp;nbsp;2004&amp;nbsp; 300&amp;nbsp; &amp;nbsp;2&lt;/P&gt;
&lt;P&gt;1 2005&amp;nbsp; 150&amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1 2006&amp;nbsp; 900&amp;nbsp; &amp;nbsp;4&lt;/P&gt;
&lt;P&gt;1 2007&amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 5&lt;/P&gt;
&lt;P&gt;1 2008&amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;n_cf is the number of non-missing cf variables between t-1 year and t-5 year.&lt;/P&gt;
&lt;P&gt;I thought&amp;nbsp;&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;where a.fyear between b.fyear-1 and b.fyear-5;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;the above part can do what I want but it actually did is first count "cf" of all observations In my dataset and then group by gvkey. I instead want to count cf observations within t-1 and t-5 by firm(gvkey).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Does any have thoughts on that? Thank you so much!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;</description>
      <pubDate>Tue, 06 Jul 2021 09:19:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/counting-observations-within-a-certain-period/m-p/752236#M236928</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-07-06T09:19:00Z</dc:date>
    </item>
  </channel>
</rss>

