不知道为什么,但我真的很纠结这个问题。 我正在尝试获取 n 个唯一数字。 在这个例子中,我希望它是 15 个数字;
%let maximum_draws = 15;
无论我尝试什么(我在这上面花了几个小时,我得到了重复的)。 有人可以解释一下原因吗?
data test;
array game(&maximum_draws);
game(1) = int(ranuni(0)*15+1);
do i = 2 to &maximum_draws;
rand = int(ranuni(0)*15+1);
do j = 1 to i-1;
if rand eq game(j) then do while (rand eq game(j));
rand = int(ranuni(0)*15+1);
end;
end;
game(i) = rand;
end;
run;
最佳答案
您可以使用 not in
运算符进行更有效的测试以检查号码是否已被选中:
data test;
array game(&maximum_draws);
do i = 1 to &maximum_draws;
do while (game(i) = .);
rand = int(ranuni(0)*15+1);
if rand not in game then game(i) = rand;
end;
end;
run;
关于sas - 随机化 n 个唯一数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28176678/