<?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 Retain only -3 and +3 period obs in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Retain-only-3-and-3-period-obs/m-p/445336#M283097</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to retain only -3 and +3 period observations for each group.&lt;/P&gt;&lt;P&gt;Now my data looks like...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data example;&lt;/P&gt;&lt;P&gt;input&amp;nbsp;firm manager $ year turnover post_turnover turnover_year;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;1001 abc 2005 0 0 .&lt;/P&gt;&lt;P&gt;1001 abc 2006 0 0 .&lt;/P&gt;&lt;P&gt;1001 abc 2007 1 1 2007&lt;/P&gt;&lt;P&gt;1001 abc 2008 0 0 .&lt;/P&gt;&lt;P&gt;1001 abc 2008 0&amp;nbsp;1&amp;nbsp;.&lt;/P&gt;&lt;P&gt;1001 abc 2009 0 0 .&lt;/P&gt;&lt;P&gt;1001 abc 2009 0 1 .&amp;nbsp;&lt;/P&gt;&lt;P&gt;1001 abc 2010 0 0 .&amp;nbsp;&lt;/P&gt;&lt;P&gt;1001 abc 2010 0 1 .&lt;/P&gt;&lt;P&gt;1001 abc 2011&amp;nbsp;0&amp;nbsp;0 .&amp;nbsp;&lt;/P&gt;&lt;P&gt;1001 abc 2011 0 1 .&lt;/P&gt;&lt;P&gt;1001 abc 2012 1 1 2012&lt;/P&gt;&lt;P&gt;1001 abc 2013 0 1 .&lt;/P&gt;&lt;P&gt;1002 def 1990 0 0 .&lt;/P&gt;&lt;P&gt;1002 def 1991 0 0 .&lt;/P&gt;&lt;P&gt;1002 def 1992 0 0 .&lt;/P&gt;&lt;P&gt;1002 def 1993 0 0 .&lt;/P&gt;&lt;P&gt;1002 def 1994 1 1 1994&lt;/P&gt;&lt;P&gt;1002 def 1995 0&amp;nbsp;0 .&lt;/P&gt;&lt;P&gt;1002 def 1995 0 1 .&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1002 def 1996&amp;nbsp;1 1 1996&lt;/P&gt;&lt;P&gt;1002 def 1996 0 1 .&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As you can see, I have multiple turnovers by each firm. So, observations between two turnovers were duplicated because they can be both post and pre.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now my problem is how I can retain&amp;nbsp;only observations within three years around turnover_year (i.e., -3 and +3) for each firm. Also, I need to exclude the observations where the turnover was occurred.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please help me this problem.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you SAS masters!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 13 Mar 2018 21:57:36 GMT</pubDate>
    <dc:creator>hkim3677</dc:creator>
    <dc:date>2018-03-13T21:57:36Z</dc:date>
    <item>
      <title>Retain only -3 and +3 period obs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-only-3-and-3-period-obs/m-p/445336#M283097</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to retain only -3 and +3 period observations for each group.&lt;/P&gt;&lt;P&gt;Now my data looks like...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data example;&lt;/P&gt;&lt;P&gt;input&amp;nbsp;firm manager $ year turnover post_turnover turnover_year;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;1001 abc 2005 0 0 .&lt;/P&gt;&lt;P&gt;1001 abc 2006 0 0 .&lt;/P&gt;&lt;P&gt;1001 abc 2007 1 1 2007&lt;/P&gt;&lt;P&gt;1001 abc 2008 0 0 .&lt;/P&gt;&lt;P&gt;1001 abc 2008 0&amp;nbsp;1&amp;nbsp;.&lt;/P&gt;&lt;P&gt;1001 abc 2009 0 0 .&lt;/P&gt;&lt;P&gt;1001 abc 2009 0 1 .&amp;nbsp;&lt;/P&gt;&lt;P&gt;1001 abc 2010 0 0 .&amp;nbsp;&lt;/P&gt;&lt;P&gt;1001 abc 2010 0 1 .&lt;/P&gt;&lt;P&gt;1001 abc 2011&amp;nbsp;0&amp;nbsp;0 .&amp;nbsp;&lt;/P&gt;&lt;P&gt;1001 abc 2011 0 1 .&lt;/P&gt;&lt;P&gt;1001 abc 2012 1 1 2012&lt;/P&gt;&lt;P&gt;1001 abc 2013 0 1 .&lt;/P&gt;&lt;P&gt;1002 def 1990 0 0 .&lt;/P&gt;&lt;P&gt;1002 def 1991 0 0 .&lt;/P&gt;&lt;P&gt;1002 def 1992 0 0 .&lt;/P&gt;&lt;P&gt;1002 def 1993 0 0 .&lt;/P&gt;&lt;P&gt;1002 def 1994 1 1 1994&lt;/P&gt;&lt;P&gt;1002 def 1995 0&amp;nbsp;0 .&lt;/P&gt;&lt;P&gt;1002 def 1995 0 1 .&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1002 def 1996&amp;nbsp;1 1 1996&lt;/P&gt;&lt;P&gt;1002 def 1996 0 1 .&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As you can see, I have multiple turnovers by each firm. So, observations between two turnovers were duplicated because they can be both post and pre.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now my problem is how I can retain&amp;nbsp;only observations within three years around turnover_year (i.e., -3 and +3) for each firm. Also, I need to exclude the observations where the turnover was occurred.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please help me this problem.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you SAS masters!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 13 Mar 2018 21:57:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-only-3-and-3-period-obs/m-p/445336#M283097</guid>
      <dc:creator>hkim3677</dc:creator>
      <dc:date>2018-03-13T21:57:36Z</dc:date>
    </item>
    <item>
      <title>Re: Retain only -3 and +3 period obs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-only-3-and-3-period-obs/m-p/445341#M283098</link>
      <description>&lt;P&gt;You don't say if Manager plays any role in the selection process.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See if this gets close:&lt;/P&gt;
&lt;PRE&gt;proc sql;
   create table want as
   select distinct b.*
   from (select * from example
         where not missing(turnover_year) )as a
        left join
        example as b
        on a.firm=b.firm
        and abs (a.year - b.year) le 3
        and missing(b.turnover_year)
   ;
quit;&lt;/PRE&gt;</description>
      <pubDate>Tue, 13 Mar 2018 22:17:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-only-3-and-3-period-obs/m-p/445341#M283098</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-03-13T22:17:39Z</dc:date>
    </item>
    <item>
      <title>Re: Retain only -3 and +3 period obs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-only-3-and-3-period-obs/m-p/445349#M283099</link>
      <description>&lt;P&gt;Amazing Thank you!&lt;/P&gt;</description>
      <pubDate>Tue, 13 Mar 2018 22:37:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-only-3-and-3-period-obs/m-p/445349#M283099</guid>
      <dc:creator>hkim3677</dc:creator>
      <dc:date>2018-03-13T22:37:31Z</dc:date>
    </item>
    <item>
      <title>Re: Retain only -3 and +3 period obs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-only-3-and-3-period-obs/m-p/445478#M283100</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data example;
input firm manager $ year turnover post_turnover turnover_year;
datalines;
1001 abc 2005 0 0 .
1001 abc 2006 0 0 .
1001 abc 2007 1 1 2007
1001 abc 2008 0 0 .
1001 abc 2008 0 1 .
1001 abc 2009 0 0 .
1001 abc 2009 0 1 . 
1001 abc 2010 0 0 . 
1001 abc 2010 0 1 .
1001 abc 2011 0 0 . 
1001 abc 2011 0 1 .
1001 abc 2012 1 1 2012
1001 abc 2013 0 1 .
1002 def 1990 0 0 .
1002 def 1991 0 0 .
1002 def 1992 0 0 .
1002 def 1993 0 0 .
1002 def 1994 1 1 1994
1002 def 1995 0 0 .
1002 def 1995 0 1 .
1002 def 1996 1 1 1996
1002 def 1996 0 1 .
;
run;
data key;
 set example(where=(turnover_year is not missing));
 do i=turnover_year-3 to turnover_year+3;
  year=i;output;
 end;
 keep firm manager year;
run;
data want;
 if _n_=1 then do;
   if 0 then set key;
   declare hash h(dataset:'key');
   h.definekey('firm', 'manager', 'year');
   h.definedone();
 end;
set example;
if h.check()=0;
run;

proc print;run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 14 Mar 2018 13:39:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-only-3-and-3-period-obs/m-p/445478#M283100</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-03-14T13:39:09Z</dc:date>
    </item>
  </channel>
</rss>

