SAS中文论坛

标题: 有后面的观测赋值个第一个观测 [打印本页]

作者: shiyiming    时间: 2011-1-14 21:35
标题: 有后面的观测赋值个第一个观测
假设数据集one为
a     b      c
1     2      3
1     4      5
1     6      7
2     8      9
2     10    11
2     12    13

请问在各组中,X为一随机产生的数,如何使当0<x<=1/2时,各组的第一个观测值中b变量等于变量b的第二个观测值,若1/2<X<1,则第一个观测值中b等于变量c的第二个观测值。

即可能得到的结果为:
a     b      c
1     4      3
1     4      5
1     6      7
2     11      9
2     10    11
2     12    13

谢谢啦!
作者: Qiong    时间: 2011-1-19 11:53
标题: Re: 有后面的观测赋值个第一个观测
土法,凑合用
[code:3dh5sz34]data vk ;
input a b c;
cards;
1 2 3
1 4 5
1 6 7
2 8 9
2 10 11
2 12 13
;
run;
data  vk2;
retain new 0;
set vk;by a notsorted;   
new= new*(1-first.a)+1;
rename b=newb c=newc;
if new=2;
run;
data vk ;
merge vk  vk2;
by a;
if first.a then do;x=RAND('BERNOULLI',0.5);b= x*newc+(1-x)*newb; end;
drop new:;
run;
proc print;
run;[/code:3dh5sz34]
作者: shiyiming    时间: 2011-1-21 00:06
标题: Re: 有后面的观测赋值个第一个观测
to vicky1020: learn a lot from you, thank you!
And I have not found other code besides the following trivial code.
[code:swajljf9]
data out(keep=a b c);
set raw end=last;
by a;
if first.a then do;
  array raw{2} b c;
  array temp{2};
  do i=1 to 2;
    temp[i]=raw[i];
  end;
index=_n_+1;
set raw point=index;
if         0<ranuni(123)<=0.5 then raw[2]=temp[2];
else do;
raw[1]=raw[2];
raw[2]=temp[2];
end;
end;
output;
if last then stop;
run;
[/code:swajljf9]
作者: shiyiming    时间: 2011-1-21 01:24
标题: Re: 有后面的观测赋值个第一个观测
to hairul
啊。写得很好啊。




欢迎光临 SAS中文论坛 (https://mysas.net/forum/) Powered by Discuz! X3.2