具有非循环元素的 SAS 随机数组

SAS Random array with a nonrecurrent elements

你能帮帮我吗? 我想生成一个从 0 到 5 的 随机数组 ,我正在使用这个函数

rand_num = int(ranuni(0)*5+1)

但我想生成一个包含 nonrecurrent 元素的随机数组。 例如(1,2,3,4,5) (3,1,5,4,2)等..

我该怎么做? 谢谢!

/* draw with repetition */
data a;
    array rand(5);

    do i = 1 to dim(rand);
        rand(i) = int(ranuni(0)*5+1);
    end;

    keep rand:;
run;

/* draw without repetition */
data a;
    array rand(5);

    do i = 1 to dim(rand);
        do until(rand(i) ^= .);
            value = int(ranuni(0)*5+1);
            if value not in rand then
                rand(i) = value;
        end;
    end;

    keep rand:;
run;

我认为 call ranperm 是更好的解决方案,尽管两者似乎具有大致相同的统计属性。这是一个使用它的解决方案(与 Keith 在 中指出的非常相似):

data want;
  array rand_array[5];

  *initialize the array (once);
  do _i = 1 to dim(rand_array);
    rand_array[_i]=_i;
  end;

  *seed for the RNG;
  seed=5;

  *randomize;
  *each time `call ranperm` is used, this shuffles the group;
  do _i = 1 to 1e5;
    call ranperm(seed,of rand_array[*]);
    output;
  end;
run;