<?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: Find the observations in the current period exist in the previous period in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Find-the-observations-in-the-current-period-exist-in-the/m-p/805973#M317512</link>
    <description>&lt;P&gt;Should be simple in PROC SQL.&lt;/P&gt;
&lt;P&gt;First let's convert your listing into a dataset (why did you list the variables in backwards order?).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  length id year gvkey 8;
  input year gvkey id;
cards;
2010	1000	1
2011	1000	1
2011	2000	1
2011	1000	2
2014	3000	2
2017	3000	2
2014	2000	3
2014	4000	3
2016	2000	3
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now just make the indicator to flag the values of GVKEY that occur after the first time it occurs.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
  select *,(year &amp;gt; min(year)) as indicator
  from have
  group id,gvkey
  order by id,year,gvkey
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;PRE&gt;Obs    id    year    gvkey    indicator

 1      1    2010     1000        0
 2      1    2011     1000        1
 3      1    2011     2000        0
 4      2    2011     1000        0
 5      2    2014     3000        0
 6      2    2017     3000        1
 7      3    2014     2000        0
 8      3    2014     4000        0
 9      3    2016     2000        1
&lt;/PRE&gt;</description>
    <pubDate>Tue, 05 Apr 2022 04:25:22 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2022-04-05T04:25:22Z</dc:date>
    <item>
      <title>Find the observations in the current period exist in the previous period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-observations-in-the-current-period-exist-in-the/m-p/805906#M317482</link>
      <description>&lt;P&gt;My data is the panel data.&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;year&lt;/TD&gt;&lt;TD&gt;gvkey&lt;/TD&gt;&lt;TD&gt;id&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2010&lt;/TD&gt;&lt;TD&gt;1000&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2011&lt;/TD&gt;&lt;TD&gt;1000&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2011&lt;/TD&gt;&lt;TD&gt;2000&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2011&lt;/TD&gt;&lt;TD&gt;1000&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2014&lt;/TD&gt;&lt;TD&gt;3000&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2017&lt;/TD&gt;&lt;TD&gt;3000&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2014&lt;/TD&gt;&lt;TD&gt;2000&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2014&lt;/TD&gt;&lt;TD&gt;4000&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2016&lt;/TD&gt;&lt;TD&gt;2000&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to find whether the &lt;STRONG&gt;gvkey&lt;/STRONG&gt;s of an &lt;STRONG&gt;id&lt;/STRONG&gt; of the current period exist in the prior period, regardless of whether there is a discontinuity in the years (i.e., the&lt;STRONG&gt; id&lt;/STRONG&gt; &lt;STRONG&gt;2&lt;/STRONG&gt; has a three-year gap between 2014 and 2017). The panel data is fine as long as the years are in order by grouping the&lt;STRONG&gt; id&lt;/STRONG&gt;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;year&lt;/TD&gt;&lt;TD&gt;gvkey&lt;/TD&gt;&lt;TD&gt;id&lt;/TD&gt;&lt;TD&gt;indicator&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2010&lt;/TD&gt;&lt;TD&gt;1000&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;2011&lt;/TD&gt;&lt;TD&gt;1000&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;2011&lt;/TD&gt;&lt;TD&gt;2000&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;2011&lt;/TD&gt;&lt;TD&gt;1000&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2014&lt;/TD&gt;&lt;TD&gt;3000&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2017&lt;/TD&gt;&lt;TD&gt;3000&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2014&lt;/TD&gt;&lt;TD&gt;2000&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2014&lt;/TD&gt;&lt;TD&gt;4000&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2016&lt;/TD&gt;&lt;TD&gt;2000&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;Hence, my intended outcome should look like the column &lt;STRONG&gt;indicator&lt;/STRONG&gt; where 1 equals presence and 0 equals absence in the prior period.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you so much for your assistance.&lt;/P&gt;</description>
      <pubDate>Mon, 04 Apr 2022 17:56:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-observations-in-the-current-period-exist-in-the/m-p/805906#M317482</guid>
      <dc:creator>Jarvin99</dc:creator>
      <dc:date>2022-04-04T17:56:07Z</dc:date>
    </item>
    <item>
      <title>Re: Find the observations in the current period exist in the previous period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-observations-in-the-current-period-exist-in-the/m-p/805915#M317487</link>
      <description>&lt;P&gt;Try this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input year gvkey id;
datalines;
2010 1000 1
2011 1000 1
2011 2000 1
2011 1000 2
2014 3000 2
2017 3000 2
2014 2000 3
2014 4000 3
2016 2000 3
;

data want;
   do _N_ = 1 by 1 until (last.id);
      set have;
	  by id;
      array r {999} _temporary_;
	  indicator = gvkey in r;
	  r[_N_] = gvkey;
	  output;
   end;

   call missing(of r[*]);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;U&gt;Result:&lt;/U&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;year  gvkey  id  indicator 
2010  1000   1   0 
2011  1000   1   1 
2011  2000   1   0 
2011  1000   2   0 
2014  3000   2   0 
2017  3000   2   1 
2014  2000   3   0 
2014  4000   3   0 
2016  2000   3   1 &lt;/PRE&gt;</description>
      <pubDate>Mon, 04 Apr 2022 19:09:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-observations-in-the-current-period-exist-in-the/m-p/805915#M317487</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-04-04T19:09:45Z</dc:date>
    </item>
    <item>
      <title>Re: Find the observations in the current period exist in the previous period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-observations-in-the-current-period-exist-in-the/m-p/805955#M317503</link>
      <description>&lt;P&gt;Assuming the data are sorted by ID/YEAR, then you can just keep a running list of GVKEYs encountered for each ID:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input year gvkey id;
datalines;
2010 1000 1
2011 1000 1
2011 2000 1
2011 1000 2
2014 3000 2
2017 3000 2
2014 2000 3
2014 4000 3
2016 2000 3
;
data want;
  set have;
  by id ;
  array _gvkeys {20} _temporary_;
  if first.id then call missing(of _gvkeys{*});
  indicator=whichn(gvkey,of _gvkeys{*})&amp;gt;0;
  if indicator=0 then _gvkeys{n(of _gvkeys{*})+1}=gvkey;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This program assumes no ID has more the 20 observations.&amp;nbsp; If you expect more then increase the size of the array &lt;STRONG&gt;_GVKEYS&lt;/STRONG&gt;.&lt;/P&gt;</description>
      <pubDate>Mon, 04 Apr 2022 22:29:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-observations-in-the-current-period-exist-in-the/m-p/805955#M317503</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-04-04T22:29:56Z</dc:date>
    </item>
    <item>
      <title>Re: Find the observations in the current period exist in the previous period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-observations-in-the-current-period-exist-in-the/m-p/805973#M317512</link>
      <description>&lt;P&gt;Should be simple in PROC SQL.&lt;/P&gt;
&lt;P&gt;First let's convert your listing into a dataset (why did you list the variables in backwards order?).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  length id year gvkey 8;
  input year gvkey id;
cards;
2010	1000	1
2011	1000	1
2011	2000	1
2011	1000	2
2014	3000	2
2017	3000	2
2014	2000	3
2014	4000	3
2016	2000	3
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now just make the indicator to flag the values of GVKEY that occur after the first time it occurs.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
  select *,(year &amp;gt; min(year)) as indicator
  from have
  group id,gvkey
  order by id,year,gvkey
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;PRE&gt;Obs    id    year    gvkey    indicator

 1      1    2010     1000        0
 2      1    2011     1000        1
 3      1    2011     2000        0
 4      2    2011     1000        0
 5      2    2014     3000        0
 6      2    2017     3000        1
 7      3    2014     2000        0
 8      3    2014     4000        0
 9      3    2016     2000        1
&lt;/PRE&gt;</description>
      <pubDate>Tue, 05 Apr 2022 04:25:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-observations-in-the-current-period-exist-in-the/m-p/805973#M317512</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-04-05T04:25:22Z</dc:date>
    </item>
    <item>
      <title>Re: Find the observations in the current period exist in the previous period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-observations-in-the-current-period-exist-in-the/m-p/806021#M317528</link>
      <description>&lt;P&gt;Should be more efficient in Hash Table and keep original order.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  length id year gvkey 8;
  input year gvkey id;
cards;
2010	1000	1
2011	1000	1
2011	2000	1
2011	1000	2
2014	3000	2
2017	3000	2
2014	2000	3
2014	4000	3
2016	2000	3
;
proc sql noprint;
select min(year) into :year from have;
quit;
data want;
 if _n_=1 then do;
  declare hash h(dataset:'have');
  h.definekey('year','gvkey','id');
  h.definedone();
 end;
set have; 
indicator=0;
do year1=&amp;amp;year. to year-1; 
 if h.check(key:year1,key:gvkey,key:id)=0 then do;indicator=1;leave;end;
end;
drop year1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 05 Apr 2022 10:44:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-observations-in-the-current-period-exist-in-the/m-p/806021#M317528</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-04-05T10:44:38Z</dc:date>
    </item>
  </channel>
</rss>

