lundi 3 avril 2017

Assign random number by group

I am trying to assign a the same random number to each observation within a group. Thus in the dataset below, the value of the variable "random" would be equal for each observation where gp=B, and would take another value for each observation where gp=A, and so on.

data test ;
input gp $ a b c ;
datalines;
B 2 2 3
B 2 2 3
A 1 2 3
A 1 2 3
C 3 3 4
C 3 3 4
;

Stupidly I tried to create a different seed for each group based upon common unique values to each group:

data test2 ;
set test ;

seed = a*b*c ;
random = ranuni(seed) ;

run ;

This creates a common starting point per group, but which obviously changes for each observation.

How can I obtain a random number equivalent for each observation in the group? Due to the very large size of the real dataset I would like to avoid any sorting or other time consuming processes.

The required datset would thus look something like:

data want ;
input gp $ a b c random ;
datalines;
B 2 2 3 0.123
B 2 2 3 0.123
A 1 2 3 0.456
A 1 2 3 0.456
C 3 3 4 0.789
C 3 3 4 0.789
;




Aucun commentaire:

Enregistrer un commentaire