<?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 New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Do-loop/m-p/606903#M17464</link>
    <description>&lt;P&gt;Suppose you have&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;a dataset HAVE with one observation per ID, containing ID and value (V),&lt;/LI&gt;
&lt;LI&gt;a dataset INCR with one observation per ID, containing ID and number of incrementations (N),&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;both sorted by ID.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have(drop=n) incr(drop=v);
input id v n;
cards;
1 855 2
2 101 0
3 998 4
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then you can merge the two datasets by ID and in the same DATA step create the additional observations with the incremented V values for each ID:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(drop=n);
merge have incr;
by id;
output;
do n=1 to n;
  v=mod(v,1000)+1;
  output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;id       v

 1     855
&lt;FONT color="#33CCCC"&gt;&lt;STRONG&gt; 1     856
 1     857&lt;/STRONG&gt;&lt;/FONT&gt;
 2     101
 3     998
&lt;STRONG&gt;&lt;FONT color="#33CCCC"&gt; 3     999
 3    1000
 3       1
 3       2
&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/PRE&gt;
&lt;P&gt;(Highlighted observations were newly inserted.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the number of incrementations (N) was already contained in dataset HAVE (rather than in a separate dataset), a simple&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;set have;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;would replace the MERGE and BY statements and HAVE would not need to be sorted by ID.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To obtain a dataset consisting of the newly created observations alone, just delete the first of the two OUTPUT statements.&lt;/P&gt;</description>
    <pubDate>Mon, 25 Nov 2019 09:24:59 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2019-11-25T09:24:59Z</dc:date>
    <item>
      <title>Do loop</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Do-loop/m-p/603902#M16999</link>
      <description>&lt;P&gt;Hi Guys,&lt;/P&gt;&lt;P&gt;Need help on do loop.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Got a scenario where the values are assigned between 1 to 1000 for each id, if the previous/last value assigned was 855&amp;nbsp; and if I want to have 2 incremental then, need to assign 856,857 etc (incrementally). Similarly, if the last value assigned was 998 and want to have 4 increments then need to assign as 999,1000, then go back to 1,2 etc.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How do i dealt this situation&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Karthik&lt;/P&gt;</description>
      <pubDate>Wed, 13 Nov 2019 16:25:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Do-loop/m-p/603902#M16999</guid>
      <dc:creator>meckarthik</dc:creator>
      <dc:date>2019-11-13T16:25:35Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Do-loop/m-p/603907#M17000</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/275095"&gt;@meckarthik&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can increment the values using the MOD function:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;v=mod(v,1000)+1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
v=998; /* start value */
n=4;   /* number of incrementations */
do i=1 to n;
  v=mod(v,1000)+1;
  put v=;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;v=999
v=1000
v=1
v=2&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Nov 2019 17:17:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Do-loop/m-p/603907#M17000</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-11-13T17:17:55Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Do-loop/m-p/606878#M17463</link>
      <description>&lt;P&gt;Thank you so much. It was really helpful. Just one more to that, how do I create a temp table or insert to the table to capture all the 'v'&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Nov 2019 06:05:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Do-loop/m-p/606878#M17463</guid>
      <dc:creator>meckarthik</dc:creator>
      <dc:date>2019-11-25T06:05:43Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Do-loop/m-p/606903#M17464</link>
      <description>&lt;P&gt;Suppose you have&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;a dataset HAVE with one observation per ID, containing ID and value (V),&lt;/LI&gt;
&lt;LI&gt;a dataset INCR with one observation per ID, containing ID and number of incrementations (N),&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;both sorted by ID.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have(drop=n) incr(drop=v);
input id v n;
cards;
1 855 2
2 101 0
3 998 4
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then you can merge the two datasets by ID and in the same DATA step create the additional observations with the incremented V values for each ID:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(drop=n);
merge have incr;
by id;
output;
do n=1 to n;
  v=mod(v,1000)+1;
  output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;id       v

 1     855
&lt;FONT color="#33CCCC"&gt;&lt;STRONG&gt; 1     856
 1     857&lt;/STRONG&gt;&lt;/FONT&gt;
 2     101
 3     998
&lt;STRONG&gt;&lt;FONT color="#33CCCC"&gt; 3     999
 3    1000
 3       1
 3       2
&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/PRE&gt;
&lt;P&gt;(Highlighted observations were newly inserted.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the number of incrementations (N) was already contained in dataset HAVE (rather than in a separate dataset), a simple&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;set have;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;would replace the MERGE and BY statements and HAVE would not need to be sorted by ID.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To obtain a dataset consisting of the newly created observations alone, just delete the first of the two OUTPUT statements.&lt;/P&gt;</description>
      <pubDate>Mon, 25 Nov 2019 09:24:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Do-loop/m-p/606903#M17464</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-11-25T09:24:59Z</dc:date>
    </item>
  </channel>
</rss>

