<?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: Observations in pre and post events only in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Observations-in-pre-and-post-events-only/m-p/560135#M156544</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/198822"&gt;@amanjot_42&lt;/a&gt;&amp;nbsp; &amp;nbsp;For what it;s worth, some boring proc sql boolean&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input Cusip Year Sales dummy;
datalines;
11000 2005 10 0
11000 2006  20 1
11000 2007  30 0
11000 2008  40 0
12000 2004  65 1
12000 2005  90 0
12000 2006  65 0
13000 2010  80 0
13000 2011  75 0
13000 2012  95 1
14000 2008  65 0
14000 2009  45 0
14000 2010  70 1
14000 2011  90 0
;

proc sql;
create table want(drop=_:) as
select *,sum(_t1)&amp;gt;0 and sum(_t2)&amp;gt;0 as _t
from 
(select *,max((dummy=1)*year) as _year, calculated _year-5 lt Year lt calculated _year as _t1,calculated _Year lt year lt  calculated _year+5 as _t2
from have
group by cusip)
group by cusip
having _t;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or even simpler&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select a.*
from have a left join (select cusip,year from have where dummy=1) b
on a.cusip=b.cusip
group by a.cusip
having max(b.year-5 lt a.Year lt b.year)&amp;gt;0 and max( b.Year lt a.year lt   b.year+5 )&amp;gt;0;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 20 May 2019 14:15:49 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2019-05-20T14:15:49Z</dc:date>
    <item>
      <title>Observations in pre and post events only</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Observations-in-pre-and-post-events-only/m-p/560105#M156532</link>
      <description>&lt;P&gt;Hi guys,&lt;/P&gt;&lt;P&gt;I am new to SAS!&lt;/P&gt;&lt;P&gt;Actually, I was trying to limit my sample into those firms only which have data for both pre and post events in my sample. The event years are different for different firms. For instance, if event year of one firm is 2006, I want to see whether this firm has data for the previous five years and post five years or not. I don't want the firms to be having data for each of the pre-and post years. eve observation from pre- and one from post- would suffice my purpose. But they should have at least one in each if they don't then I want to delete those firms from my sample.&lt;/P&gt;&lt;P&gt;&amp;nbsp;Data look like:&lt;/P&gt;&lt;P&gt;Cusip Year Sales event_year&lt;/P&gt;&lt;P&gt;11000 2005 10&amp;nbsp; 0&lt;/P&gt;&lt;P&gt;11000&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;2006&amp;nbsp; 20&amp;nbsp; 1&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;11000 2007&amp;nbsp; 30&amp;nbsp; 0&lt;/P&gt;&lt;P&gt;11000 2008&amp;nbsp; 40&amp;nbsp; 0&lt;/P&gt;&lt;P&gt;12000&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;2004&lt;/STRONG&gt;&amp;nbsp;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;65&amp;nbsp; 1&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;12000 2005&amp;nbsp; 90&amp;nbsp; 0&lt;/P&gt;&lt;P&gt;12000 2006&amp;nbsp; 65&amp;nbsp; 0&lt;/P&gt;&lt;P&gt;13000 2010&amp;nbsp; 80&amp;nbsp; 0&lt;/P&gt;&lt;P&gt;13000 2011&amp;nbsp; 75&amp;nbsp; 0&amp;nbsp;&lt;/P&gt;&lt;P&gt;13000&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;2012&amp;nbsp; 95&amp;nbsp; 1&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;14000 2008&amp;nbsp; 65&amp;nbsp; 0&lt;/P&gt;&lt;P&gt;14000 2009&amp;nbsp; 45&amp;nbsp; 0&lt;/P&gt;&lt;P&gt;14000&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;2010&amp;nbsp; 70&amp;nbsp; 1&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;14000 2011&amp;nbsp; 90&amp;nbsp; 0&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So according to the criteria, I don't want cusip codes 13000 and 12000 because they don't have data for either pre- or post event years. Please help me with the code!&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;P&gt;Aman&lt;/P&gt;</description>
      <pubDate>Mon, 20 May 2019 12:05:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Observations-in-pre-and-post-events-only/m-p/560105#M156532</guid>
      <dc:creator>amanjot_42</dc:creator>
      <dc:date>2019-05-20T12:05:03Z</dc:date>
    </item>
    <item>
      <title>Re: Observations in pre and post events only</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Observations-in-pre-and-post-events-only/m-p/560121#M156540</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Cusip Year Sales dummy;
datalines;
11000 2005 10 0
11000 2006  20 1
11000 2007  30 0
11000 2008  40 0
12000 2004  65 1
12000 2005  90 0
12000 2006  65 0
13000 2010  80 0
13000 2011  75 0
13000 2012  95 1
14000 2008  65 0
14000 2009  45 0
14000 2010  70 1
14000 2011  90 0
;

data temp;
   do until (last.Cusip);
      set have;
      by Cusip;
      if dummy=1 then _year=Year;
   end;

   do until (last.Cusip);
      set have;
      by Cusip;
      if _year-5 le Year le _year+5 then output;
   end;
run;

proc sql;
   create table want as
      select * from temp
      group by Cusip
      having max(Year) gt _year
         and min(Year) lt _year;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 20 May 2019 13:12:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Observations-in-pre-and-post-events-only/m-p/560121#M156540</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-05-20T13:12:34Z</dc:date>
    </item>
    <item>
      <title>Re: Observations in pre and post events only</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Observations-in-pre-and-post-events-only/m-p/560135#M156544</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/198822"&gt;@amanjot_42&lt;/a&gt;&amp;nbsp; &amp;nbsp;For what it;s worth, some boring proc sql boolean&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input Cusip Year Sales dummy;
datalines;
11000 2005 10 0
11000 2006  20 1
11000 2007  30 0
11000 2008  40 0
12000 2004  65 1
12000 2005  90 0
12000 2006  65 0
13000 2010  80 0
13000 2011  75 0
13000 2012  95 1
14000 2008  65 0
14000 2009  45 0
14000 2010  70 1
14000 2011  90 0
;

proc sql;
create table want(drop=_:) as
select *,sum(_t1)&amp;gt;0 and sum(_t2)&amp;gt;0 as _t
from 
(select *,max((dummy=1)*year) as _year, calculated _year-5 lt Year lt calculated _year as _t1,calculated _Year lt year lt  calculated _year+5 as _t2
from have
group by cusip)
group by cusip
having _t;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or even simpler&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select a.*
from have a left join (select cusip,year from have where dummy=1) b
on a.cusip=b.cusip
group by a.cusip
having max(b.year-5 lt a.Year lt b.year)&amp;gt;0 and max( b.Year lt a.year lt   b.year+5 )&amp;gt;0;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 May 2019 14:15:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Observations-in-pre-and-post-events-only/m-p/560135#M156544</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-05-20T14:15:49Z</dc:date>
    </item>
    <item>
      <title>Re: Observations in pre and post events only</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Observations-in-pre-and-post-events-only/m-p/560142#M156547</link>
      <description>&lt;P&gt;Hello &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/198822"&gt;@amanjot_42&lt;/a&gt;&amp;nbsp; Another one step DOW approach&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input Cusip Year Sales dummy;
datalines;
11000 2005 10 0
11000 2006  20 1
11000 2007  30 0
11000 2008  40 0
12000 2004  65 1
12000 2005  90 0
12000 2006  65 0
13000 2010  80 0
13000 2011  75 0
13000 2012  95 1
14000 2008  65 0
14000 2009  45 0
14000 2010  70 1
14000 2011  90 0
;

data want;
do until(last.cusip);
merge have have(where=(_d=1) rename=(dummy=_d year=_year) keep=cusip dummy year);
by cusip;
if _year-5 lt Year lt _year then k=sum(k,1);
else  if  _Year lt year lt  _year+5 then k1=sum(k1,1);
end;
do until(last.cusip);
set have;
by cusip;
if k&amp;gt;0 and k1&amp;gt;0 then output;
end;
drop _: k:;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 May 2019 13:56:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Observations-in-pre-and-post-events-only/m-p/560142#M156547</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-05-20T13:56:10Z</dc:date>
    </item>
    <item>
      <title>Re: Observations in pre and post events only</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Observations-in-pre-and-post-events-only/m-p/560319#M156651</link>
      <description>&lt;P&gt;Thank you so much!&lt;/P&gt;&lt;P&gt;It worked really well!&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;</description>
      <pubDate>Tue, 21 May 2019 00:33:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Observations-in-pre-and-post-events-only/m-p/560319#M156651</guid>
      <dc:creator>amanjot_42</dc:creator>
      <dc:date>2019-05-21T00:33:46Z</dc:date>
    </item>
    <item>
      <title>Re: Observations in pre and post events only</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Observations-in-pre-and-post-events-only/m-p/560320#M156652</link>
      <description>&lt;P&gt;thank you for the additional inputs!&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;</description>
      <pubDate>Tue, 21 May 2019 00:34:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Observations-in-pre-and-post-events-only/m-p/560320#M156652</guid>
      <dc:creator>amanjot_42</dc:creator>
      <dc:date>2019-05-21T00:34:23Z</dc:date>
    </item>
  </channel>
</rss>

