<?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: Add and update flags for consecutive dates in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Add-and-update-flags-for-consecutive-dates/m-p/828125#M35393</link>
    <description>&lt;P&gt;From your explanation and example, the start and stop dates are not used in determining the flags. Is that right?&lt;/P&gt;</description>
    <pubDate>Wed, 10 Aug 2022 17:16:19 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2022-08-10T17:16:19Z</dc:date>
    <item>
      <title>Add and update flags for consecutive dates</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Add-and-update-flags-for-consecutive-dates/m-p/828087#M35379</link>
      <description>&lt;P&gt;Hi guys,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have the following data set:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; ID&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Start&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Stop&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;001&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;01JAN2013&amp;nbsp; &amp;nbsp; &amp;nbsp;31JAN2013&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;001&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;01FEB2013&amp;nbsp; &amp;nbsp; &amp;nbsp;31DEC2013&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;002&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;01MAR2013&amp;nbsp; &amp;nbsp; &amp;nbsp;31DC2013&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;003&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;01JAN2013&amp;nbsp; &amp;nbsp; &amp;nbsp;31DEC2013&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need the following output:&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Start&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Stop&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Start_flag&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;End_flag&lt;/P&gt;&lt;P&gt;&amp;nbsp;001&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;01JAN2013&amp;nbsp; &amp;nbsp; &amp;nbsp;31JAN2013&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;/P&gt;&lt;P&gt;&amp;nbsp;001&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;01FEB2013&amp;nbsp; &amp;nbsp; &amp;nbsp;31DEC2013&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3&lt;/P&gt;&lt;P&gt;&amp;nbsp;002&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;01MAR2013&amp;nbsp; &amp;nbsp; &amp;nbsp;31DC2013&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;/P&gt;&lt;P&gt;&amp;nbsp;003&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;01JAN2013&amp;nbsp; &amp;nbsp; &amp;nbsp;31DEC2013&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In other words I need to add a flag for the start and end with the exception that for consecutive periods the end flag of the previous period will become the start flag of the subsequent period and the remaining end flag will be increased by 1.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can anyone help me please?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thnk you in advance&lt;/P&gt;</description>
      <pubDate>Wed, 10 Aug 2022 13:28:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Add-and-update-flags-for-consecutive-dates/m-p/828087#M35379</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2022-08-10T13:28:52Z</dc:date>
    </item>
    <item>
      <title>Re: Add and update flags for consecutive dates</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Add-and-update-flags-for-consecutive-dates/m-p/828119#M35390</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by id;
  if first.id then start_flag=1;
  else start_flag+1;
  end_flag=start_flag+1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 10 Aug 2022 16:33:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Add-and-update-flags-for-consecutive-dates/m-p/828119#M35390</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-08-10T16:33:21Z</dc:date>
    </item>
    <item>
      <title>Re: Add and update flags for consecutive dates</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Add-and-update-flags-for-consecutive-dates/m-p/828121#M35391</link>
      <description>&lt;P&gt;You&amp;nbsp; need to provide a somewhat better example as I am not sure from your description what the result for the 3rd row of the modified ID 001 data below. Or Explicitly state that there will not be gaps (or overlaps) in the dates of the sequences.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;ID             Start                 Stop        
 001       01JAN2013     31JAN2013             
 001       01FEB2013     31DEC2013           
 001       01MAY2014     30JUN2014
 002       01MAR2013     31DC2013              
 003       01JAN2013     31DEC2013    &lt;/PRE&gt;
&lt;P&gt;What I understand to this point:&lt;/P&gt;
&lt;PRE&gt; /* assumes data is sorted by ID and start as implied*/

 data want;
   set have;
   by id;
   retain start_flag;
   ls = lag(stop);
   Difdate = start- ls;
   if first.id then do;
      start_flag=1;
      end_flag=2;
   end;
   else if difdate=1 then do;
      start_flag+1;
      end_flag = start_flag+1;
   end;
   Else do;
     /* this would be for gaps in the sequence*/
   end;
   drop ls difdate;
run;&lt;/PRE&gt;
&lt;P&gt;When you have By group processing in a data step SAS creates automatic variables that indicate whether a particular observation is the First or Last of the by group. These are 1/0 (True/False) variables that are accessed with the First. and Last. construct as shown. This allows setting things at breaks in the data such as your Id changes.&lt;/P&gt;
&lt;P&gt;The LAG function pulls a value from a preceding observation (if done properly, best not in IF type constructs). Retain keeps the value of a variable across data step iterations and is often used for a accumulator or sequencing variables like your Start_flag.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A style point for naming variables: If you have variables named Start and Stop and then Start_flag, it would make a lot more sense to have a Stop_flag then an End_flag for the variable name. Then the connection when you read the code a year later makes more sense.&lt;/P&gt;</description>
      <pubDate>Wed, 10 Aug 2022 16:45:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Add-and-update-flags-for-consecutive-dates/m-p/828121#M35391</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-08-10T16:45:22Z</dc:date>
    </item>
    <item>
      <title>Re: Add and update flags for consecutive dates</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Add-and-update-flags-for-consecutive-dates/m-p/828125#M35393</link>
      <description>&lt;P&gt;From your explanation and example, the start and stop dates are not used in determining the flags. Is that right?&lt;/P&gt;</description>
      <pubDate>Wed, 10 Aug 2022 17:16:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Add-and-update-flags-for-consecutive-dates/m-p/828125#M35393</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-08-10T17:16:19Z</dc:date>
    </item>
  </channel>
</rss>

