SAS running a macro loop for n and p generating variables–> then creating a macro loop which has n and p as arguments

huangapple go评论59阅读模式
英文:

SAS running a macro loop for n and p generating variables--> then creating a macro loop which has n and p as arguments

问题

以下是代码的翻译部分:

"As the title states im trying to illustrate the law of large numbers through bernoulli(p) by running a macro loop to generate output in the log. However with the following code i am getting no output what so ever. Following code is below."

"正如标题所述,我正在尝试通过运行宏循环来通过伯努利(p)来说明大数定律,并生成日志中的输出。但是,使用以下代码,我根本没有得到任何输出。以下是代码:"

英文:

As the title states im trying to illustrate the law of large numbers through bernoulli(p) by running a macro loop to generate output in the log. However with the following code i am getting no output what so ever. Following code is below.

Code:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

%macro LLN(n=10, p=0.6);
  %let sum = 0; %let mean = 0;
   data LLN; 
%do i = 1 %to &amp;n;
x = rand(&quot;Bernoulli&quot;, &amp;p); 
sum = sum + x;
mean = sum / i;
output; 
%end;
run;
%mend LLN;

<!-- end snippet -->

答案1

得分: 2

以下是翻译好的部分:

  1. 你实际上不需要宏。可以在正常的数据步骤中完成do循环。
  2. 如果你想在数据步骤中使用宏变量i来与数据步骤变量进行计算,你应该使用&i
  3. summean的初始方式是为宏变量设计的,但你实际上将它们用作数据步骤变量。

我建议你使用数据步骤:

data test;
  do i=1 to 10000;
    x = rand("Bernoulli", 0.6); 
    sum + x;
    mean = sum / i;
  end;
run;
英文:

There are three points to improve:

  1. You don't really need a macro. The do loop can be done in normal data step.
  2. If you want to use macro variable i in your data step to join the calculation with data step variables, you should use &amp;i.
  3. The initial way of sum and mean is for macro variable, but you truly use them as data step variables.

I suggest you to use a data step:

data test;
  do i=1 to 10000;
    x = rand(&quot;Bernoulli&quot;, 0.6); 
    sum + x;
    mean = sum / i;
  end;
run;

答案2

得分: 1

这是您提供的代码的中文翻译:

将@whymath代码宏化将接受循环<code>max</code>和<code>p</code>作为rand()函数的参数。

示例:

使用此宏创建单个数据集。为什么要在每次循环迭代中计算平均值?在LLN的研究中是否需要中间平均值?如果需要,在循环内部添加一个OUTPUT。如果不需要,在循环结束后计算mean = sum / &amp;N。您可能还希望宏接受一个随机数种子以便未来运行时能够复现性。

%macro LLN (N=1000, p=0.6, out=, seed=.);  /* .表示随机种子 */
  data &amp;out (label=&quot;LLN: N=&amp;N, p=&amp;p&quot;);
    call streaminit(&amp;seed);
    do i=1 to &amp;N;
      x = rand(&quot;Bernoulli&quot;, &amp;p); 
      sum + x;
      mean = sum / i;
    end;
    * mean = sum / &amp;N; * 或许? ;
  run;

%mend;

但是,您还可以在单个数据步骤中为不同的N和p组合计算总和和平均值,并涉及使用bygroup概念的某种可视化。

data LLN_study;
  do p = 0.1, 0.2, 0.4, 0.5, 0.6, 0.8, 0.9;
    do N = 1e3, 1e4, 1e5;

      call missing (sum, mean);
      do i = 1 to N;
        x = rand(&quot;Bernoulli&quot;, p); 
        sum + x;
        mean = sum / i;
        * OUTPUT ;  /* 如果需要每个N、p、i的sum和mean,请取消注释 */
      end;
      * mean = sum / N;
      * OUTPUT ;  /* 如果需要每个N、p的sum和mean,请取消注释 */
    end; /* p变化 */
  end; /* N变化 */
run;

proc sgplot data=LLN_study;
  series y=mean x=i / group=N;
  where mod(i,10)=1;
  by p;
run;

希望这有助于您理解代码的中文翻译。

英文:

A macro-ization of @whymath code would take argument for the loop <code>max</code> and <code>p</code> parameter of the rand() function.

Example:

A use of this macro creates a single data set. Why mean is calculated at each loop iteration? Do you want the intermediate mean in you study of LLN? If so, add an OUTPUT inside the loop. If not, after the loop concludes compute mean = sum / &amp;N. You probably also want the macro to accept a random number seed for reproducibility in future runs.

%macro LLN (N=1000, p=0.6, out=, seed=.);  /* . is a random seed */
  data &amp;out (label=&quot;LLN: N=&amp;N, p=&amp;p&quot;);
    call streaminit(&amp;seed);
    do i=1 to &amp;N;
      x = rand(&quot;Bernoulli&quot;, &amp;p); 
      sum + x;
      mean = sum / i;
    end;
    * mean = sum / &amp;N; * maybe? ;
  run;

%mend;

However, you can also compute the sum & mean for various combinations of N & p in a single data step and do some sort of visualizaton involving by and group concepts.

data LLN_study;
  do p = 0.1, 0.2, 0.4, 0.5, 0.6, 0.8, 0.9;
    do N = 1e3, 1e4, 1e5;

      call missing (sum, mean);
      do i = 1 to N;
        x = rand(&quot;Bernoulli&quot;, p); 
        sum + x;
        mean = sum / i;
        * OUTPUT ;  /* uncomment if you want every sum &amp; mean for each N,p,i */
      end;
      * mean = sum / N;
      * OUTPUT ;  /* uncomment if you want every sum &amp; mean for each N,p */
    end; /* p variation */
  end; /* N variation */
run;

proc sgplot data=LLN_study;
  series y=mean x=i / group=N;
  where mod(i,10)=1;
  by p;
run;

答案3

得分: 1

Get the SAS code to work properly for some value of N and P first.

So this code will create 10 observations.

data LLN; 
  do i = 1 to 10;
    x = rand("Bernoulli", 0.6); 
    sum + x;
    mean = sum / i;
    output; 
  end;
run;

If you just want the final observation and not all of the partial sums then either move the OUTPUT statement outside the loop, or delete it since SAS will write the observation at the end of the step when there is no explicit output statement.

data LLN; 
  do i = 1 to 10;
    x = rand("Bernoulli", 0.6); 
    sum + x;
  end;
  mean = sum / 10;
run;

Now you can try to convert it to a macro by replacing 10 with &N and 0.6 with &P.

%let n=10;
%let p=0.6;
data LLN; 
  do i = 1 to &n;
    x = rand("Bernoulli", &p); 
    sum + x;
  end;
  mean = sum / &n;
run;

Once you see that works you are ready to create a macro with parameters N and P.

%macro LLN(n, p);
data LLN; 
  do i = 1 to &n;
    x = rand("Bernoulli", &p); 
    sum + x;
  end;
  mean = sum / &n;
run;
%mend LLN;
%LLN(n=10,p=0.6)
英文:

Get the SAS code to work properly for some value of N and P first.

So this code will create 10 observations.

data LLN; 
  do i = 1 to 10;
    x = rand(&quot;Bernoulli&quot;, 0.6); 
    sum + x;
    mean = sum / i;
    output; 
  end;
run;

If you just want the final observation and not all of the partial sums then either move the OUTPUT statement outside the loop, or delete it since SAS will write the observation at the end of the step when there is no explicit output statement.

data LLN; 
  do i = 1 to 10;
    x = rand(&quot;Bernoulli&quot;, 0.6); 
    sum + x;
  end;
  mean = sum / 10;
run;

Now you can try to convert it to a macro by replacing 10 with &N and 0.6 with &P.

%let n=10;
%let p=0.6;
data LLN; 
  do i = 1 to &amp;n;
    x = rand(&quot;Bernoulli&quot;, &amp;p); 
    sum + x;
  end;
  mean = sum / &amp;n;
run;

Once you see that works you are ready to create a macro with parameters N and P.

%macro LLN(n, p);
data LLN; 
  do i = 1 to &amp;n;
    x = rand(&quot;Bernoulli&quot;, &amp;p); 
    sum + x;
  end;
  mean = sum / &amp;n;
run;
%mend LLN;
%LLN(n=10,p=0.6)

huangapple
  • 本文由 发表于 2023年5月10日 11:57:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76214786.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定