标题: 如何定义group [打印本页] 作者: shiyiming 时间: 2011-6-30 15:22 标题: 如何定义group data a;
input id $ x;
cards;
a 0
a 1
a 2
a 1
b 3
c 0
c 3
d 2
d 2
e 1
...
;
run;
在上面的data a里,我想建一个group:x_grp,也就是由每个id里distinct x值组成:
data b;
id x x_grp
a 0 0_1_2
a 1 0_1_2
a 2 0_1_2
a 1 0_1_2
b 3 3
c 0 0_3
c 3 0_3
d 2 2
d 2 2
e 1
...
请问这个x_grp如何建?
谢谢!作者: shiyiming 时间: 2011-6-30 17:38 标题: Re: 如何定义group [code:a2d597ur]
data a;
input id $ x $;
cards;
a 0
a 1
a 2
a 1
b 3
c 0
c 3
d 2
d 1
d 2
e 1
;
run;
data a2;
set a;
retain grp;
by id notsorted;
if first.id then grp=x;
else if find(grp,compress(x))=0 then grp=catx('_',grp,x);
if last.id then output;
run;
data b;
merge a a2;
by id;
run;
[/code:a2d597ur]作者: shiyiming 时间: 2011-6-30 18:58 标题: Re: 如何定义group [code:1xagyg28]data a;
input id $ x;
cards;
a 0
a 11
a 1
a 11
b 3
c 0
c 3
d 2
d 2
e 1
;
data b;
length x_grp $50;
do _n_=1 by 1 until(last.id);
set a;
by id notsorted;
if indexw(trim(x_grp),cats(x),'_')=0 then x_grp=catx('_',x_grp,x);
end;
do _n_=1 to _n_;
set a;
output;
end;
run;[/code:1xagyg28]作者: shiyiming 时间: 2011-7-5 21:07 标题: Re: 如何定义group 为什么用indexw而非index呢?查了一下,感觉差不多啊。另外为什么用cats(x)而不直接x?直接x也可以run出来结果。
[quote="hopewell":3092qyu1][code:3092qyu1]data a;
input id $ x;
cards;
a 0
a 11
a 1
a 11
b 3
c 0
c 3
d 2
d 2
e 1
;
data b;
length x_grp $50;
do _n_=1 by 1 until(last.id);
set a;
by id notsorted;
if indexw(trim(x_grp),cats(x),'_')=0 then x_grp=catx('_',x_grp,x);
end;
do _n_=1 to _n_;
set a;
output;
end;
run;[/code:3092qyu1][/quote:3092qyu1]作者: shiyiming 时间: 2011-7-8 00:54 标题: Re: 如何定义group I guess they are the same in this case.
Thx nobodyknows,hopewell ,hssnow .