Date: Tue, 26 Oct 2004 20:33:49 -0400
Reply-To: "Chang Y. Chung" <chang_y_chung@HOTMAIL.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: "Chang Y. Chung" <chang_y_chung@HOTMAIL.COM>
Subject: Re: one random permutation
On Tue, 26 Oct 2004 15:13:25 -0600, nyiann <nwy@UALBERTA.CA> wrote:
>Hi all,
>
>I have a set of data I want to randomize--essentially, just one random
>permutation of the dataset. I guessd the easiest way to describe it is
>shifting the fields up or down a random number of positions. Is there an
>easy way to do this that anyone has done before? I can't think of any
>easier way than to subset each field, randomize it, then merge the fields
>together again.
Hi, N,
Just tried Paul's idea of doing obs*col array. HTH.
Cheers,
Chang
/* test data */
data one;
array v[1:3] v1-v3;
drop i j;
do i = 1 to 7;
do j = 1 to 3;
v[j] = 10*i + j;
end;
output;
end;
run;
proc print data=one;
run;
/* on lst
Obs v1 v2 v3
1 11 12 13
2 21 22 23
3 31 32 33
4 41 42 43
5 51 52 53
6 61 62 63
7 71 72 73
*/
/* now let us make a random data */
%let seed = 123456;
%let nCols = 3;
%let nRows = 7;
data two(keep=v:);
/* set up */
array M[1:&nRows., 1:&nCols.] _temporary_;
array column[1:&nRows.] _temporary_;
nRows = &nRows.;
nCols = &nCols.;
set one end=end;
array v[1:&nCols.] v:;
/* main */
link inputData;
do c = 1 to nCols;
rand = floor(nRows * ranuni(&seed.));
link rotateColumn;
end;
link outputData;
stop;
/* subs */
inputData:
do r = 1 to nRows;
set one;
do c = 1 to nCols;
M[r,c] = v[c];
end;
end;
return;
outputData:
do r = 1 to nRows;
do c = 1 to nCols;
v[c] = M[r,c];
end;
output;
end;
return;
rotateColumn:
do r = 1 to nRows;
column[r] = M[r,c];
end;
do r = 1 to nRows;
rr = mod(rand + r, nRows);
if rr = 0 then rr = nRows;
M[r,c] = column[rr];
end;
return;
run;
proc print data=two;
run;
/* on lst
Obs v1 v2 v3
1 61 22 53
2 71 32 63
3 11 42 73
4 21 52 13
5 31 62 23
6 41 72 33
7 51 12 43
*/