<?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 Creating a variable that counts consecutive years in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-that-counts-consecutive-years/m-p/881256#M348203</link>
    <description>&lt;P&gt;I have the following dataset:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

DATA have;
input year	person_id	firm_id;
DATALINES;
2000	1	405
2001	1	405
2002	1	405
2003	1	302
2004	1	302
2005	1	302
2006	1	405
2007	1	405
2008	1	405
2009	1	543
2010	1	543
2005	2	112
2006	2	112
2007	2	112
2010	2	112
2011	2	405
2012	2	543
2013	2	112
2014	2	112
2015	2	112
2016	2	112
2017	2	112
2018	2	112
2019	2	112
2001	3	405
2004	3	405
2005	3	302
2010	3	302
2011	3	405
2014	3	405
2015	3	405
2016	3	405
2000	4	302
2005	4	405
2010	4	112

;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to create a variable called tenure to produce the following:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA want;
input year	person_id	firm_id	tenure;
DATALINES;
2000	1	405	0
2001	1	405	1
2002	1	405	2
2003	1	302	0
2004	1	302	1
2005	1	302	2
2006	1	405	0
2007	1	405	1
2008	1	405	2
2009	1	543	0
2010	1	543	1
2005	2	112	0
2006	2	112	1
2007	2	112	2
2010	2	112	5
2011	2	405	0
2012	2	543	0
2013	2	112	0
2014	2	112	1
2015	2	112	2
2016	2	112	3
2017	2	112	4
2018	2	112	5
2019	2	112	6
2001	3	405	0
2004	3	405	3
2005	3	302	0
2010	3	302	5
2011	3	405	0
2014	3	405	3
2015	3	405	4
2016	3	405	5
2000	4	302	0
2005	4	405	0
2010	4	112	0
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Basically, tenure is constructed as the number of consecutive years that the same firm_id has served the person_id. For example, firm_id=405 serves person_id=1 from 2000 to 2002, but then person_id=1 switches to firm_id=302 in 2003, so the tenure variable resets to 0. In 2006, the person switches back to firm_id=405, even though this firm has served this person before, tenure is defined as consecutive years of service, so tenure begins from 0 again in 2006.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that there could be gaps in years, if there are gaps, and the firm_id did not change, then we assume the same firm_id has served the person_id. E.g., person_id=3 and firm_id=405 in 2001 and 2004. Even though there is a gap, since the firm_id does not change, tenure is 3 in 2004. If the firm_id changes when there is a gap, then tenure = 0. E.g., person_id=4.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 17 Jun 2023 11:16:15 GMT</pubDate>
    <dc:creator>elbarto</dc:creator>
    <dc:date>2023-06-17T11:16:15Z</dc:date>
    <item>
      <title>Creating a variable that counts consecutive years</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-that-counts-consecutive-years/m-p/881256#M348203</link>
      <description>&lt;P&gt;I have the following dataset:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

DATA have;
input year	person_id	firm_id;
DATALINES;
2000	1	405
2001	1	405
2002	1	405
2003	1	302
2004	1	302
2005	1	302
2006	1	405
2007	1	405
2008	1	405
2009	1	543
2010	1	543
2005	2	112
2006	2	112
2007	2	112
2010	2	112
2011	2	405
2012	2	543
2013	2	112
2014	2	112
2015	2	112
2016	2	112
2017	2	112
2018	2	112
2019	2	112
2001	3	405
2004	3	405
2005	3	302
2010	3	302
2011	3	405
2014	3	405
2015	3	405
2016	3	405
2000	4	302
2005	4	405
2010	4	112

;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to create a variable called tenure to produce the following:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA want;
input year	person_id	firm_id	tenure;
DATALINES;
2000	1	405	0
2001	1	405	1
2002	1	405	2
2003	1	302	0
2004	1	302	1
2005	1	302	2
2006	1	405	0
2007	1	405	1
2008	1	405	2
2009	1	543	0
2010	1	543	1
2005	2	112	0
2006	2	112	1
2007	2	112	2
2010	2	112	5
2011	2	405	0
2012	2	543	0
2013	2	112	0
2014	2	112	1
2015	2	112	2
2016	2	112	3
2017	2	112	4
2018	2	112	5
2019	2	112	6
2001	3	405	0
2004	3	405	3
2005	3	302	0
2010	3	302	5
2011	3	405	0
2014	3	405	3
2015	3	405	4
2016	3	405	5
2000	4	302	0
2005	4	405	0
2010	4	112	0
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Basically, tenure is constructed as the number of consecutive years that the same firm_id has served the person_id. For example, firm_id=405 serves person_id=1 from 2000 to 2002, but then person_id=1 switches to firm_id=302 in 2003, so the tenure variable resets to 0. In 2006, the person switches back to firm_id=405, even though this firm has served this person before, tenure is defined as consecutive years of service, so tenure begins from 0 again in 2006.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that there could be gaps in years, if there are gaps, and the firm_id did not change, then we assume the same firm_id has served the person_id. E.g., person_id=3 and firm_id=405 in 2001 and 2004. Even though there is a gap, since the firm_id does not change, tenure is 3 in 2004. If the firm_id changes when there is a gap, then tenure = 0. E.g., person_id=4.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 17 Jun 2023 11:16:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-that-counts-consecutive-years/m-p/881256#M348203</guid>
      <dc:creator>elbarto</dc:creator>
      <dc:date>2023-06-17T11:16:15Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a variable that counts consecutive years</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-that-counts-consecutive-years/m-p/881257#M348204</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by person_id firm_id notsorted;
retain _year;
if first.firm_id then _year = year;
tenure = year - _year;
drop _year;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Untested, posted from my tablet.&lt;/P&gt;</description>
      <pubDate>Sat, 17 Jun 2023 12:40:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-that-counts-consecutive-years/m-p/881257#M348204</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-06-17T12:40:32Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a variable that counts consecutive years</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-that-counts-consecutive-years/m-p/881277#M348214</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by person_id firm_id notsorted;
  tenure+dif(year);
  if first.firm_id then tenure=0;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 17 Jun 2023 17:31:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-that-counts-consecutive-years/m-p/881277#M348214</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-06-17T17:31:52Z</dc:date>
    </item>
  </channel>
</rss>

