group x y
a 1 5
a 7 10
a 8 10
b 5 10
b 10 22
b 11 21
b 16 25
c 1 5
d 2 6
d 7 9
d 10 20
d 11 12
d 13 15
d 15 23
e 1 3
f 2 7
f 9 11
....
问题: 在每个group里,如何找出相互有时间重合的records,比如:(a 7 10)和(a 8 10).我知道可以用SQL,但我想知道如何主要用retain来实现.谢谢!作者: shiyiming 时间: 2009-12-11 17:52 标题: Re: 关于'retain' 个人认为斑竹的SQL是最佳的方法,不推荐retain
[code:x2myd2sl]data raw;
input group $ x y;
datalines;
a 1 5
a 7 10
a 8 10
b 5 10
b 10 22
b 11 21
b 16 25
c 1 5
d 2 6
d 7 9
d 10 20
d 11 12
d 13 15
d 15 23
e 1 3
f 2 7
f 9 11
;
data temp(drop=max_n n);
retain max_n;
n=0;
do _n_=1 by 1 until(last.group);
set raw end=eof;
by group;
n+1;
output;
end;
max_n=max(max_n,n);
if eof then call symputx('n',max_n);
run;
data temp(keep=group x y flag);
array arr{2,&n} x1-x&n y1-y&n;
do _n_=1 by 1 until(last.group);
set temp;
by group;
flag=0;
arr(1,_n_)=x;
arr(2,_n_)=y;
do i=1 to &n;
if arr(1,i)<=x<=arr(2,i) then flag+1;
end;
flag=flag-1;
output;
end;
run;[/code:x2myd2sl]作者: shiyiming 时间: 2009-12-15 02:16 标题: Re: 关于'retain' Do you think the last data step could create what you want?
When there are many variables to keep, this may not a good choice.
data test;
input group $1. x y;
cards;
a 1 5
a 7 10
a 8 10
b 5 10
b 10 22
b 11 21
b 16 25
c 1 5
d 2 6
d 7 9
d 10 20
d 11 12
d 13 15
d 15 23
e 1 3
f 2 7
f 9 11
;
run;
*make more OBS;
data test1;
input group $1. x y;
cards;
a 8 11
b 6 10
c 2 6
d 3 6
d 8 9
e 2 3
f 2 8
;
run;
data test;
set test test1;
run;
proc sort data=test;
by group x y;
run;
data test2(rename=(x1=x y1=y));
set test;
by group x y ;
retain flag 0 x1 y1;
keep group x1 y1;
x1=lag(x);
y1=lag(y);
if first.group then flag=0;
else if x=x1 or y=y1 then do;
if flag=0 then output;
flag+1;
x1=x;
y1=y;
output;
end;
else flag=0;
run;