<?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 How does CALL RANUNI work? in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/How-does-CALL-RANUNI-work/m-p/937014#M42115</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here are two examples from the SAS Help Center:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data u1(keep=x);
   seed=104;
   do i=1 to 5;
      call ranuni(seed, x);
      output;
   end;
   call symputx('seed', seed);
run;
data u2(keep=x);
   seed=&amp;amp;seed;
   do i=1 to 5;
      call ranuni(seed, x);
      output;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Question 1: The call symputx in the u1, is used for the data step in u2?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If I modify the code from u1 to the one below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data u1(keep=x);
   do seed=1 to 5;
      call ranuni(seed, x);
      output;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Question 2: Why doesn't the modified code provide 5 different numbers? Each time the seed has a new value, which is like the seed in dataset u2, except it's not a macro variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 24 Jul 2024 15:47:50 GMT</pubDate>
    <dc:creator>cosmid</dc:creator>
    <dc:date>2024-07-24T15:47:50Z</dc:date>
    <item>
      <title>How does CALL RANUNI work?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-does-CALL-RANUNI-work/m-p/937014#M42115</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here are two examples from the SAS Help Center:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data u1(keep=x);
   seed=104;
   do i=1 to 5;
      call ranuni(seed, x);
      output;
   end;
   call symputx('seed', seed);
run;
data u2(keep=x);
   seed=&amp;amp;seed;
   do i=1 to 5;
      call ranuni(seed, x);
      output;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Question 1: The call symputx in the u1, is used for the data step in u2?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If I modify the code from u1 to the one below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data u1(keep=x);
   do seed=1 to 5;
      call ranuni(seed, x);
      output;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Question 2: Why doesn't the modified code provide 5 different numbers? Each time the seed has a new value, which is like the seed in dataset u2, except it's not a macro variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 24 Jul 2024 15:47:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-does-CALL-RANUNI-work/m-p/937014#M42115</guid>
      <dc:creator>cosmid</dc:creator>
      <dc:date>2024-07-24T15:47:50Z</dc:date>
    </item>
    <item>
      <title>Re: How does CALL RANUNI work?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-does-CALL-RANUNI-work/m-p/937024#M42118</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/253026"&gt;@cosmid&lt;/a&gt;, great question! It's easy to miss this detail in the documentation, but it states under the seed argument that:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;A new value for&amp;nbsp;seed&amp;nbsp;is returned each time CALL RANUNI is executed.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Documentation for CALL RANUNI Routine: &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/p1iizc7tw29sean1gc3oaoxkbp3r.htm#:~:text=A%20new%20value%20for%20seed%20is%20returned%20each%20time%20CALL%20RANUNI%20is%20executed" target="_blank"&gt;https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/p1iizc7tw29sean1gc3oaoxkbp3r.htm#:~:text=A%20new%20value%20for%20seed%20is%20returned%20each%20time%20CALL%20RANUNI%20is%20executed&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you run a debugger on your second example, you'll see that in your first iteration of the DO loop seed=1. However, as soon as the CALL RANUNI statement is run, the seed is set to a completely different number that is not within the range of seed values for the DO loop. This causes the DO loop to terminate resulting in only one value.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 24 Jul 2024 16:16:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-does-CALL-RANUNI-work/m-p/937024#M42118</guid>
      <dc:creator>antonbcristina</dc:creator>
      <dc:date>2024-07-24T16:16:35Z</dc:date>
    </item>
    <item>
      <title>Re: How does CALL RANUNI work?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-does-CALL-RANUNI-work/m-p/937025#M42119</link>
      <description>&lt;P&gt;Second question first:&lt;BR /&gt;Why doesn't the modified code provide 5 different numbers? Each time the seed has a new value, which is like the seed in dataset u2, except it's not a macro variable&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;data u1(keep=x);
   do seed=1 to 5;
      call ranuni(seed, x);
      output;
   end;
run;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;Call ranuni UPDATES the value of seed in that call. So it is very likely returning a value larger than 5 and the loop only executes one time because of the value of the returned value.&lt;/P&gt;
&lt;P&gt;You can see what happens with this code:&lt;/P&gt;
&lt;PRE&gt;data work.u1;
   seed=1234;
   do y=1 to 5;
      call ranuni(seed,x);
      output;
   end;
run;&lt;/PRE&gt;
&lt;P&gt;Even though the first time seed is used it has a value of 1234 when the call ranuni executes it will be something quite different when the OUTPUT statement is encountered. When I ran this the output value of seed for the first observation is 523580480.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Question 1: The call symputx in the u1, is used for the data step in u2?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The Call symputx assigns the value of the seed after the last call ranuni execution to the macro variable seed so that can be used as the initial seed in the second data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;An additional point may be of interest using the following. NOTE the change to the first set keeping the SEED values:&lt;/P&gt;
&lt;PRE&gt;data work.u1(keep=x seed);
   seed=104;
   do i=1 to 5;
      call ranuni(seed, x);
      output;
   end;
   call symputx('seed', seed);
run;
data work.u2(keep=x);
   seed=&amp;amp;seed;
   do i=1 to 5;
      call ranuni(seed, x);
      output;
   end;
run;

data work.u3 (keep=x);
   set work.u1 (firstobs=5);
   do i=1 to 5;
      call ranuni(seed, x);
      output;
   end;
run;
&lt;/PRE&gt;
&lt;P&gt;Compare U2 and U3. See anything interesting?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 24 Jul 2024 16:23:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-does-CALL-RANUNI-work/m-p/937025#M42119</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-07-24T16:23:14Z</dc:date>
    </item>
    <item>
      <title>Re: How does CALL RANUNI work?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-does-CALL-RANUNI-work/m-p/937038#M42121</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/253026"&gt;@cosmid&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your questions 1 and 2 have already been answered very well. Let me just add a mathematician's answer to the question in the subject line.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The DATA step&lt;/P&gt;
&lt;PRE&gt;data test;
seed=104;
&lt;STRONG&gt;call ranuni(seed,x);&lt;/STRONG&gt;
run;&lt;/PRE&gt;
&lt;P&gt;creates the same result as this DATA step:&lt;/P&gt;
&lt;PRE&gt;data test;
seed=104;
&lt;STRONG&gt;seed=mod(seed*397204094, 2**31-1);
x=seed/(2**31-1);&lt;/STRONG&gt;
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;Mathematically&lt;/EM&gt;, the same two formulas work for any other valid, positive seed value (i.e., integer between 1 and 2,147,483,646) as well. &lt;EM&gt;Practically&lt;/EM&gt;, however, the equivalence of the two steps will often break down if the product of the seed and the constant multiplier 397,204,094 exceeds &lt;FONT face="courier new,courier"&gt;constant('exactint')&lt;/FONT&gt;, which equals 2**53 (=9,007,199,254,740,992) under Windows. For example, with &lt;FONT face="courier new,courier"&gt;seed=22676507&lt;/FONT&gt; the results differ slightly (on my Windows workstation) because of a rounding error occurring in the second DATA step. The internal algorithm used by the CALL RANUNI routine avoids rounding errors (and can be emulated in a DATA step with a bit more work).&lt;/P&gt;</description>
      <pubDate>Wed, 24 Jul 2024 17:49:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-does-CALL-RANUNI-work/m-p/937038#M42121</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2024-07-24T17:49:59Z</dc:date>
    </item>
    <item>
      <title>Re: How does CALL RANUNI work?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-does-CALL-RANUNI-work/m-p/937055#M42122</link>
      <description>ahh, I did read where it said a new value for seed is returned each time CALL RANUNI is executed but I didn't grasp the meaning of it. Thanks for the explanation!</description>
      <pubDate>Wed, 24 Jul 2024 20:00:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-does-CALL-RANUNI-work/m-p/937055#M42122</guid>
      <dc:creator>cosmid</dc:creator>
      <dc:date>2024-07-24T20:00:02Z</dc:date>
    </item>
    <item>
      <title>Re: How does CALL RANUNI work?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-does-CALL-RANUNI-work/m-p/937056#M42123</link>
      <description>Thanks for the examples! I'm just glad I don't have to explain things mathematiclally to people, lol</description>
      <pubDate>Wed, 24 Jul 2024 20:01:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-does-CALL-RANUNI-work/m-p/937056#M42123</guid>
      <dc:creator>cosmid</dc:creator>
      <dc:date>2024-07-24T20:01:26Z</dc:date>
    </item>
    <item>
      <title>Re: How does CALL RANUNI work?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-does-CALL-RANUNI-work/m-p/937058#M42124</link>
      <description>&lt;P&gt;Thank you for that work.u3 example! I now understand that you can use a variable from a dataset as well for the first parameter. And they are using the value from the last seed from work.u1, one is from the CALL SYMPUTX which is used after the DO LOOP and the other is by using the firstobs=5 option. Thanks!&lt;/P&gt;</description>
      <pubDate>Wed, 24 Jul 2024 20:16:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-does-CALL-RANUNI-work/m-p/937058#M42124</guid>
      <dc:creator>cosmid</dc:creator>
      <dc:date>2024-07-24T20:16:08Z</dc:date>
    </item>
    <item>
      <title>Re: How does CALL RANUNI work?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-does-CALL-RANUNI-work/m-p/937079#M42126</link>
      <description>&lt;UL class="lia-list-style-type-square"&gt;
&lt;LI&gt;Six reasons you should stop using the RANUNI function to generate random numbers&lt;BR /&gt;By Rick Wicklin on The DO Loop July 10, 2013&lt;BR /&gt;&lt;A href="https://blogs.sas.com/content/iml/2013/07/10/stop-using-ranuni.html" target="_blank"&gt;https://blogs.sas.com/content/iml/2013/07/10/stop-using-ranuni.html&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;How to use the new random-number generators in SAS &lt;BR /&gt;By Rick Wicklin on The DO Loop January 29, 2018&lt;BR /&gt;&lt;A href="https://blogs.sas.com/content/iml/2018/01/29/random-number-generators-sas.html" target="_blank"&gt;https://blogs.sas.com/content/iml/2018/01/29/random-number-generators-sas.html&lt;/A&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;Koen&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jul 2024 08:03:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-does-CALL-RANUNI-work/m-p/937079#M42126</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2024-07-25T08:03:28Z</dc:date>
    </item>
  </channel>
</rss>

