SAS中文论坛

 找回密码
 立即注册

扫一扫,访问微社区

查看: 734|回复: 2
打印 上一主题 下一主题

关于'retain'

[复制链接]

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
楼主
 楼主| 发表于 2009-12-11 14:43:17 | 只看该作者

关于'retain'

假设一个dataset(其中(x,y)是个时间区间, group,x,y were sorted),每个group里的records不等:

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来实现.谢谢!
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
沙发
 楼主| 发表于 2009-12-11 17:52:15 | 只看该作者

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]
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
板凳
 楼主| 发表于 2009-12-15 02:16:07 | 只看该作者

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;
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|手机版|Archiver|SAS中文论坛  

GMT+8, 2026-2-6 06:25 , Processed in 0.068352 second(s), 20 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表