<?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: Need Help for Sequence Loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Need-Help-for-Sequence-Loop/m-p/691199#M210342</link>
    <description>&lt;P&gt;I did this using a RETAIN statement, although I'm sure there are other ways.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    set example;
    retain new_sequence;
    by accno;
    if first.accno and amt=0 then new_sequence=.;
    if first.accno and amt&amp;gt;0 then new_sequence=old_sequence;
    if not first.accno and not flag_to_reset then new_sequence=sum(new_sequence,1);  
    if flag_to_reset then new_sequence=1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 13 Oct 2020 10:53:44 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2020-10-13T10:53:44Z</dc:date>
    <item>
      <title>Need Help for Sequence Loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-Help-for-Sequence-Loop/m-p/690948#M210234</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Would require assistance to achieve the following loop to create a NEW sequence column based on a flag.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please find screenshot for reference. For each account number, I would like to RETAIN the first sequence. Meaning for accno 900, first sequence to retain is 40. For acc 991, first sequence to retain is 20. From there, it will add an incremental of +1 all the way until it hits a flag for reset with the value of 1. If it hits a value of 1 in the flag, it will reset the sequence and it will start from 1 and then add an incremental of +1 subsequently.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="sas_newbie94_0-1602505576570.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/50560i822E4E1B4CF14F72/image-size/medium?v=v2&amp;amp;px=400" role="button" title="sas_newbie94_0-1602505576570.png" alt="sas_newbie94_0-1602505576570.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data example;
input accno old_sequence flag_to_reset new_sequence;
datalines;
900 40   40
900 41   41
900 42   42
900 43   43
900 44 1 1
900 45   2
900 46   3
900 47   4
900 48   5
991 20   20
991 21   21
991 22 1 1
991 23   2
991 24   3
991 25   4
991 26   5

;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Oct 2020 12:30:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-Help-for-Sequence-Loop/m-p/690948#M210234</guid>
      <dc:creator>sas_newbie94</dc:creator>
      <dc:date>2020-10-12T12:30:31Z</dc:date>
    </item>
    <item>
      <title>Re: Need Help for Sequence Loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-Help-for-Sequence-Loop/m-p/690955#M210241</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data example;
input accno old_sequence flag_to_reset ;
datalines;
900 40 .  40
900 41 .  41
900 42  . 42
900 43  . 43
900 44 1 1
900 45 .  2
900 46 .  3
900 47 .  4
900 48 .  5
991 20 .  20
991 21 .  21
991 22 1 1
991 23  . 2
991 24  . 3
991 25  . 4
991 26  . 5
;
run;
data want;
 set example;
 by accno;
 if first.accno then new_sequence=old_sequence-1;
 if flag_to_reset then new_sequence=0;
 new_sequence+1;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 12 Oct 2020 12:49:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-Help-for-Sequence-Loop/m-p/690955#M210241</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-10-12T12:49:55Z</dc:date>
    </item>
    <item>
      <title>Re: Need Help for Sequence Loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-Help-for-Sequence-Loop/m-p/690957#M210243</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    set example;
    by accno;
    if first.accno then new_sequence=old_sequence;
    if not first.accno and not flag_to_reset then new_sequence+1;
    if flag_to_reset then new_sequence=1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 12 Oct 2020 12:53:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-Help-for-Sequence-Loop/m-p/690957#M210243</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-10-12T12:53:42Z</dc:date>
    </item>
    <item>
      <title>Re: Need Help for Sequence Loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-Help-for-Sequence-Loop/m-p/691117#M210309</link>
      <description>&lt;P&gt;Hi both&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for the replies. Both codes worked!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However I have one more additional criteria that I need to look out for when recalculating the sequence.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If an account number does not have an AMT or AMT that is showing 0, we &lt;STRONG&gt;do not&lt;/STRONG&gt; use back the old sequence column despite it being the FIRST.ACCNO. Pls refer to accno 900 for this scenario. Instead, at the point where there's an AMT we will have to recalculate the new_sequence and start from 1. Initially I thought I can omit AMT = 0 entirely from the data set, but take note that at subsequent rows if there's 0 AMT, we still retain the new_sequence count from above. Pls refer to highlighted in red text for 0 Amt for accno 900. It will still continue counting. ONLY at the starting point for each Accno, we need to check for the AMT value whether it's showing 0. If it's 0, we need to start counting at the instance when there's a AMT value.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For accno 991, we can use back the old sequence since it's the FIRST.ACCNO and there's an $ AMT to that row, so we can use back the old sequence and start flagging from there.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Prior checks for "Flag to reset" condition still exist.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="sas_newbie94_0-1602552963369.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/50583i8AE7CED94387FABE/image-size/medium?v=v2&amp;amp;px=400" role="button" title="sas_newbie94_0-1602552963369.png" alt="sas_newbie94_0-1602552963369.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data example;
input accno old_sequence flag_to_reset AMT ;
datalines;
900 40 . 0
900 41 . 0 
900 42  . 100
900 43  . 100
900 44 1 100
900 45 . 100
900 46 . 100
900 47 . 0 
900 48 . 0
991 20 . 100
991 21 . 100
991 22 1 100
991 23  . 100
991 24  . 100
991 25  . 100
991 26  . 100
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 13 Oct 2020 01:59:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-Help-for-Sequence-Loop/m-p/691117#M210309</guid>
      <dc:creator>sas_newbie94</dc:creator>
      <dc:date>2020-10-13T01:59:31Z</dc:date>
    </item>
    <item>
      <title>Re: Need Help for Sequence Loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-Help-for-Sequence-Loop/m-p/691199#M210342</link>
      <description>&lt;P&gt;I did this using a RETAIN statement, although I'm sure there are other ways.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    set example;
    retain new_sequence;
    by accno;
    if first.accno and amt=0 then new_sequence=.;
    if first.accno and amt&amp;gt;0 then new_sequence=old_sequence;
    if not first.accno and not flag_to_reset then new_sequence=sum(new_sequence,1);  
    if flag_to_reset then new_sequence=1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 13 Oct 2020 10:53:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-Help-for-Sequence-Loop/m-p/691199#M210342</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-10-13T10:53:44Z</dc:date>
    </item>
    <item>
      <title>Re: Need Help for Sequence Loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-Help-for-Sequence-Loop/m-p/691480#M210472</link>
      <description>&lt;PRE&gt;data example;
input accno old_sequence flag_to_reset AMT ;
datalines;
900 40 . 0
900 41 . 0 
900 42  . 100
900 43  . 100
900 44 1 100
900 45 . 100
900 46 . 100
900 47 . 0 
900 48 . 0
991 20 . 100
991 21 . 100
991 22 1 100
991 23  . 100
991 24  . 100
991 25  . 100
991 26  . 100
;
run;
data want;
 set example;
 by accno;
 retain flag ;
 if first.accno then do;
   new_sequence=old_sequence-1;
   if amt=0 then flag=1;
 end;
 if amt ne 0 then flag=0;

 if flag_to_reset then new_sequence=0;
 new_sequence+1;
 if flag then  new_sequence=.;
run;&lt;/PRE&gt;</description>
      <pubDate>Wed, 14 Oct 2020 09:25:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-Help-for-Sequence-Loop/m-p/691480#M210472</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-10-14T09:25:30Z</dc:date>
    </item>
  </channel>
</rss>

