p*****o 发帖数: 543 | 1 数据如下:
VAR1
A
A
B
B
B
C
C
C
D
D
。。。。。
。。。。。
现在要做的就是给每个OBS都从1,2,3 (其实是1,2,3,。。。100,但是咱就先用3
个的来讨论吧)中随机选一个数,设为VAR2. 但是限制条件是:
VAR1 的值同样的情况下,VAR2的值必须不一样。
但是VAR1的值不同的情况下,VAR2的值可以一样。 |
w*******n 发帖数: 469 | 2 你这个表述实在是模糊!
【在 p*****o 的大作中提到】 : 数据如下: : VAR1 : A : A : B : B : B : C : C : C
|
p*****o 发帖数: 543 | 3 就是我要给每个OBS随机地从1,2,3,。。。,100中取一个数。但是对于组内而言(
比如VAR1 = A的组),他们不能取同样的数(即WITHOUT REPLACEMENT)。 |
l*********s 发帖数: 5409 | 4 It is not very polite to just tell people do coding for you.
【在 p*****o 的大作中提到】 : 数据如下: : VAR1 : A : A : B : B : B : C : C : C
|
g**a 发帖数: 2129 | |
p*****o 发帖数: 543 | 6 actually i coded all by myself but just not a easy way...what i really want is to find a easy way....and i do believe there should be some easy way to take care of it.
and following is just one way i tried.....
%macro assign_rand(gvar);
data t1;
set tmp3;
if a = &gvar.;
run;
data tr1;
array x x1-x27 (1 2 3);
seed = 0;
call ranperm(seed, of x1-x3);
run;
proc transpose data=tr1 out=tr2;
run;
data tr3;
a = &gvar.;
set tr2;
keep a col1;
if _NAME_ = "seed" then delete;
run;
data t2;
set tr3;
set t1;
【在 l*********s 的大作中提到】 : It is not very polite to just tell people do coding for you.
|
l*********s 发帖数: 5409 | 7 A has 2 entries, but B,C,D each has 3? and, the number of records for
particular V1 is always less than # of possible outcomes? |
D******n 发帖数: 2836 | 8 data a1;
input var1 $;
datalines;
A
A
B
B
B
C
C
C
D
D
;
data a1;set a1;by var1;
retain match_key 0;match_key=match_key+1;
if first.var1 then match_key=1;run;
data a2;set a1;by var1;
if first.var1 then do;
do var2=1 to 3;
rand=ranuni(100);
output;
end;
end;drop match_key;run;
proc sort data=a2;by var1 rand;run;
data a2;set a2;by var1;
retain match_key 0;match_key=match_key+1;
if first.var1 then match_key=1;run;
proc sql;select a1.var1,a2.var2 from a1,a2
where a1.var1=a2.va
【在 p*****o 的大作中提到】 : actually i coded all by myself but just not a easy way...what i really want is to find a easy way....and i do believe there should be some easy way to take care of it. : and following is just one way i tried..... : %macro assign_rand(gvar); : data t1; : set tmp3; : if a = &gvar.; : run; : data tr1; : array x x1-x27 (1 2 3); : seed = 0;
|
p*****o 发帖数: 543 | 9 great idea!!
【在 D******n 的大作中提到】 : data a1; : input var1 $; : datalines; : A : A : B : B : B : C : C
|
p*****o 发帖数: 543 | 10 but i'm wondering in the created data set, is it still the same order of
those 2 A (and 3B, etc) as in the original data set?
though you matched with match_key, i didnt quite understand whether the
order remains the same or not... |
|
|
D******n 发帖数: 2836 | 11 u mean the order within each group of VAR1?
They will not change. |
p*****o 发帖数: 543 | 12 yea. i mean the order within each group of var1.
they will not change?
interesting...i will read through it again.
Is that b/c of the match_key existing there?
【在 D******n 的大作中提到】 : u mean the order within each group of VAR1? : They will not change.
|
p*****o 发帖数: 543 | 13 i still think it changed....
for example, just by running ur code completely, i guess the 1st B should be
assigned with number 3 since the rand for the 1st B generated is the
largest one among the three rands for three B. But in the final data set, I
guess we assigned number 2 to the first B.
Not sure whether I understand correctly or not...
【在 D******n 的大作中提到】 : u mean the order within each group of VAR1? : They will not change.
|
D******n 发帖数: 2836 | 14 I don't quite get what you worry about.
You want 1,2,3 randomly assigned to As and Bs and so on, so it doesn't
matter what the first B has(there is no such thing as what the first B "
should" have), as long as it is random. |
p*****o 发帖数: 543 | 15 that's true for what i posted here....
but actually Im facing a more complicated situation. I have also other
variables, such as something like match_key. So once I need to keep track of
the original order....
Or probably I should change the way of posting the problem as we have
original data set:
Var1
A_1
A_2
B_1
B_2
B_3
C_1
C_2
C_3
...
and Im still trying to do the same thing as randomly pick 1 number from 1,2,
3 to assign to var1, while this can not be the same number for Var1 starti
【在 D******n 的大作中提到】 : I don't quite get what you worry about. : You want 1,2,3 randomly assigned to As and Bs and so on, so it doesn't : matter what the first B has(there is no such thing as what the first B " : should" have), as long as it is random.
|
S******y 发帖数: 1123 | 16 #A Python approach: scramble the oder within a group
import random
var1_list = ['A', 'A', 'B', 'B', 'B', 'C', 'C', 'C', 'D', 'D']
ls_item=[]
ls_int =[]
tmp_var1 =''
for item in var1_list:
if tmp_var1 != item and tmp_var1 !='':
ls_int = range(len(ls_item))
random.shuffle(ls_int)
for index, x in enumerate(ls_item):
print x, ls_int[index]+1 #print out var1, var2
ls_item=[]
ls_int=[]
ls_item.append(item)
tmp_var1 = item
ls_int |
p*****o 发帖数: 543 | 17 thanks a lot.
正好最近在学PYTHON!
【在 S******y 的大作中提到】 : #A Python approach: scramble the oder within a group : import random : var1_list = ['A', 'A', 'B', 'B', 'B', 'C', 'C', 'C', 'D', 'D'] : ls_item=[] : ls_int =[] : tmp_var1 ='' : for item in var1_list: : if tmp_var1 != item and tmp_var1 !='': : ls_int = range(len(ls_item)) : random.shuffle(ls_int)
|
w*******n 发帖数: 469 | 18 Good to learn sth. thanks.
【在 D******n 的大作中提到】 : data a1; : input var1 $; : datalines; : A : A : B : B : B : C : C
|