<?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 Flag jumping years in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Flag-jumping-years/m-p/835958#M36000</link>
    <description>&lt;P&gt;Guys, is there a way to flag consecutive years while considering gaps?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Suppose to have:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Person1 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2015&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Person1 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2018&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Person1 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2019&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Person1 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2020&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Person2 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2015&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Person2 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2016&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Person2 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2018&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Person2 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2019&lt;/P&gt;
&lt;P&gt;Person3 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2015&lt;/P&gt;
&lt;P&gt;Person3 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2016&lt;/P&gt;
&lt;P&gt;Person3 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2017&lt;/P&gt;
&lt;P&gt;Person3 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2020 &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To have &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;Person1 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2015 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;
&lt;P&gt;Person1 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2018 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;
&lt;P&gt;Person1 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2019 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;
&lt;P&gt;Person1 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2020 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;
&lt;P&gt;Person2 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2015 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;
&lt;P&gt;Person2 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2016 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;
&lt;P&gt;Person2 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2018 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;/P&gt;
&lt;P&gt;Person2 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2019 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;/P&gt;
&lt;P&gt;Person3 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2015 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1 &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Person3 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2016 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;
&lt;P&gt;Person3 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2017 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;
&lt;P&gt;Person3 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2020 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0 &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you in advance.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Just edited to simplify. I used 2 to distinguish 2015-2016 from 2018-2019 for Person2 because there is a gap otherwise they will be considered consecutives while using 1 but any other flag is ok.&lt;/P&gt;</description>
    <pubDate>Thu, 29 Sep 2022 22:26:55 GMT</pubDate>
    <dc:creator>NewUsrStat</dc:creator>
    <dc:date>2022-09-29T22:26:55Z</dc:date>
    <item>
      <title>Flag jumping years</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Flag-jumping-years/m-p/835958#M36000</link>
      <description>&lt;P&gt;Guys, is there a way to flag consecutive years while considering gaps?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Suppose to have:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Person1 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2015&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Person1 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2018&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Person1 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2019&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Person1 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2020&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Person2 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2015&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Person2 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2016&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Person2 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2018&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Person2 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2019&lt;/P&gt;
&lt;P&gt;Person3 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2015&lt;/P&gt;
&lt;P&gt;Person3 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2016&lt;/P&gt;
&lt;P&gt;Person3 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2017&lt;/P&gt;
&lt;P&gt;Person3 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2020 &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To have &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;Person1 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2015 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;
&lt;P&gt;Person1 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2018 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;
&lt;P&gt;Person1 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2019 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;
&lt;P&gt;Person1 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2020 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;
&lt;P&gt;Person2 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2015 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;
&lt;P&gt;Person2 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2016 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;
&lt;P&gt;Person2 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2018 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;/P&gt;
&lt;P&gt;Person2 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2019 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;/P&gt;
&lt;P&gt;Person3 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2015 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1 &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Person3 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2016 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;
&lt;P&gt;Person3 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2017 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;
&lt;P&gt;Person3 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2020 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0 &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you in advance.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Just edited to simplify. I used 2 to distinguish 2015-2016 from 2018-2019 for Person2 because there is a gap otherwise they will be considered consecutives while using 1 but any other flag is ok.&lt;/P&gt;</description>
      <pubDate>Thu, 29 Sep 2022 22:26:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Flag-jumping-years/m-p/835958#M36000</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2022-09-29T22:26:55Z</dc:date>
    </item>
    <item>
      <title>Re: Flag jumping years</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Flag-jumping-years/m-p/835960#M36001</link>
      <description>&lt;P&gt;I assume by "jumping years" you mean out of sequence. With an unstated requirement of "within Person"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why is the first record marked as a flag:&lt;/P&gt;
&lt;PRE&gt;Person1       2015      flag
Person1       2018        0&lt;/PRE&gt;
&lt;P&gt;I would expect&lt;/P&gt;
&lt;PRE&gt;Person1       2015      0
Person1       2018      flag&lt;/PRE&gt;
&lt;P&gt;Assuming my interpretation above is complete one way:&lt;/P&gt;
&lt;PRE&gt;data have;
   input person $ year;
datalines;
Person1       2015 
Person1       2018 
Person1       2019 
Person1       2020 
Person2       2015 
Person2       2016 
Person2       2018 
Person2       2019
Person3       2015
Person3       2016
Person3       2017
Person3       2020
;

/* assumes sorted by Person and Year*/

data want;
   set have;
   by person;
   dyr = dif(year);
   if first.person then flag=0;
   else flag=dyr&amp;gt;1;
   drop dyr;
run;&lt;/PRE&gt;
&lt;P&gt;The data step is the way to provide example data. When we have to guess we make assumptions as to variable types that may not be correct.&lt;/P&gt;
&lt;P&gt;The DIF function returns the numeric difference between the current value of a variable and that of the previous record with some limitations.&lt;/P&gt;
&lt;P&gt;SAS when using BY group processing in a data step adds automatic variables First. and Last. for the variables on the By statement that indicate whether the current record is the first or last of the group. These are numeric 1 (true)/ 0 (false) values and can be used as logical values as shown.&lt;/P&gt;
&lt;P&gt;If your data is not sorted by ID but is grouped by id with the years in sequence you would add the option NOTSORTED to the BY statement as the default is that the values are sorted and will cause an error if the data is not actually sorted.&lt;/P&gt;
&lt;P&gt;A flag value of 1 indicates difference greater than one.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note: if you are concerned about same values in sequence this will not flag repeated values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Sep 2022 22:08:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Flag-jumping-years/m-p/835960#M36001</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-09-29T22:08:25Z</dc:date>
    </item>
    <item>
      <title>Re: Flag jumping years</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Flag-jumping-years/m-p/835962#M36002</link>
      <description>&lt;P&gt;It is the same: I need to flag "all in sequence" or "all out of sequence" because consecutive sequences will be processed (but not "outliers"). Thank you very much!&lt;/P&gt;</description>
      <pubDate>Thu, 29 Sep 2022 22:13:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Flag-jumping-years/m-p/835962#M36002</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2022-09-29T22:13:19Z</dc:date>
    </item>
    <item>
      <title>Re: Flag jumping years</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Flag-jumping-years/m-p/835963#M36003</link>
      <description>I think you meant to flag the second observation not the first.&lt;BR /&gt;&lt;BR /&gt;Here is one approach.&lt;BR /&gt;&lt;BR /&gt;data want;&lt;BR /&gt;set have:&lt;BR /&gt;by person;&lt;BR /&gt;flag = dif(year) ne 1;&lt;BR /&gt;if first.person then flag=0;&lt;BR /&gt;run;</description>
      <pubDate>Thu, 29 Sep 2022 22:13:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Flag-jumping-years/m-p/835963#M36003</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2022-09-29T22:13:21Z</dc:date>
    </item>
    <item>
      <title>Re: Flag jumping years</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Flag-jumping-years/m-p/835965#M36004</link>
      <description>Just need to flag out of consecutive</description>
      <pubDate>Thu, 29 Sep 2022 22:14:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Flag-jumping-years/m-p/835965#M36004</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2022-09-29T22:14:13Z</dc:date>
    </item>
    <item>
      <title>Re: Flag jumping years</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Flag-jumping-years/m-p/835971#M36005</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/134532"&gt;@NewUsrStat&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Just need to flag out of consecutive&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Assign each sequence its own number.&amp;nbsp; Then you can use BY group processing to exclude the sequences that only have one observation.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input id $ year ;
cards;
Person1 2015
Person1 2018
Person1 2019
Person1 2020
Person2 2015
Person2 2016
Person2 2018
Person2 2019
Person3 2015
Person3 2016
Person3 2017
Person3 2020
;

data want;
 set have;
 by id ;
 sequence + dif(year) &amp;gt; 1;
 if first.id then sequence=1;
run;

proc print;
run;

data subset;
  set want;
  by id sequence;
  if not (first.sequence and last.sequence);
run;

proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1664493023258.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/75725i35103FEB6C44792F/image-size/large?v=v2&amp;amp;px=999" role="button" title="Tom_0-1664493023258.png" alt="Tom_0-1664493023258.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_1-1664493046536.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/75726iF3A5581632791598/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_1-1664493046536.png" alt="Tom_1-1664493046536.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Sep 2022 23:11:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Flag-jumping-years/m-p/835971#M36005</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-09-29T23:11:11Z</dc:date>
    </item>
    <item>
      <title>Re: Flag jumping years</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Flag-jumping-years/m-p/836011#M36013</link>
      <description>Thank you very much! It works perfectly!</description>
      <pubDate>Fri, 30 Sep 2022 07:00:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Flag-jumping-years/m-p/836011#M36013</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2022-09-30T07:00:52Z</dc:date>
    </item>
  </channel>
</rss>

