sas - 随机化 n 个唯一数字

标签 sas

不知道为什么,但我真的很纠结这个问题。 我正在尝试获取 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/

相关文章:

sas - 当我将 SAS 数据集导出到 csv 时;它正在修剪字符中的所有前导空格

sas - 在 SAS 中使用两个变量分配遭遇数

recursion - 导出文件期间检测到打开代码语句递归

sas - sas如何为数据类型分配内存

sas - 使用 Fast LOAD 将 SAS 数据集加载到 Teradata 表中

count - SAS如何获取同一数据集中的汇总计数

sql - 使用 proc sql 更新

sas - 在 SAS 中构建包含子汇总行的表

excel - 使用 SAS 处理 Excel 中的大量列

HTML 格式的 SAS 存储过程