| 
 | 
 
我的数据大概如下: 
id year com1 com2 com3 
1 2000 B1 B2 B3 
1 2001 B1 B3 B4 
1 2002 B1 B4 B6 
1 2003 B1 B2 B4 
1 2004 B2 B1 B3 
2 2000 B2 B3 B4 
2 2001 B1 B3 B4 
2 2002 B2 B3 B4 
2 2003 B2 B4 B5 
2 2004 B2 B3 B4 
 
我希望实现的是: 
1,每一年的com1与前一年的com1比较是否变化; 
2,每一年的com1如果变化是否在com1,com2,com3中变化; 
3,从第4年开始判断每一id的com1在前三年是否变化,前三年中任一年变化则为变化,变化是否仍在com1,com2,com3中 
 
同理验证com2, com3的变化。 
 
我的办法很笨,如下; 
proc sql; 
 create table test1 as 
 select a.*, b.com1 as lastYcom1, b.com2 as lastYcom2, b.com3 as lastYcom3 
 from sample as a left join sample as b 
 on a.id=b.id and a.year=b.year+1 
 order by id year; 
quit; 
data test1; set test1;  
 if lastYcom1 ne . then do;  
  if com1=lastYcom1 then com1ChangeY1=0;  
  else com1changeY1=1; 
  if com1=lastYcom1 or com1=lastYcom2 or com1=lastYcom3 then com1changein3com_Y1=1; 
  else com1changein3com_Y1=0; 
 end; 
run; 
 
如此比较每个过去年的com2, com3 
 
还请大牛们帮忙看看有没有更简洁的方式? 
 
非常感谢! 
 
 
 |   
 
 
 
 |