2017-02-28 2 views
1

Könnten Sie mir bitte helfen. Ich möchte eine zufällige Anordnung erzeugen von 0 bis 5, und ich bin mit dieser FunktionSAS Zufallsfeld mit nicht wiederkehrenden Elementen

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

Aber ich würde eine zufällige Anordnung mit einem transient Elemente erzeugen möchten. Zum Beispiel (1,2,3,4,5) (3,1,5,4,2) etc ..

Wie kann ich es tun? Danke!

+1

Sehen Sie sich auch die Antwort von @data_null in dieser Frage. 'Call Randperm' ist eine gute Option für Sie. https://stackoverflow.com/questions/42086988/randoming-symbols-from-a-z/42091012#42091012 – Longfish

Antwort

0
/* 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; 
0

Ich denke call ranperm die bessere Lösung dafür ist, obwohl beide in etwa die gleichen statistischen Eigenschaften zu haben scheinen. Hier ist eine Lösung, die (sehr ähnlich zu dem, was Keith deutete in @data_null_'s solution on another question) mit:

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;