SAS 中的嵌套 Do 循环
Nested Do Loops in SAS
我正在尝试弄清楚如何嵌套这些循环,以便生成一些多级数据。不过,我一直收到错误消息。我真的是 SAS 的新手,所以我正在努力弄清楚如何解决这个问题。感谢您的帮助!
%macro MLM;
data x;
call streaminit(525600);
do rep=1 to 1000;
%do a=1 %to 4;
%if &a=1 %then %do; %let N=100; %end;
%if &a=2 %then %do; %let N=250; %end;
%if &a=3 %then %do; %let N=500; %end;
%if &a=4 %then %do; %let N=1000; %end;
do J=1 to &N;
u0= RAND('NORMAL',0,1);
w1= RAND('BERNOULLI',.5);
do I=1 to (10000/&N);
%do b = 1 %to 5;
%if &b=1 %then %do; %let e=RAND('NORMAL',0,1); %end;
%if &b=2 %then %do; %let e=RAND('UNIFORM'); %end;
%if &b=3 %then %do; %let e=RAND('CHISQUARE', &N-1); %end;
%if &b=4 %then %do; %let e=RAND('LOGNORMAL'); %end;
%if &b=5 %then %do; %let e=RAND('BETA',1,1); %end;
x1= RAND('BERNOULLI',.5);
b0 = 2.5+ 1*w1+u0;
b1 = 0.15;
y=b0+b1*x1+&e;
output;
%end;
end;
end;
%end;
end;
%mend MLM; run; %MLM; run;
我想运行 1000次模拟,4个样本大小(a,N),然后生成N个样本,期间我想模拟5种不同的误差分布。所以,最后,我应该得到 1000x4x5 个样本。
再次感谢!
删除宏逻辑并稍微简化它可以得到这个,这对我来说似乎工作正常。注意我简化了 N 以确保它快速运行。请注意,我没有验证逻辑,假设你这边是正确的。
data x;
call streaminit(525600);
do rep=1 to 5;
do a=1 to 4;
if a=1 then
N=100;
else if a=2 then
N=250;
else if a=3 then
N=500;
else if a=4 then
N=1000;
do J=1 to N;
u0=RAND('NORMAL', 0, 1);
w1=RAND('BERNOULLI', .5);
do I=1 to (2000/N);
do b=1 to 5;
if b=1 then
e=RAND('NORMAL', 0, 1);
else if b=2 then
e=RAND('UNIFORM');
else if b=3 then
e=RAND('CHISQUARE', N-1);
else if b=4 then
e=RAND('LOGNORMAL');
else if b=5 then
e=RAND('BETA', 1, 1);
x1=RAND('BERNOULLI', .5);
b0=2.5+ 1*w1+u0;
b1=0.15;
y=b0+b1*x1+e;
output;
end;
end;
end;
end;
end;
run;
我正在尝试弄清楚如何嵌套这些循环,以便生成一些多级数据。不过,我一直收到错误消息。我真的是 SAS 的新手,所以我正在努力弄清楚如何解决这个问题。感谢您的帮助!
%macro MLM;
data x;
call streaminit(525600);
do rep=1 to 1000;
%do a=1 %to 4;
%if &a=1 %then %do; %let N=100; %end;
%if &a=2 %then %do; %let N=250; %end;
%if &a=3 %then %do; %let N=500; %end;
%if &a=4 %then %do; %let N=1000; %end;
do J=1 to &N;
u0= RAND('NORMAL',0,1);
w1= RAND('BERNOULLI',.5);
do I=1 to (10000/&N);
%do b = 1 %to 5;
%if &b=1 %then %do; %let e=RAND('NORMAL',0,1); %end;
%if &b=2 %then %do; %let e=RAND('UNIFORM'); %end;
%if &b=3 %then %do; %let e=RAND('CHISQUARE', &N-1); %end;
%if &b=4 %then %do; %let e=RAND('LOGNORMAL'); %end;
%if &b=5 %then %do; %let e=RAND('BETA',1,1); %end;
x1= RAND('BERNOULLI',.5);
b0 = 2.5+ 1*w1+u0;
b1 = 0.15;
y=b0+b1*x1+&e;
output;
%end;
end;
end;
%end;
end;
%mend MLM; run; %MLM; run;
我想运行 1000次模拟,4个样本大小(a,N),然后生成N个样本,期间我想模拟5种不同的误差分布。所以,最后,我应该得到 1000x4x5 个样本。
再次感谢!
删除宏逻辑并稍微简化它可以得到这个,这对我来说似乎工作正常。注意我简化了 N 以确保它快速运行。请注意,我没有验证逻辑,假设你这边是正确的。
data x;
call streaminit(525600);
do rep=1 to 5;
do a=1 to 4;
if a=1 then
N=100;
else if a=2 then
N=250;
else if a=3 then
N=500;
else if a=4 then
N=1000;
do J=1 to N;
u0=RAND('NORMAL', 0, 1);
w1=RAND('BERNOULLI', .5);
do I=1 to (2000/N);
do b=1 to 5;
if b=1 then
e=RAND('NORMAL', 0, 1);
else if b=2 then
e=RAND('UNIFORM');
else if b=3 then
e=RAND('CHISQUARE', N-1);
else if b=4 then
e=RAND('LOGNORMAL');
else if b=5 then
e=RAND('BETA', 1, 1);
x1=RAND('BERNOULLI', .5);
b0=2.5+ 1*w1+u0;
b1=0.15;
y=b0+b1*x1+e;
output;
end;
end;
end;
end;
end;
run;