由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Statistics版 - 再来问一个SAS问题
相关主题
如何在1,2,3,4,5中随机选出2个数来?[合集] how to calculate column sum not row sum in SAS? thanks a lo
请教个SAS问题SAS 请教
用SAS sampling的一个问题Ask 2 simple SAS questions,thanks
SAS问题来了A SAS question
SAS问题SAS question - baozi
请问R Code和 Matlab Code 思路是否近似?请教SAS LABEL问题。
请教一个SAS小问题:如何得到 one cumulative varialbe of character variable?SAS question - baozi
SAS question: count non-missing value for different variables急问一个call symput问题(SAS)
相关话题的讨论汇总
话题: var1话题: ls话题: data话题: key话题: match
进入Statistics版参与讨论
1 (共1页)
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
5
还是没懂。给个完整的obs来看看吧。
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...
相关主题
请问R Code和 Matlab Code 思路是否近似?[合集] how to calculate column sum not row sum in SAS? thanks a lo
请教一个SAS小问题:如何得到 one cumulative varialbe of character variable?SAS 请教
SAS question: count non-missing value for different variablesAsk 2 simple SAS questions,thanks
进入Statistics版参与讨论
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

1 (共1页)
进入Statistics版参与讨论
相关主题
急问一个call symput问题(SAS)SAS问题
一个SAS的时间区域求值问题请问R Code和 Matlab Code 思路是否近似?
双包子求教:SAS问题请教一个SAS小问题:如何得到 one cumulative varialbe of character variable?
弱问一个SAS里面求adjusted means的问题SAS question: count non-missing value for different variables
如何在1,2,3,4,5中随机选出2个数来?[合集] how to calculate column sum not row sum in SAS? thanks a lo
请教个SAS问题SAS 请教
用SAS sampling的一个问题Ask 2 simple SAS questions,thanks
SAS问题来了A SAS question
相关话题的讨论汇总
话题: var1话题: ls话题: data话题: key话题: match