SAS中文论坛

标题: 如何补充数据 [打印本页]

作者: shiyiming    时间: 2012-12-12 13:54
标题: 如何补充数据
数据格式如下
year id  price
1998 1  10
1999 1  12
2000 1  11.5
1998 2  11
1999 2  11.5
2000 3  12

我想对这个数据年增加到 2005年或者任意其他,price 缺省。即如下格式:
year id  price
1998 1  10
1999 1  12
2000 1  11.5
2001 1  .
2002 1  .
2003 1  .
2004 1  .
2005  1  .
1998 2  11
1999 2  11.5
2000 3  12
2001 2  .
2002 2  .
2003 2  .
2004 2  .
2005  2  .

谢谢!
作者: shiyiming    时间: 2012-12-13 13:47
标题: Re: 如何补充数据
自己想了下,这样做,效率不行。

data temp;
input year id price;
DATALINES;
1998 1 10
1999 1 12
2000 1 11.5
1998 2 11
1999 2 11.5
2000 3 12
;

run;

proc sort data=temp out=missing;
by year id;
run;


data missing;
set missing;
price=.;
do year=1995 to 1997;
if year<=1997 then output;
end;
run;

proc sort data=missing nodupkey;
by id year;
proc sort data=temp;
by id year;
/*merge have and the expanded missing*/
data want;
  merge missing (in=ina) temp (in=inb);
  by id year;
  if inb or (ina and not inb);
run;
作者: shiyiming    时间: 2013-1-4 02:56
标题: Re: 如何补充数据
[code:1mw391v3]data a;
input year id price;
   yi=catx('_',year,id);
cards;
1998 1 10
1999 1 12
2000 1 11.5
1998 2 11
1999 2 11.5
2000 2 12
;
data b;
do id=1 to 2;
do year=1995 to 2005;
   yi=catx('_',year,id);
   output;
   end;
end;
run;
proc sql;
create table ab(drop=yi) as
  select d1.*, d2.price from b d1 left join a d2
   on d1.yi=d2.yi
   order by id,year;
quit;[/code:1mw391v3]




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