全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
1722 5
2012-10-15
大大们帮忙看一下!

我现在的数据按 ID year排序后是这个样子:

ID        year
1         1998
1         2001
2         1994
2         1995
2         1999
2         2001
3         1997
3         1999
3         2000

我想把每一个ID里最小年和最大年之间缺失的年份补全,做好之后是这个样子,黄色标出的是补全的部分:

ID        year
1         1998
1         1999
1         2000
1         2001
2         1994
2         1995
2         1996
2         1997
2         1998
2         1999
2         2000
2         2001
3         1997
3         1998
3         1999
3         2000


请问这个在SAS里边应该怎么实现?多谢!
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2012-10-15 07:20:08
data test;
input ID $        year;
cards;
1         1998
1         2001
2         1994
2         1995
2         1999
2         2001
3         1997
3         1999
3         2000
;

data test1;
    set test;
        by id year;
        lagyear=lag(year);
        if first.id then output;
        else do;
            if year=lagyear+1 then output;
                else do year=lagyear+1 to year;
                     output;
                end;
        end;
        drop lagyear;
run;
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2012-10-15 07:22:32
data test;
input ID $        year;
cards;
1         1998
1         2001
2         1994
2         1995
2         1999
2         2001
3         1997
3         1999
3         2000
;

data test2;
    set test;
        by id year;
        retain minyear;
        if first.id then minyear=year;
        if last.id then do
          maxyear=year;
         do year=minyear to maxyear;
                    output;
                 end;
        end;
        drop minyear maxyear;
run;
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2012-10-15 07:48:21
pobel 发表于 2012-10-15 07:20
data test;
input ID $        year;
cards;
多谢大牛!!!
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2012-10-16 10:07:38
To generate the desired the data set, one only needs the first and last observation for each id. So as long as a data set contain such information. It can be created automatically.

The follow will generate codes as,
NOTE: CALL EXECUTE generated line.
1   + data b;
2   + ID=1;
3   + do year=1998 to 2001;output;end;
4   + ID=2;
5   + do year=1994 to 2001;output;end;
6   + ID=3;
7   + do year=1997 to 2000;output;end;
8   + run;

*********************the output****************;
Obs    ID    year

   1     1    1998
   2     1    1999
   3     1    2000
   4     1    2001
   5     2    1994
   6     2    1995
   7     2    1996
   8     2    1997
   9     2    1998
  10     2    1999
  11     2    2000
  12     2    2001
  13     3    1997
  14     3    1998
  15     3    1999
  16     3    2000


data a;
input ID        year;
cards;
1         1998
1         2001
2         1994
2         1995
2         1999
2         2001
3         1997
3         1999
3         2000
;

data _null_;
  retain y0;
  set a end=end;
  by id;
  if _n_=1 then  call execute('data b;');

  if first.id then do;
    y0=year;
    call execute(cat('ID=',ID,';'));
  end;

  if last.id then do;
     call execute(cat('do year=',y0,' to ', year, ';output;end;'));
  end;

if end then call execute('run;');
run;

proc print;run;
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2012-10-16 13:03:03
欣赏一下!1
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群