楼主: superyxo
1776 5

[原创博文] 两条记录中间插入缺失记录的问题 [推广有奖]

  • 2关注
  • 1粉丝

硕士生

64%

还不是VIP/贵宾

-

威望
0
论坛币
91 个
通用积分
1.0005
学术水平
7 点
热心指数
2 点
信用等级
2 点
经验
9870 点
帖子
93
精华
0
在线时间
199 小时
注册时间
2007-8-28
最后登录
2021-4-15

楼主
superyxo 发表于 2012-10-15 06:54:06 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
大大们帮忙看一下!

我现在的数据按 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里边应该怎么实现?多谢!
二维码

扫码加我 拉你入群

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

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

关键词:year ear 记录 黄色

沙发
pobel 在职认证  发表于 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;
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
superyxo + 1 + 1 + 1 热心帮助其他会员

总评分: 学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

和谐拯救危机

藤椅
pobel 在职认证  发表于 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;
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
superyxo + 1 + 1 + 1 热心帮助其他会员

总评分: 学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

和谐拯救危机

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

报纸
bobguy 发表于 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;
已有 1 人评分论坛币 学术水平 热心指数 信用等级 收起 理由
数据分析师3K + 60 + 3 + 3 + 3 精彩帖子

总评分: 论坛币 + 60  学术水平 + 3  热心指数 + 3  信用等级 + 3   查看全部评分

地板
wodemac 发表于 2012-10-16 13:03:03
欣赏一下!1

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

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-28 19:06