<?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: Do loop in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/Do-loop/m-p/651740#M19282</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/323613"&gt;@Mathis1&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Would this code give the desired output?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	set have;
	by PUIS_CLASS;
	retain FLAG -1;
	if first.PUIS_CLASS then FLAG + 1;
	if first.PUIS_CLASS then NEW = FLAG;
	NEW + 1;
	if NEW = 12 then NEW = 1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Best,&lt;/P&gt;</description>
    <pubDate>Fri, 29 May 2020 14:25:29 GMT</pubDate>
    <dc:creator>ed_sas_member</dc:creator>
    <dc:date>2020-05-29T14:25:29Z</dc:date>
    <item>
      <title>Do loop</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Do-loop/m-p/651715#M19278</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;I'm trying to generate a variable (named "NEW") that is set to i=1 when first.VAR is observed and then is incremented by one until last.VAR.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But i'd like that each time a new first.VAR is met, NEW is set to&amp;nbsp; i+1.&lt;/P&gt;
&lt;P&gt;And as a last requirement, i'd like that when NEW=12, then NEW+1 is set back to 1.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I tried something like this (In the attached table VAR is named PUIS_CLASS) :&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WAANT;
Set HAVE;
Retain NEW;
	Do i=1 to 12;
	By PUIS_CLASS;
	if first.PUIS_CLASS then NEW=i;
	else NEW=i+1;
	if last.PUIS_CLASS then output;
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But it's not the solution...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you again for your help &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 29 May 2020 13:42:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Do-loop/m-p/651715#M19278</guid>
      <dc:creator>Mathis1</dc:creator>
      <dc:date>2020-05-29T13:42:01Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Do-loop/m-p/651719#M19279</link>
      <description>&lt;P&gt;Either just test the value of NEW to see if it has gotten too large.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set have;
  by puis_class;
  new+1;
  if first.puis_class or (new &amp;gt; 12) then new=1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or put the SET statement inside the DO loop.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  do new=1 to 12 until (last.puis_class);
    set have;
    by puis_class;
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 29 May 2020 13:51:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Do-loop/m-p/651719#M19279</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-05-29T13:51:52Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Do-loop/m-p/651725#M19280</link>
      <description>&lt;P&gt;Thank you for your response.&lt;/P&gt;
&lt;P&gt;the problem is, I would like that when the first PUIS_CLASS="10_K" is met, New=2 and not 1. And when the first&amp;nbsp;PUIS_CLASS="10_N"&amp;nbsp; is observed, New=3 etc...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 29 May 2020 14:02:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Do-loop/m-p/651725#M19280</guid>
      <dc:creator>Mathis1</dc:creator>
      <dc:date>2020-05-29T14:02:54Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Do-loop/m-p/651728#M19281</link>
      <description>&lt;P&gt;Example of the desired output for the given input.&lt;/P&gt;</description>
      <pubDate>Fri, 29 May 2020 14:05:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Do-loop/m-p/651728#M19281</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-05-29T14:05:32Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Do-loop/m-p/651740#M19282</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/323613"&gt;@Mathis1&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Would this code give the desired output?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	set have;
	by PUIS_CLASS;
	retain FLAG -1;
	if first.PUIS_CLASS then FLAG + 1;
	if first.PUIS_CLASS then NEW = FLAG;
	NEW + 1;
	if NEW = 12 then NEW = 1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Best,&lt;/P&gt;</description>
      <pubDate>Fri, 29 May 2020 14:25:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Do-loop/m-p/651740#M19282</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2020-05-29T14:25:29Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Do-loop/m-p/651742#M19283</link>
      <description>&lt;P&gt;It works perfectly, thank you again &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;NB : for what i want exactly, it's :&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;if NEW = 13 then NEW = 1;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;instead of NEW=12.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 29 May 2020 14:33:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Do-loop/m-p/651742#M19283</guid>
      <dc:creator>Mathis1</dc:creator>
      <dc:date>2020-05-29T14:33:38Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Do-loop/m-p/651760#M19284</link>
      <description>&lt;P&gt;Little rectification, in order for the code to work properly when FLAG exceeds 12, the code should be :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	set FORUM.HAVE;
	by PUIS_CLASS;
	retain FLAG -1;
	if first.PUIS_CLASS then FLAG + 1;
	&lt;U&gt;&lt;STRONG&gt;If FLAG = 13 then FLAG = 0;&lt;/STRONG&gt;&lt;/U&gt;
	if first.PUIS_CLASS then NEW = FLAG;
	NEW+1;
	if NEW = 13 then NEW = 1;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 29 May 2020 15:23:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Do-loop/m-p/651760#M19284</guid>
      <dc:creator>Mathis1</dc:creator>
      <dc:date>2020-05-29T15:23:13Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Do-loop/m-p/651766#M19285</link>
      <description>&lt;P&gt;So you want to create a sequence variable that starts at 1 for the first by group and 2 for the second by group?&lt;/P&gt;
&lt;P&gt;Like this?&lt;/P&gt;
&lt;PRE&gt;1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 ...
2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 ...
3 4 5 6 7 8 9 10 11 12 1 2 3 4 ...
4 5 6 7 8 9 10 11 12 1 2 3 4 ...
5 6 7 8 9 10 11 12 1 2 3 4 ...&lt;/PRE&gt;
&lt;P&gt;If so then your current algorithm is breaking down after the 13th group.&lt;/P&gt;
&lt;P&gt;From your sample data:&lt;/P&gt;
&lt;PRE&gt;10_J --&amp;gt; 1 2 3 4 5 6 7 8 9 10 11 12
10_K --&amp;gt; 2 3 4 5 6 7 8 9 10 11 12 1
10_N --&amp;gt; 3 4 5 6 7 8 9 10 11 12 1 2
10_O --&amp;gt; 4 5 6 7 8 9 10 11 12 1 2 3
10_P --&amp;gt; 5 6 7 8 9 10 11 12 1 2 3 4
10_Q --&amp;gt; 6 7 8 9 10 11 12 1 2 3 4 5
10_R --&amp;gt; 7 8 9 10 11 12 1 2 3 4 5 6
10_S --&amp;gt; 8 9 10 11 12 1 2 3 4 5 6 7
11_M --&amp;gt; 9 10 11 12 1 2 3 4 5 6 7 8
11_N --&amp;gt; 10 11 12 1 2 3 4 5 6 7 8 9
11_O --&amp;gt; 11 12 1 2 3 4 5 6 7 8 9 10
11_P --&amp;gt; 12 1 2 3 4 5 6 7 8 9 10 11
11_Q --&amp;gt; 1 2 3 4 5 6 7 8 9 10 11 12
11_R --&amp;gt; 1 2 3 4 5 6 7 8 9 10 11 12
&lt;/PRE&gt;
&lt;P&gt;Notice how "11_R" is repeating the same sequence as "11_Q".&lt;/P&gt;
&lt;P&gt;Try this code instead that will count the groups and the rows within the groups and then generate NEW using modulo (remainder) division.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want2;
  do seq=0 by 1 until (last.puis_class);
    set have;
    by PUIS_CLASS;
    new = 1+mod(group+seq,12);
    output;
  end;
  group+1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 29 May 2020 16:15:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Do-loop/m-p/651766#M19285</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-05-29T16:15:17Z</dc:date>
    </item>
  </channel>
</rss>

