<?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 and mod fuction in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-and-mod-fuction/m-p/561264#M157091</link>
    <description>&lt;P&gt;What problem are you having? It works for me, except that you get multiple hits on the same number, but you just have to add else to the if cases.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	Do K=4 to 300;
		if mod(k,2)=0 then output;
		else If mod(k,3)=0 then output;
		else if mod(k,5)=0 then output;
		else if mod(k,7)=0 then output;
	End;
run;

Proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;If it is actually prime number (and not just as an exercise) that you are after, I found this code:&amp;nbsp;&lt;A href="http://www.globalstatements.com/shortcuts/88a.html" target="_blank"&gt;http://www.globalstatements.com/shortcuts/88a.html&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 24 May 2019 00:48:07 GMT</pubDate>
    <dc:creator>heffo</dc:creator>
    <dc:date>2019-05-24T00:48:07Z</dc:date>
    <item>
      <title>Do loop and mod fuction</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-and-mod-fuction/m-p/561261#M157089</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've been trying to write a code to answer this practice question in SAS but i haven't had success.&lt;/P&gt;&lt;P&gt;Use a do loop, the mod function and an if-statement to put out all prime numbers between 4 and 300.&amp;nbsp; Consider a prime number to be any number greater than 3 that is not divisible by 2, 3, 5 or 7.&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here are my codes&lt;/P&gt;&lt;P&gt;data one;&lt;BR /&gt;Do K=4 to 300;&lt;BR /&gt;if mod(k,2)=0 then output;&lt;BR /&gt;If mod(k,3)=0 then output;&lt;BR /&gt;if mod(k,5)=0 then output;&lt;BR /&gt;if mod(k,7)=0 then output;&lt;BR /&gt;End;&lt;BR /&gt;Proc print; run;&lt;/P&gt;&lt;P&gt;I had a problem in the results. Please can anyone review these to let me know how best i can modify or get the prime numbers between 4 and 300.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Fri, 24 May 2019 00:32:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-and-mod-fuction/m-p/561261#M157089</guid>
      <dc:creator>ChuksManuel</dc:creator>
      <dc:date>2019-05-24T00:32:07Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop and mod fuction</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-and-mod-fuction/m-p/561264#M157091</link>
      <description>&lt;P&gt;What problem are you having? It works for me, except that you get multiple hits on the same number, but you just have to add else to the if cases.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	Do K=4 to 300;
		if mod(k,2)=0 then output;
		else If mod(k,3)=0 then output;
		else if mod(k,5)=0 then output;
		else if mod(k,7)=0 then output;
	End;
run;

Proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;If it is actually prime number (and not just as an exercise) that you are after, I found this code:&amp;nbsp;&lt;A href="http://www.globalstatements.com/shortcuts/88a.html" target="_blank"&gt;http://www.globalstatements.com/shortcuts/88a.html&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 24 May 2019 00:48:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-and-mod-fuction/m-p/561264#M157091</guid>
      <dc:creator>heffo</dc:creator>
      <dc:date>2019-05-24T00:48:07Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop and mod fuction</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-and-mod-fuction/m-p/561266#M157093</link>
      <description>You overlooked the word NOT:&lt;BR /&gt;&lt;BR /&gt;"not divisible by 2, 3, 5, or 7."</description>
      <pubDate>Fri, 24 May 2019 01:06:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-and-mod-fuction/m-p/561266#M157093</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-05-24T01:06:44Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop and mod fuction</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-and-mod-fuction/m-p/561267#M157094</link>
      <description>&lt;P&gt;Thanks.&lt;/P&gt;&lt;P&gt;I was trying to get the prime numbers between 4 and 300. The code wasn't giving me that.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 24 May 2019 01:09:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-and-mod-fuction/m-p/561267#M157094</guid>
      <dc:creator>ChuksManuel</dc:creator>
      <dc:date>2019-05-24T01:09:03Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop and mod fuction</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-and-mod-fuction/m-p/561268#M157095</link>
      <description>&lt;P&gt;That's the part i've been struggling with.&lt;/P&gt;&lt;P&gt;Any help is appreciated.&lt;/P&gt;</description>
      <pubDate>Fri, 24 May 2019 01:09:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-and-mod-fuction/m-p/561268#M157095</guid>
      <dc:creator>ChuksManuel</dc:creator>
      <dc:date>2019-05-24T01:09:50Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop and mod fuction</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-and-mod-fuction/m-p/561272#M157096</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/236266"&gt;@ChuksManuel&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here one way to get the logic right. You could now try to also write a second loop for your list of prime numbers instead of spelling out everything using AND conditions.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  do K=4 to 300;
    if mod(k,2) ne 0 
      and mod(k,3) ne 0 
      and mod(k,5) ne 0 
      and mod(k,7) ne 0 then
      output;
  end;
run;

proc print data=want;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And here a more general approach which returns actual prime numbers up to the &amp;amp;upper_bound you define.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let upper_bound=300;
data _null_;
  length prim_num 8;
  call missing(prim_num);
  dcl hash h1(ordered:'y');
  dcl hiter iter1('h1');
  h1.defineKey('prim_num');
  h1.defineData('prim_num');
  h1.defineDone();

  h1.add(key:2,data:2);
  h1.add(key:3,data:3);

  /* loop only over odd numbers */
  do k=5 to &amp;amp;upper_bound by 2;
    prim_flg='1';
    rc=iter1.first();
    do while(rc eq 0);
      if mod(k,prim_num)=0 then 
        do;
          /* K not a prime number: Try next value of K */
          prim_flg='0';
          leave;
        end;
      rc=iter1.next();
    end;
    /* add new prime number to hash */
    if prim_flg='1' then h1.add(key:k,data:k);
  end;

  /* write list of found prime numbers to data set WANT */
  h1.output(dataset:'want');
  stop;
run;

proc print data=want;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 24 May 2019 01:51:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-and-mod-fuction/m-p/561272#M157096</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-05-24T01:51:59Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop and mod fuction</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-and-mod-fuction/m-p/561274#M157098</link>
      <description>&lt;P&gt;Thank you that worked.&lt;/P&gt;</description>
      <pubDate>Fri, 24 May 2019 01:46:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-and-mod-fuction/m-p/561274#M157098</guid>
      <dc:creator>ChuksManuel</dc:creator>
      <dc:date>2019-05-24T01:46:44Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop and mod fuction</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-and-mod-fuction/m-p/561277#M157101</link>
      <description>&lt;P&gt;Hash is both more complex and takes more storage than as simple array would.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let max_primes=1000;
%let max_number=300;
data want;
  array primes (&amp;amp;max_primes) _temporary_;
  do prime=2,3 by 2 to &amp;amp;max_number until(last&amp;gt;=dim(primes));
    notprime=0;
    do i=1 to last while(not notprime);
      notprime= not mod(prime,primes[i]) ;
    end;
    if not notprime then do;
      last+1;
      primes[last]=prime;
      output;
      put prime @;
    end;
  end;
  keep prime;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101
103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193
197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293

NOTE: The data set WORK.WANT has 62 observations and 1 variables.&lt;/PRE&gt;</description>
      <pubDate>Fri, 24 May 2019 02:08:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-and-mod-fuction/m-p/561277#M157101</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-05-24T02:08:08Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop and mod fuction</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-and-mod-fuction/m-p/561311#M157115</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;"&lt;SPAN&gt;Hash is both more complex and takes more storage than as simple array would&lt;/SPAN&gt;"&lt;/P&gt;
&lt;P&gt;More complex: In my opinion not really once you get the hang of it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Unlike an array is flexible and doesn't need the elements predefined so also not sure that the memory argument is really true once the numbers get bigger. So yes, the hash requires some initial memory just to define the structure but once done it then only requires more memory for actually identified primary numbers.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 24 May 2019 03:54:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-and-mod-fuction/m-p/561311#M157115</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-05-24T03:54:25Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop and mod fuction</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-and-mod-fuction/m-p/561335#M157124</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt;: Nice application of the hash object.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A minor change improves the performance substantially (for larger values of &lt;FONT face="courier new,courier"&gt;upper_bound&lt;/FONT&gt;&lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;
&lt;PRE&gt;do while(rc eq 0 &lt;STRONG&gt;&lt;FONT color="#008000"&gt;&amp;amp; prim_num**2&amp;lt;=k&lt;/FONT&gt;&lt;/STRONG&gt;);&lt;/PRE&gt;
&lt;P&gt;Run times on my computer: 203 s --&amp;gt; 1 s for&amp;nbsp;&lt;FONT face="courier new,courier"&gt;upper_bound=1000000&lt;/FONT&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Similarly for&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;'s solution, which is then even faster (run time 0.5 s in the above example):&lt;/P&gt;
&lt;PRE&gt;do i=1 to last while(not notprime &lt;STRONG&gt;&lt;FONT color="#008000"&gt;&amp;amp; primes[i]**2&amp;lt;=prime&lt;/FONT&gt;&lt;/STRONG&gt;);&lt;/PRE&gt;</description>
      <pubDate>Fri, 24 May 2019 08:31:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-and-mod-fuction/m-p/561335#M157124</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-05-24T08:31:02Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop and mod fuction</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-and-mod-fuction/m-p/561362#M157136</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Nice!&lt;/P&gt;
&lt;P&gt;I get it that we don't have to test with all already found primary numbers. I don't get it (mathematically) why it's&amp;nbsp;&lt;EM&gt;prim_num**2&amp;lt;=k&lt;/EM&gt; . Can you please enlighten me?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 24 May 2019 12:27:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-and-mod-fuction/m-p/561362#M157136</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-05-24T12:27:06Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop and mod fuction</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-and-mod-fuction/m-p/561374#M157140</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Nice!&lt;/P&gt;
&lt;P&gt;I get it that we don't have to test with all already found primary numbers. I don't get it (mathematically) why it's&amp;nbsp;&lt;EM&gt;prim_num**2&amp;lt;=k&lt;/EM&gt; . Can you please enlighten me?&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You do not need to test whether the current number can be divided by a prime that is larger than its square root.&amp;nbsp; If said prime could divide the number being tested the result would have been a prime number less than the square root.&amp;nbsp; But you already tested all of those primes.&lt;/P&gt;</description>
      <pubDate>Fri, 24 May 2019 12:52:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-and-mod-fuction/m-p/561374#M157140</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-05-24T12:52:56Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop and mod fuction</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-and-mod-fuction/m-p/561375#M157141</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I do understand the consequences of the formula BUT I don't understand its reason.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;"&lt;EM&gt;If said prime could divide the number being tested the result would have been a prime number less than the square root.&lt;/EM&gt;"&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Oh! Yes, of course. I had to read this like 10 times but I got it now. Thanks!&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 24 May 2019 13:07:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-and-mod-fuction/m-p/561375#M157141</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-05-24T13:07:48Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop and mod fuction</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-and-mod-fuction/m-p/561376#M157142</link>
      <description>&lt;P&gt;The fastest algorithm is Prime Number Sieve :&lt;/P&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;
&lt;PRE&gt;data want;
array x{300} _temporary_;
do i=1 to 300;
  x{i}=i;
end;

do i=2 to int(sqrt(300));
  if not missing(x{i}) then do;
     do j=i+1 to dim(x);
       if not missing(x{j}) then do; if mod(x{j},x{i})=0 then call missing(x{j});end; 
     end;
  end;
end;

do k=4 to 300;
  if not missing(x{k}) then do;prime_number=x{k};output;end;
end;
keep prime_number;
run;&lt;/PRE&gt;</description>
      <pubDate>Fri, 24 May 2019 13:12:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-and-mod-fuction/m-p/561376#M157142</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-05-24T13:12:13Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop and mod fuction</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-and-mod-fuction/m-p/561403#M157155</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;. More precisely: "...&amp;nbsp;&lt;SPAN&gt;the result would have been &lt;STRONG&gt;&lt;FONT color="#008000"&gt;divisible by&lt;/FONT&gt;&lt;/STRONG&gt; a prime number less than the square root. (...)".&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt; Example: sqrt(6853)=82.7..., hence it's sufficient to divide by the primes up to 79 (next prime 83 is already greater than that square root). We don't need to find out that mod(6853, 89)=0 in order to prove that 6853 is not prime, because 6853/89=77 must necessarily have prime factors &amp;lt;=77&amp;lt;=sqrt(6853), indeed: 7 and 11. So, we got this information already when we saw mod(6853, 7)=0.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt;: My first idea was to write &lt;FONT face="courier new,courier"&gt;prim_num&amp;lt;=sqrt(k)&lt;/FONT&gt;, but I figured that the equivalent criterion &lt;FONT face="courier new,courier"&gt;prim_num**2&amp;lt;=k&lt;/FONT&gt; (or even &lt;FONT face="courier new,courier"&gt;prim_num*prim_num&amp;lt;=k&lt;/FONT&gt;) can be evaluated slightly faster as it involves only integer multiplication rather than the computationally more expensive SQRT function.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 24 May 2019 14:12:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-and-mod-fuction/m-p/561403#M157155</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-05-24T14:12:26Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop and mod fuction</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-and-mod-fuction/m-p/561450#M157172</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;for chiming in. Yes, given that it's one of the oldest algorithms (Eratosthenes, 3rd century B.C.), it's stunningly fast.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think your current implementation is somewhat below its best, though: The idea of setting every x{i}-th array element to missing could be implemented using a DO ... BY statement rather than calling the MOD function to identify the elements to be canceled. This change reduces the run time for the primes up to 1 million from about 2 to 0.1 s (on my computer).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The version below takes only 6 seconds for the 5,761,455 primes up to &lt;EM&gt;100 million&lt;/EM&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%let max=100000000;

data want;
array x{&amp;amp;max} _temporary_;
do i=1 to &amp;amp;max;
  x{i}=i;
end;

do i=2 to int(sqrt(&amp;amp;max));
  if not missing(x{i}) then do;
     do j=&lt;FONT color="#008000"&gt;&lt;STRONG&gt;i*i&lt;/STRONG&gt;&lt;/FONT&gt; to dim(x) &lt;STRONG&gt;&lt;FONT color="#008000"&gt;by x{i}&lt;/FONT&gt;&lt;/STRONG&gt;;
       &lt;STRONG&gt;&lt;FONT color="#008000"&gt;x{j}=.&lt;/FONT&gt;&lt;/STRONG&gt;;
     end;
  end;
end;

do k=&lt;STRONG&gt;&lt;FONT color="#008000"&gt;2&lt;/FONT&gt;&lt;/STRONG&gt; to &amp;amp;max;
  if not missing(x{k}) then do; prime_number=x{k}; output; end;
end;
keep prime_number;
run;&lt;/PRE&gt;
&lt;P&gt;I was tempted to replace the first DO loop by specifying initial values for the array, &lt;FONT face="courier new,courier"&gt;(1:&amp;amp;max)&lt;/FONT&gt;, but that turned out to be significantly slower.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit: By skipping the even numbers &amp;gt;2 the run time can be further reduced to &amp;lt;5.0 seconds.&lt;/P&gt;
&lt;PRE&gt;data want;
array x{&amp;amp;max} _temporary_;
do i=&lt;STRONG&gt;&lt;FONT color="#008000"&gt;2, 3&lt;/FONT&gt;&lt;/STRONG&gt; to &amp;amp;max &lt;STRONG&gt;&lt;FONT color="#008000"&gt;by 2&lt;/FONT&gt;&lt;/STRONG&gt;;
  x{i}=i;
end;

do i=&lt;FONT color="#008000"&gt;&lt;STRONG&gt;3&lt;/STRONG&gt;&lt;/FONT&gt; to int(sqrt(&amp;amp;max)) &lt;STRONG&gt;&lt;FONT color="#008000"&gt;by 2&lt;/FONT&gt;&lt;/STRONG&gt;;
  if not missing(x{i}) then do;
     do j=i*i to dim(x) by x{i};
       x{j}=.;
     end;
  end;
end;

do k=&lt;FONT color="#008000"&gt;&lt;STRONG&gt;2, 3&lt;/STRONG&gt;&lt;/FONT&gt; to &amp;amp;max &lt;STRONG&gt;&lt;FONT color="#008000"&gt;by 2&lt;/FONT&gt;&lt;/STRONG&gt;;
  if not missing(x{k}) then do; prime_number=x{k}; output; end;
end;
keep prime_number;
run;&lt;/PRE&gt;</description>
      <pubDate>Fri, 24 May 2019 18:42:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-and-mod-fuction/m-p/561450#M157172</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-05-24T18:42:55Z</dc:date>
    </item>
  </channel>
</rss>

