<?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: Create flag for next record based on condition in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-flag-for-next-record-based-on-condition/m-p/736529#M229456</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/102730"&gt;@Abishekaa&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;This is my sample dataset:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PtID visit Flag&lt;/P&gt;
&lt;P&gt;001&amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;
&lt;P&gt;001&amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/P&gt;
&lt;P&gt;001&amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/P&gt;
&lt;P&gt;002&amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;
&lt;P&gt;002&amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For each PtID, if the first.Flag = 0, I want to Flag the next visit. This is the dataset I try to create:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PtID visit Flag&lt;/P&gt;
&lt;P&gt;001&amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;001&amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;001&amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/P&gt;
&lt;P&gt;002&amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;
&lt;P&gt;002&amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Do you really mean "first.flag=0", or (as I suspect) you mean the first obs for a given ID has flag=0.&amp;nbsp; &amp;nbsp;If it's the latter, then:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
&amp;nbsp; set have;
&amp;nbsp; by ptid;
&amp;nbsp; if first.ptid=0 and lag(first.ptid)=1 and lag(flag)=0 then flag=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The if statement tests for&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;The current obs is the second for a given ID -- &lt;EM&gt;&lt;STRONG&gt;first.ptid=0 and lag(first.ptid)=1&lt;/STRONG&gt;&lt;/EM&gt;.&amp;nbsp; (or you could replace those 2 conditions with&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;STRONG&gt;&lt;EM&gt;dif(first.ptid)=-1&lt;/EM&gt;&lt;/STRONG&gt;&amp;nbsp; (dif(x) is defined as x-lag(x).&lt;/LI&gt;
&lt;LI&gt;The immediately preceding flag is zero&amp;nbsp; --&amp;nbsp; &lt;EM&gt;&lt;STRONG&gt;lag(flag)=0&lt;/STRONG&gt;&lt;/EM&gt;&lt;/LI&gt;
&lt;/OL&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>Fri, 23 Apr 2021 00:33:37 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2021-04-23T00:33:37Z</dc:date>
    <item>
      <title>Create flag for next record based on condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-flag-for-next-record-based-on-condition/m-p/736496#M229440</link>
      <description>&lt;P&gt;This is my sample dataset:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PtID visit Flag&lt;/P&gt;&lt;P&gt;001&amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;&lt;P&gt;001&amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/P&gt;&lt;P&gt;001&amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/P&gt;&lt;P&gt;002&amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;002&amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For each PtID, if the first.Flag = 0, I want to Flag the next visit. This is the dataset I try to create:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PtID visit Flag&lt;/P&gt;&lt;P&gt;001&amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;001&amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;001&amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/P&gt;&lt;P&gt;002&amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;002&amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 22 Apr 2021 19:01:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-flag-for-next-record-based-on-condition/m-p/736496#M229440</guid>
      <dc:creator>Abishekaa</dc:creator>
      <dc:date>2021-04-22T19:01:27Z</dc:date>
    </item>
    <item>
      <title>Re: Create flag for next record based on condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-flag-for-next-record-based-on-condition/m-p/736497#M229441</link>
      <description>&lt;P&gt;Are the visits always so nicely numbered (1,2,3,....)?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have ;
  if visit=2 and lag(flag)=0 then flag=1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or not?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have ;
  by ptid ;
  if first.ptid then lag_flag=flag;
  else do;
    if lag_flag=0 then flag=1;
    lag_flag=.;
  end;
  retain lag_flag ;
  drop lag_flag;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 22 Apr 2021 19:14:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-flag-for-next-record-based-on-condition/m-p/736497#M229441</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-04-22T19:14:19Z</dc:date>
    </item>
    <item>
      <title>Re: Create flag for next record based on condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-flag-for-next-record-based-on-condition/m-p/736529#M229456</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/102730"&gt;@Abishekaa&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;This is my sample dataset:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PtID visit Flag&lt;/P&gt;
&lt;P&gt;001&amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;
&lt;P&gt;001&amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/P&gt;
&lt;P&gt;001&amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/P&gt;
&lt;P&gt;002&amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;
&lt;P&gt;002&amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For each PtID, if the first.Flag = 0, I want to Flag the next visit. This is the dataset I try to create:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PtID visit Flag&lt;/P&gt;
&lt;P&gt;001&amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;001&amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;001&amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/P&gt;
&lt;P&gt;002&amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;
&lt;P&gt;002&amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Do you really mean "first.flag=0", or (as I suspect) you mean the first obs for a given ID has flag=0.&amp;nbsp; &amp;nbsp;If it's the latter, then:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
&amp;nbsp; set have;
&amp;nbsp; by ptid;
&amp;nbsp; if first.ptid=0 and lag(first.ptid)=1 and lag(flag)=0 then flag=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The if statement tests for&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;The current obs is the second for a given ID -- &lt;EM&gt;&lt;STRONG&gt;first.ptid=0 and lag(first.ptid)=1&lt;/STRONG&gt;&lt;/EM&gt;.&amp;nbsp; (or you could replace those 2 conditions with&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;STRONG&gt;&lt;EM&gt;dif(first.ptid)=-1&lt;/EM&gt;&lt;/STRONG&gt;&amp;nbsp; (dif(x) is defined as x-lag(x).&lt;/LI&gt;
&lt;LI&gt;The immediately preceding flag is zero&amp;nbsp; --&amp;nbsp; &lt;EM&gt;&lt;STRONG&gt;lag(flag)=0&lt;/STRONG&gt;&lt;/EM&gt;&lt;/LI&gt;
&lt;/OL&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>Fri, 23 Apr 2021 00:33:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-flag-for-next-record-based-on-condition/m-p/736529#M229456</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-04-23T00:33:37Z</dc:date>
    </item>
    <item>
      <title>Re: Create flag for next record based on condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-flag-for-next-record-based-on-condition/m-p/737780#M230039</link>
      <description>&lt;P&gt;This works perfectly! Thank you so much.&amp;nbsp; I didn't realize the lag function could be used to fully solve this problem &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Apr 2021 01:05:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-flag-for-next-record-based-on-condition/m-p/737780#M230039</guid>
      <dc:creator>Abishekaa</dc:creator>
      <dc:date>2021-04-29T01:05:05Z</dc:date>
    </item>
  </channel>
</rss>

