<?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: How to count the number of observations between two events and create a variable conditional on in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/How-to-count-the-number-of-observations-between-two-events-and/m-p/585467#M14335</link>
    <description>&lt;P&gt;Okay Thank you&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/287084"&gt;@user95&lt;/a&gt;&amp;nbsp;. A good representative sample always helps and also avoid any assumptions plus going back and forth. The fix is very minor though. Try-&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
do until(last.id);
 set have;
 by id Event_occurrence notsorted;
 array t(999999) _temporary_;
 if first.Event_occurrence then _n=0;
 if Event_occurrence then do; _f=1;y=year;end;
 if _f then  _n+(Event_occurrence=0);
 if last.Event_occurrence and Event_occurrence=0 and _f then do;
 _i+1;
 t(_i)=_n;
 end;
end;
call missing(of _:);
if not missing(y) then _y=year ne y;
do until(last.id);
 set have;
 by id Event_occurrence notsorted;
 want_var=Event_occurrence;
 if Event_occurrence then _f=1;
 if first.Event_occurrence then _n=0;
 if first.Event_occurrence and Event_occurrence=0 and _f then _i+1;
 if _f then do; _n+(Event_occurrence=0);want_var=1;end;
 if Event_occurrence=0 and _f then do;
  if  t(_i)&amp;lt;3 then want_var=1;
  else if t(_i)=3 then want_var=0;
  else if t(_i)&amp;gt;3 then want_var=_n&amp;lt;=t(_i)-3;
 end;
 if _y then want_var=ifn(year&amp;gt;=y,1,want_var);
 output;
end;
call missing(of t(*),of _:);
drop y _:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sun, 01 Sep 2019 14:37:55 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2019-09-01T14:37:55Z</dc:date>
    <item>
      <title>How to count the number of observations between two events and create a variable conditional on that</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-count-the-number-of-observations-between-two-events-and/m-p/584704#M14223</link>
      <description>&lt;P&gt;Hello everyone,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am a relatively new SAS user and working on a project for which I need to do the following:&lt;/P&gt;&lt;P&gt;For each company ID, I have a dummy variable "event occurrence" that is either 1 or 0, for each year from 1992 to 2017.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to be able to count the number of observations between every two consecutive events ( every time event occurrence =1)&lt;/P&gt;&lt;P&gt;Then, if the difference is less than three years then the variable (want_var) will equal 1 for those years&lt;/P&gt;&lt;P&gt;if the difference=3 then the variable (want_var) will equal 0 for those years.&lt;/P&gt;&lt;P&gt;if the difference is &amp;gt;3 years, then, the variable (want_var) will equal 1 for all those years up to three years before the next event.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I am looking to achieve is the following&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;year&lt;/TD&gt;&lt;TD&gt;Event occurrence&lt;/TD&gt;&lt;TD&gt;want_var&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1004&lt;/TD&gt;&lt;TD&gt;1992&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1004&lt;/TD&gt;&lt;TD&gt;1993&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;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t;0&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1004&lt;/TD&gt;&lt;TD&gt;2013&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1004&lt;/TD&gt;&lt;TD&gt;2014&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1004&lt;/TD&gt;&lt;TD&gt;2015&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1004&lt;/TD&gt;&lt;TD&gt;2016&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&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;P&gt;Any advice is welcome!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Aug 2019 18:13:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-count-the-number-of-observations-between-two-events-and/m-p/584704#M14223</guid>
      <dc:creator>user95</dc:creator>
      <dc:date>2019-08-28T18:13:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to count the number of observations between two events and create a variable conditional on</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-count-the-number-of-observations-between-two-events-and/m-p/584711#M14224</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;if the difference is &amp;gt;3 years, then, the variable (want_var) will equal 1 for all those years up to three years before the next event.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is there an example of this in the data? If so, which year(s) show what you mean by this?&lt;/P&gt;</description>
      <pubDate>Wed, 28 Aug 2019 18:20:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-count-the-number-of-observations-between-two-events-and/m-p/584711#M14224</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-08-28T18:20:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to count the number of observations between two events and create a variable conditional on</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-count-the-number-of-observations-between-two-events-and/m-p/584738#M14228</link>
      <description>&lt;P&gt;Greetings,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Overall,&amp;nbsp;looking at the table I attacked,&amp;nbsp;the logic is as follow:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Before the first event for each firm, (before 1995) the variable want_var would equal 0&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The first event is in 1995, the next event is in 1996, so since the difference between those two events is only one year, the variable want_var will equal 1 for both those years (1995, and 1996)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The second event and the third event are three years apart ( 3 non event years apart) (1996 to 2000), so the variable want_var will equal 1 for the event year 1996 and 2000 and zero during the those three non event years.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Once again, the next event is only one year apart (in 2001) so want_var=1,&amp;nbsp;&lt;/P&gt;&lt;P&gt;Between the 2001 event and 2003 event there is only one non event year, so want_var=1 for all those years. (2001,2002,2003)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Between the 2003 event year and 2014 event year , there are 10 non event years,&amp;nbsp;&lt;/P&gt;&lt;P&gt;so what I want to achieve in the case of difference&amp;gt;3 of non event years years is having the new variable want_var equal 1 from 2004 until 3 years before the next event which is 2014, so it would equal 1 on 2003 and 2014 ,&amp;nbsp; from 2004 until 2010, and then equal 0 in 2011,2012,2013.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then lastly, if the last event year (2014) is not the last year recorded for that firm (2016), want_var =1 for all consecutive years until the end of data of that specific firm.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i hope what I want to achieve is more clear&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Aug 2019 19:02:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-count-the-number-of-observations-between-two-events-and/m-p/584738#M14228</guid>
      <dc:creator>user95</dc:creator>
      <dc:date>2019-08-28T19:02:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to count the number of observations between two events and create a variable conditional on</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-count-the-number-of-observations-between-two-events-and/m-p/584909#M14237</link>
      <description>&lt;P&gt;This is a good explanation of your problem. Thank you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I spent about 15 minutes on trying to solve this last night, and didn't succeed. I suspect this might be a good example where the hash objects in SAS data steps might help. So I hereby summon our hash experts&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/21262"&gt;@hashman&lt;/a&gt;&amp;nbsp;to see if they can come up with a solution, if they have the time and interest.&lt;/P&gt;</description>
      <pubDate>Thu, 29 Aug 2019 12:36:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-count-the-number-of-observations-between-two-events-and/m-p/584909#M14237</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-08-29T12:36:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to count the number of observations between two events and create a variable conditional on</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-count-the-number-of-observations-between-two-events-and/m-p/584948#M14247</link>
      <description>&lt;P&gt;Thank you Sir&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp; for the mention, however I am just&amp;nbsp; Expert&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/21262"&gt;@hashman&lt;/a&gt;&amp;nbsp;&lt;U&gt; wannabe .&amp;nbsp;&lt;/U&gt; Hopefully I become like him one day sooner than later. He knows I am striving hard for it. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/287084"&gt;@user95&lt;/a&gt;&amp;nbsp; &amp;nbsp;Since your year is continuous, the requirement appears pretty straight forward(&lt;EM&gt;unless I missed something very important that I am known for in lacking ATD aka Attention to detail&lt;/EM&gt;) with a simple Double DOW i.e 1.&amp;nbsp; to Flag and Count 2. To look up. So here you go-&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
infile cards expandtabs truncover;
input ID	year	Event_occurrence;*	want_var;
cards;
1004	1992	0	0
1004	1993	0	0
1004	1994	0	0
1004	1995	1	1
1004	1996	1	1
1004	1997	0	0
1004	1998	0	0
1004	1999	0	0
1004	2000	1	1
1004	2001	1	1
1004	2002	0	1
1004	2003	1	1
1004	2004	0	1
1004	2005	0	1
1004	2006	0	1
1004	2007	0	1
1004	2008	0	1
1004	2009	0	1
1004	2010	0	1
1004	2011	0	0
1004	2012	0	0
1004	2013	0	0
1004	2014	1	1
1004	2015	0	1
1004	2016	0	1
;
/*RULES*/
/*Then, if the difference is less than three years then the variable (want_var) will equal 1 for those years*/
/**/
/*if the difference=3 then the variable (want_var) will equal 0 for those years.*/
/**/
/*if the difference is &amp;gt;3 years, then, the variable (want_var) will equal 1 for all those years up to three years before the next event.*/
data want; 
 do _n=1 by 1 until(last.Event_occurrence);
  set have;
  by id Event_occurrence notsorted;
  retain _f want_var;
  if first.id then do;_f=0;want_var=0;end;
  if Event_occurrence then _f=1;
 end;
 do _n_=1 by 1 until(last.Event_occurrence);
  set have;
  by id Event_occurrence notsorted;
  if _f then want_var=1;
  if Event_occurrence=0 and _f then do;
  if  _n&amp;lt;3 then want_var=1;
  else if _n=3 then want_var=0;
  else if _n&amp;gt;3 then want_var=_n_&amp;lt;=_n-3;
  end;
 output;
end;
drop _:;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;HTH &amp;amp; Best Regards!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Aug 2019 15:04:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-count-the-number-of-observations-between-two-events-and/m-p/584948#M14247</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-08-29T15:04:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to count the number of observations between two events and create a variable conditional on</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-count-the-number-of-observations-between-two-events-and/m-p/584950#M14248</link>
      <description>&lt;P&gt;Nice solution,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;. That method did not occur to me.&lt;/P&gt;</description>
      <pubDate>Thu, 29 Aug 2019 14:49:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-count-the-number-of-observations-between-two-events-and/m-p/584950#M14248</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-08-29T14:49:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to count the number of observations between two events and create a variable conditional on</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-count-the-number-of-observations-between-two-events-and/m-p/585036#M14257</link>
      <description>&lt;P&gt;A wit into performance by not having another &lt;EM&gt;&lt;STRONG&gt;"by"&amp;nbsp;&lt;/STRONG&gt;&amp;nbsp;&lt;/EM&gt;in the 2nd pass as to read the records from cache&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want; 
 do _n=1 by 1 until(last.Event_occurrence);
  set have;
  by id Event_occurrence notsorted;
  retain _f want_var;
  if first.id then do;_f=0;want_var=0;end;
  if Event_occurrence then _f=1;
 end;
 do _n_=1 to _n;
  set have;
  if _f then want_var=1;
  if Event_occurrence=0 and _f then do;
  if  _n&amp;lt;3 then want_var=1;
  else if _n=3 then want_var=0;
  else if _n&amp;gt;3 then want_var=_n_&amp;lt;=_n-3;
  end;
 output;
end;
drop _:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 29 Aug 2019 18:36:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-count-the-number-of-observations-between-two-events-and/m-p/585036#M14257</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-08-29T18:36:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to count the number of observations between two events and create a variable conditional on</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-count-the-number-of-observations-between-two-events-and/m-p/585184#M14271</link>
      <description>&lt;P&gt;Greetings,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for taking the time to answer my question,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There is one detail not incorporated into your code and it is as I mentioned before as follow :&lt;/P&gt;&lt;P&gt;"&lt;SPAN&gt;Then lastly, if the last event year (2014) is not the last year recorded for that firm (2016), want_var =1 for all consecutive years until the end of data of that specific firm."&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;But following your code, it gives me want_var=1 for all remaining year except the last 3 years of the data for that firm.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;if the event_occurrence=1 is in 2014 but the last year recorded for that firm is 2020, then want_var for all consecutive years should be=1 until 2020, but in your code want_var=1 from 2014 until 3 years before the end of the data in 2020.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Is there any way to fix it?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Thank you once again!&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 30 Aug 2019 12:39:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-count-the-number-of-observations-between-two-events-and/m-p/585184#M14271</guid>
      <dc:creator>user95</dc:creator>
      <dc:date>2019-08-30T12:39:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to count the number of observations between two events and create a variable conditional on</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-count-the-number-of-observations-between-two-events-and/m-p/585274#M14289</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/287084"&gt;@user95&lt;/a&gt;&amp;nbsp; &amp;nbsp;In your expected output (you wrote-"what i am looking to achieve") doesn't seem to comply with the below condition right?&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;"Then lastly, if the last event year (2014) is not the last year recorded for that firm (2016), want_var =1 for all consecutive years until the end of data of that specific firm."&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;from - until? I am not quite understanding FROM&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 30 Aug 2019 16:29:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-count-the-number-of-observations-between-two-events-and/m-p/585274#M14289</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-08-30T16:29:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to count the number of observations between two events and create a variable conditional on</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-count-the-number-of-observations-between-two-events-and/m-p/585290#M14296</link>
      <description>&lt;P&gt;Greetings,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yes, I want to make the want_var=1 from the last event occurrence until the end of the data for that firm. So in my example it would be from 2014 until 2016&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can I achieve that?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you once more&lt;/P&gt;</description>
      <pubDate>Fri, 30 Aug 2019 16:46:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-count-the-number-of-observations-between-two-events-and/m-p/585290#M14296</guid>
      <dc:creator>user95</dc:creator>
      <dc:date>2019-08-30T16:46:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to count the number of observations between two events and create a variable conditional on</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-count-the-number-of-observations-between-two-events-and/m-p/585312#M14307</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/287084"&gt;@user95&lt;/a&gt;&amp;nbsp; &amp;nbsp;To Make it syntactically less verbose and easier to follow logic, I am doing the final in another step(proc sql). Once you deem we are correct, we shall jump to one step solution if you want.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Test and let me know&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards expandtabs truncover;
input ID	year	Event_occurrence;*	want_var;
cards;
1004	1992	0	0
1004	1993	0	0
1004	1994	0	0
1004	1995	1	1
1004	1996	1	1
1004	1997	0	0
1004	1998	0	0
1004	1999	0	0
1004	2000	1	1
1004	2001	1	1
1004	2002	0	1
1004	2003	1	1
1004	2004	0	1
1004	2005	0	1
1004	2006	0	1
1004	2007	0	1
1004	2008	0	1
1004	2009	0	1
1004	2010	0	1
1004	2011	0	0
1004	2012	0	0
1004	2013	0	0
1004	2014	1	1
1004	2015	0	1
1004	2016	0	1
;

data temp; 
 do _n=1 by 1 until(last.Event_occurrence);
  set have;
  by id Event_occurrence notsorted;
  retain _f want_var;
  if first.id then do;_f=0;want_var=0;end;
  if Event_occurrence then _f=1;
 end;
 do _n_=1 to _n;
  set have;
  if _f then want_var=1;
  if Event_occurrence=0 and _f then do;
  if  _n&amp;lt;3 then want_var=1;
  else if _n=3 then want_var=0;
  else if _n&amp;gt;3 then want_var=_n_&amp;lt;=_n-3;
  end;
 output;
end;
drop _:;
run;

proc sql;
create table want(drop=t:) as
select ID,year,Event_occurrence,max((Event_occurrence=1)*year) as t, max(year) as t1,
ifn(calculated t ne calculated t1 and year&amp;gt;=calculated t,1,want_var) as want_var
from temp
group by id
order by id, year;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 30 Aug 2019 18:05:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-count-the-number-of-observations-between-two-events-and/m-p/585312#M14307</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-08-30T18:05:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to count the number of observations between two events and create a variable conditional on</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-count-the-number-of-observations-between-two-events-and/m-p/585391#M14318</link>
      <description>&lt;P&gt;Greetings,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yes, the last step works as intended, as you mentioned a one step solution would be better, so that I dont end up with a separate dataset that I would need to merge back.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How would it be possible to do it all in one step?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;</description>
      <pubDate>Sat, 31 Aug 2019 05:23:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-count-the-number-of-observations-between-two-events-and/m-p/585391#M14318</guid>
      <dc:creator>user95</dc:creator>
      <dc:date>2019-08-31T05:23:49Z</dc:date>
    </item>
    <item>
      <title>Re: How to count the number of observations between two events and create a variable conditional on</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-count-the-number-of-observations-between-two-events-and/m-p/585410#M14321</link>
      <description>&lt;P&gt;Yes sure, just a slight adjustment to not have nested DOW, instead park the count in an array on the first and then look up for the reason max of the year and year of last event_occurrence=1 is fetched at group ID level&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
infile cards expandtabs truncover;
input ID	year	Event_occurrence;*	want_var;
cards;
1004	1992	0	0
1004	1993	0	0
1004	1994	0	0
1004	1995	1	1
1004	1996	1	1
1004	1997	0	0
1004	1998	0	0
1004	1999	0	0
1004	2000	1	1
1004	2001	1	1
1004	2002	0	1
1004	2003	1	1
1004	2004	0	1
1004	2005	0	1
1004	2006	0	1
1004	2007	0	1
1004	2008	0	1
1004	2009	0	1
1004	2010	0	1
1004	2011	0	0
1004	2012	0	0
1004	2013	0	0
1004	2014	1	1
1004	2015	0	1
1004	2016	0	1
;

data want;
if 0 then set have;
want_var=0;
do until(last.id);
 set have;
 by id Event_occurrence notsorted;
 array t(999999) _temporary_;
 if first.Event_occurrence then _n=0;
 if Event_occurrence then do; _f=1;y=year;end;
 if _f then  _n+(Event_occurrence=0);
 if last.Event_occurrence and Event_occurrence=0 and _f then do;
 _i+1;
 t(_i)=_n;
 end;
end;
call missing(of _:);
_y=year ne y;
do until(last.id);
 set have;
 by id Event_occurrence notsorted;
 if Event_occurrence then _f=1;
 if first.Event_occurrence then _n=0;
 if first.Event_occurrence and Event_occurrence=0 and _f then _i+1;
 if _f then do; _n+(Event_occurrence=0);want_var=1;end;
 if Event_occurrence=0 and _f then do;
  if  t(_i)&amp;lt;3 then want_var=1;
  else if t(_i)=3 then want_var=0;
  else if t(_i)&amp;gt;3 then want_var=_n&amp;lt;=t(_i)-3;
 end;
 if _y then want_var=ifn(year&amp;gt;=y,1,want_var);
 output;
end;
call missing(of t(*),of _:);
drop y _:;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 31 Aug 2019 13:55:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-count-the-number-of-observations-between-two-events-and/m-p/585410#M14321</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-08-31T13:55:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to count the number of observations between two events and create a variable conditional on</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-count-the-number-of-observations-between-two-events-and/m-p/585458#M14331</link>
      <description>&lt;P&gt;Greetings&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your reply,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Looking more carefully at my data , I realized that sometimes I had&amp;nbsp;&lt;STRONG&gt;&amp;nbsp;missing values&amp;nbsp;&lt;/STRONG&gt;for the event_occurrence variable throughout the years.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Meaning that for the some firm A from 1992 until 2016, there might be a few years of missing values for the event_occurence variable, which your code doesnt account for, and sometimes arbitrarily results in either 0 or 1 for the want_var.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can I make sure that missing values for event_occurence result in missing values in the want_var variable?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I thought about deleting the missing values before running your code , but somehow it doesnt seem like a good idea.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How do you think I should handle it?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Sun, 01 Sep 2019 11:10:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-count-the-number-of-observations-between-two-events-and/m-p/585458#M14331</guid>
      <dc:creator>user95</dc:creator>
      <dc:date>2019-09-01T11:10:49Z</dc:date>
    </item>
    <item>
      <title>Re: How to count the number of observations between two events and create a variable conditional on</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-count-the-number-of-observations-between-two-events-and/m-p/585459#M14332</link>
      <description>&lt;P&gt;can you plz post a more representative sample of your HAVE and WANT?&lt;/P&gt;</description>
      <pubDate>Sun, 01 Sep 2019 11:59:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-count-the-number-of-observations-between-two-events-and/m-p/585459#M14332</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-09-01T11:59:56Z</dc:date>
    </item>
    <item>
      <title>Re: How to count the number of observations between two events and create a variable conditional on</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-count-the-number-of-observations-between-two-events-and/m-p/585466#M14334</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Yes, here it is,&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;so far the code worked well but just didnt take under account missing values like the following&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Year&lt;/TD&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;occurrence_event&lt;/TD&gt;&lt;TD&gt;Have_with current code&lt;/TD&gt;&lt;TD&gt;Var_Want&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1992&lt;/TD&gt;&lt;TD&gt;14264&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1993&lt;/TD&gt;&lt;TD&gt;14264&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1994&lt;/TD&gt;&lt;TD&gt;14264&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1995&lt;/TD&gt;&lt;TD&gt;14264&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1996&lt;/TD&gt;&lt;TD&gt;14264&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;T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;1&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2004&lt;/TD&gt;&lt;TD&gt;15110&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2005&lt;/TD&gt;&lt;TD&gt;15110&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2006&lt;/TD&gt;&lt;TD&gt;15110&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2007&lt;/TD&gt;&lt;TD&gt;15110&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;if the data starts with a missing value for the event_occurrence variable, it is set in the var_want to either 0 or 1, and the following results get affected by that .&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;In the second example, because the first two rows of missing values are set to 1 and the&amp;nbsp; rest of the data of event_occurence is =0, following the logic of the code, the remaining values of want_var are then set to 1. But it is wrong, because an event never occurred for that firm throughout those years. So I am looking for a way to take under consideration the missing values, before they affect the values of the following want_var.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Thank you&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Sun, 01 Sep 2019 13:37:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-count-the-number-of-observations-between-two-events-and/m-p/585466#M14334</guid>
      <dc:creator>user95</dc:creator>
      <dc:date>2019-09-01T13:37:09Z</dc:date>
    </item>
    <item>
      <title>Re: How to count the number of observations between two events and create a variable conditional on</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-count-the-number-of-observations-between-two-events-and/m-p/585467#M14335</link>
      <description>&lt;P&gt;Okay Thank you&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/287084"&gt;@user95&lt;/a&gt;&amp;nbsp;. A good representative sample always helps and also avoid any assumptions plus going back and forth. The fix is very minor though. Try-&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
do until(last.id);
 set have;
 by id Event_occurrence notsorted;
 array t(999999) _temporary_;
 if first.Event_occurrence then _n=0;
 if Event_occurrence then do; _f=1;y=year;end;
 if _f then  _n+(Event_occurrence=0);
 if last.Event_occurrence and Event_occurrence=0 and _f then do;
 _i+1;
 t(_i)=_n;
 end;
end;
call missing(of _:);
if not missing(y) then _y=year ne y;
do until(last.id);
 set have;
 by id Event_occurrence notsorted;
 want_var=Event_occurrence;
 if Event_occurrence then _f=1;
 if first.Event_occurrence then _n=0;
 if first.Event_occurrence and Event_occurrence=0 and _f then _i+1;
 if _f then do; _n+(Event_occurrence=0);want_var=1;end;
 if Event_occurrence=0 and _f then do;
  if  t(_i)&amp;lt;3 then want_var=1;
  else if t(_i)=3 then want_var=0;
  else if t(_i)&amp;gt;3 then want_var=_n&amp;lt;=t(_i)-3;
 end;
 if _y then want_var=ifn(year&amp;gt;=y,1,want_var);
 output;
end;
call missing(of t(*),of _:);
drop y _:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 01 Sep 2019 14:37:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-count-the-number-of-observations-between-two-events-and/m-p/585467#M14335</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-09-01T14:37:55Z</dc:date>
    </item>
    <item>
      <title>Re: How to count the number of observations between two events and create a variable conditional on</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-count-the-number-of-observations-between-two-events-and/m-p/585472#M14336</link>
      <description>&lt;P&gt;Greetings,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you , it was helpful, one detail I noticed is that if the last line of data for a certain company's event_occurrence variable is missing the var_want is not =., instead it follows the previous rule of having all consecutive years =1 if the last.event_occurrence year is before the last year of data.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Concretely, this is what I get&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Year&lt;/TD&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;occurrence_event&lt;/TD&gt;&lt;TD&gt;Want&lt;/TD&gt;&lt;TD&gt;Have_with current code&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2003&lt;/TD&gt;&lt;TD&gt;15110&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2004&lt;/TD&gt;&lt;TD&gt;15110&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2005&lt;/TD&gt;&lt;TD&gt;15110&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2006&lt;/TD&gt;&lt;TD&gt;15110&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2007&lt;/TD&gt;&lt;TD&gt;15110&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2008&lt;/TD&gt;&lt;TD&gt;15110&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any suggestions on how to fix it?&lt;/P&gt;</description>
      <pubDate>Sun, 01 Sep 2019 16:02:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-count-the-number-of-observations-between-two-events-and/m-p/585472#M14336</guid>
      <dc:creator>user95</dc:creator>
      <dc:date>2019-09-01T16:02:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to count the number of observations between two events and create a variable conditional on</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-count-the-number-of-observations-between-two-events-and/m-p/585474#M14337</link>
      <description>&lt;P&gt;Okay&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/287084"&gt;@user95&lt;/a&gt;&amp;nbsp; &amp;nbsp;Again as you review your data thoroughly it appears the missings goes as missings and your sample contains a lot of missing values. However all these changes are very minor IF-THENs if you&lt;EM&gt; follow&lt;/EM&gt; the code to make changes. While you asked for one step solution which is fine, I really wonder whether you are able to follow and understand the code to&lt;EM&gt; edit/update&lt;/EM&gt; should you consider maintaining in a production environment. Of course I hope you do. Nonetheless, it's easier to have it split into steps to &lt;EM&gt;modify/edit/update&lt;/EM&gt; if you do not understand the code I am afraid.&amp;nbsp; I leave that up to you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
do until(last.id);
 set have;
 by id Event_occurrence notsorted;
 array t(999999) _temporary_;
 if first.Event_occurrence then _n=0;
 if Event_occurrence then do; _f=1;y=year;end;
 if _f then  _n+(Event_occurrence=0);
 if last.Event_occurrence and Event_occurrence=0 and _f then do;
 _i+1;
 t(_i)=_n;
 end;
end;
call missing(of _:);
if not missing(y) then _y=year ne y;
do until(last.id);
 set have;
 by id Event_occurrence notsorted;
 want_var=Event_occurrence;
 if Event_occurrence then _f=1;
 if first.Event_occurrence then _n=0;
 if first.Event_occurrence and Event_occurrence=0 and _f then _i+1;
 if _f and Event_occurrence&amp;gt;. then do; _n+(Event_occurrence=0);want_var=1;end;
 if Event_occurrence=0 and _f then do;
  if  t(_i)&amp;lt;3 then want_var=1;
  else if t(_i)=3 then want_var=0;
  else if t(_i)&amp;gt;3 then want_var=_n&amp;lt;=t(_i)-3;
 end;
 if _y and Event_occurrence&amp;gt;.  then want_var=ifn(year&amp;gt;=y,1,want_var);
 output;
end;
call missing(of t(*),of _:);
drop y _:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 01 Sep 2019 17:29:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-count-the-number-of-observations-between-two-events-and/m-p/585474#M14337</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-09-01T17:29:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to count the number of observations between two events and create a variable conditional on</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-count-the-number-of-observations-between-two-events-and/m-p/585487#M14339</link>
      <description>&lt;P&gt;Thank you,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It worked as intended, I am slowly getting used to working with SAS and I deeply appreciate your time and effort.&lt;/P&gt;</description>
      <pubDate>Sun, 01 Sep 2019 19:17:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-count-the-number-of-observations-between-two-events-and/m-p/585487#M14339</guid>
      <dc:creator>user95</dc:creator>
      <dc:date>2019-09-01T19:17:21Z</dc:date>
    </item>
  </channel>
</rss>

