SAS中文论坛

标题: survival analysis的data management [打印本页]

作者: shiyiming    时间: 2006-11-13 06:30
标题: survival analysis的data management
一个survival analysis的data management,琢磨了好几天未果,特在此请教各位:
有3个变量,id标识不同的人,year标识时间(年份),insure表示有无保险(1=有保险,2=无保险),初始数据如下:
id  year insure
1  1991    0     
1  1992    0
1  1993    1
1  1994    1
1  1995    1
1  1996    1
1  1997    1
1  1998    0
1  1999    0
2  1991    0
2  1992    0
2  1993    1
2  1994    0

现在希望创建一个新的变量duration,以标识每个人有(无)保险的时间跨度,新的数据应如下:

id  year insure  duration
1  1991    0        1
1  1992    0        2
1  1993    1        1
1  1994    1        2
1  1995    1        3
1  1996    1        4
1  1997    1        5
1  1998    0        1
1  1999    0        2
2  1991    0        1
2  1992    0        2
2  1993    0        3     
2  1994    1        1
2  1994    1        2
2  1994    1        3
2  1994    1        4
2  1994    1        5

请教SAS程序应如何写,多谢!
作者: shiyiming    时间: 2006-11-14 19:12
标题: 回楼上
楼上有些原数据和新数据对不上。
我写了个小程序
data span (drop = x y);
   set insurance;             /*insurance就是原数据集*/
   retain duration 1 x 0 y 0;
   if _n_=1 then do;
      x=insure;
      y=id;
      end;
      else do;
         if x=insure & y =id then duration+1;
            else do;
                duration=1;
                x=insure;
                y=id;
            end;
      end;
run;
作者: shiyiming    时间: 2006-11-16 12:45
标题: 回复楼上
不好意思,我贴的原始数据中漏了一些records
我后来用了transpose,再用数组解决了这个问题
但是相比之下楼上仁兄的程序简洁有效,让我受益匪浅,多谢了




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