<?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 Newbie needs help with macro loop to illustrate Law of Large Numbers in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Newbie-needs-help-with-macro-loop-to-illustrate-Law-of-Large/m-p/607139#M176405</link>
    <description>&lt;P&gt;Hello experts, I tried to figure out this exercise for running a macro loop, but as I am still new to this I wanted to check to make sure I was doing this correctly! I think my graph might be wrong. Here are the instructions:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT&gt;Run a macro loop to illustrate the Law of Large Numbers. This is what you need to do: (a) Generate 1, 2, 3, 4, 5, …, n Bernoulli(p) random variables and compute the means of 1, 2, 3 ,4,5, …, n variables. Generate the variables and do the computations within the same loop (cf. the code we studied in class). (b) Plot the means to show that they converge to p. (c) Create a macro loop which has n and p as arguments. &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT&gt;Here is my code:&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT&gt;%macro loop1(p,n);&lt;BR /&gt;data Bernoulli;&lt;BR /&gt;do j=1 to &amp;amp;n;&lt;BR /&gt;u=rand("Bernoulli",&amp;amp;p);&lt;BR /&gt;output;&lt;BR /&gt;keep u;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;BR /&gt;data exercise2;&lt;BR /&gt;set Bernoulli;&lt;BR /&gt;acc+u;&lt;BR /&gt;time=_N_;&lt;BR /&gt;xbar=acc/time;&lt;BR /&gt;run;&lt;BR /&gt;proc univariate data=Bernoulli noprint;&lt;BR /&gt;var u;&lt;BR /&gt;output out=stats mean=xbar;&lt;BR /&gt;run;&lt;BR /&gt;proc append base=Bernoulli data=stats;&lt;BR /&gt;run;&lt;BR /&gt;%end;&lt;BR /&gt;symbol interpol=join;&lt;BR /&gt;proc gplot data=exercise2;&lt;BR /&gt;plot xbar*time/vref=&amp;amp;p;&lt;BR /&gt;run;&lt;BR /&gt;%mend;&lt;BR /&gt;%loop1(0.6,600);&lt;BR /&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please help! Thank you!&lt;/P&gt;</description>
    <pubDate>Mon, 25 Nov 2019 23:06:03 GMT</pubDate>
    <dc:creator>user1942</dc:creator>
    <dc:date>2019-11-25T23:06:03Z</dc:date>
    <item>
      <title>Newbie needs help with macro loop to illustrate Law of Large Numbers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Newbie-needs-help-with-macro-loop-to-illustrate-Law-of-Large/m-p/607139#M176405</link>
      <description>&lt;P&gt;Hello experts, I tried to figure out this exercise for running a macro loop, but as I am still new to this I wanted to check to make sure I was doing this correctly! I think my graph might be wrong. Here are the instructions:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT&gt;Run a macro loop to illustrate the Law of Large Numbers. This is what you need to do: (a) Generate 1, 2, 3, 4, 5, …, n Bernoulli(p) random variables and compute the means of 1, 2, 3 ,4,5, …, n variables. Generate the variables and do the computations within the same loop (cf. the code we studied in class). (b) Plot the means to show that they converge to p. (c) Create a macro loop which has n and p as arguments. &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT&gt;Here is my code:&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT&gt;%macro loop1(p,n);&lt;BR /&gt;data Bernoulli;&lt;BR /&gt;do j=1 to &amp;amp;n;&lt;BR /&gt;u=rand("Bernoulli",&amp;amp;p);&lt;BR /&gt;output;&lt;BR /&gt;keep u;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;BR /&gt;data exercise2;&lt;BR /&gt;set Bernoulli;&lt;BR /&gt;acc+u;&lt;BR /&gt;time=_N_;&lt;BR /&gt;xbar=acc/time;&lt;BR /&gt;run;&lt;BR /&gt;proc univariate data=Bernoulli noprint;&lt;BR /&gt;var u;&lt;BR /&gt;output out=stats mean=xbar;&lt;BR /&gt;run;&lt;BR /&gt;proc append base=Bernoulli data=stats;&lt;BR /&gt;run;&lt;BR /&gt;%end;&lt;BR /&gt;symbol interpol=join;&lt;BR /&gt;proc gplot data=exercise2;&lt;BR /&gt;plot xbar*time/vref=&amp;amp;p;&lt;BR /&gt;run;&lt;BR /&gt;%mend;&lt;BR /&gt;%loop1(0.6,600);&lt;BR /&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please help! Thank you!&lt;/P&gt;</description>
      <pubDate>Mon, 25 Nov 2019 23:06:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Newbie-needs-help-with-macro-loop-to-illustrate-Law-of-Large/m-p/607139#M176405</guid>
      <dc:creator>user1942</dc:creator>
      <dc:date>2019-11-25T23:06:03Z</dc:date>
    </item>
    <item>
      <title>Re: Newbie needs help with macro loop to illustrate Law of Large Numbers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Newbie-needs-help-with-macro-loop-to-illustrate-Law-of-Large/m-p/607142#M176407</link>
      <description>&lt;P&gt;Show us the graph.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Describe why you think it is wrong.&lt;/P&gt;</description>
      <pubDate>Mon, 25 Nov 2019 23:32:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Newbie-needs-help-with-macro-loop-to-illustrate-Law-of-Large/m-p/607142#M176407</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-11-25T23:32:25Z</dc:date>
    </item>
    <item>
      <title>Re: Newbie needs help with macro loop to illustrate Law of Large Numbers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Newbie-needs-help-with-macro-loop-to-illustrate-Law-of-Large/m-p/607149#M176411</link>
      <description>&lt;P&gt;You may want to show us the code of your loop that actually ran. As posted it throws a macro compiler error:&lt;/P&gt;
&lt;PRE&gt;709  %macro loop1(p,n);
710  data Bernoulli;
711  do j=1 to &amp;amp;n;
712  u=rand("Bernoulli",&amp;amp;p);
713  output;
714  keep u;
715  end;
716  run;
717  data exercise2;
718  set Bernoulli;
719  acc+u;
720  time=_N_;
721  xbar=acc/time;
722  run;
723  proc univariate data=Bernoulli noprint;
724  var u;
725  output out=stats mean=xbar;
726  run;
727  proc append base=Bernoulli data=stats;
728  run;
729  %end;
&lt;STRONG&gt;&lt;FONT color="#ff0000"&gt;ERROR: There is no matching %DO statement for the %END. This statement will be ignored.
&lt;/FONT&gt;&lt;/STRONG&gt;730  symbol interpol=join;
731  proc gplot data=exercise2;
732  plot xbar*time/vref=&amp;amp;p;
733  run;
734  %mend;
&lt;/PRE&gt;
&lt;P&gt;Please post code or log entries in a code box opened with the forum's&amp;nbsp;{I} or "running man" icons to preserve formatting.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As posted your&amp;nbsp;Exercise2 data set would have been something not generated by the shown code.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Nov 2019 23:52:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Newbie-needs-help-with-macro-loop-to-illustrate-Law-of-Large/m-p/607149#M176411</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-11-25T23:52:23Z</dc:date>
    </item>
    <item>
      <title>Re: Newbie needs help with macro loop to illustrate Law of Large Numbers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Newbie-needs-help-with-macro-loop-to-illustrate-Law-of-Large/m-p/607288#M176492</link>
      <description>&lt;P&gt;I think you need SAS/IML , if you want simulate many variables.&lt;/P&gt;
&lt;P&gt;And&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13684"&gt;@Rick_SAS&lt;/a&gt;&amp;nbsp; wrote a couple of blogs about this topic .&lt;/P&gt;
&lt;P&gt;One of them I remembered is guessing weight of a pumpkin at Christmas .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There is display just one variable .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
call streaminit(123);
do i=1 to 100;
 x=rand('bern',0.2);
 output;
end;
run;
data have;
 set have;
 sum+x;
 mean=sum/_n_;
run;
proc sgplot data=have;
series x=i y=mean;
refline 0.2/axis=y;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="x.png" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/34246i7250EBA0321A5881/image-size/large?v=v2&amp;amp;px=999" role="button" title="x.png" alt="x.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Nov 2019 11:54:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Newbie-needs-help-with-macro-loop-to-illustrate-Law-of-Large/m-p/607288#M176492</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-11-26T11:54:38Z</dc:date>
    </item>
  </channel>
</rss>

