白风夕月 发表于 2020-3-11 09:39 
@Cecilia_Xi 谢谢大神,完美实现了我的数据需求!受益匪浅!
做了一下修改。若出现中间有多个年份的price值为缺失时,仍然适用(如果lag后的值是用来求年份间的差值的话)。
proc sort data=test;by stockcode time;run;
data want3/*(keep=stockcode time price change)*/;
set test;
by stockcode;
retain price1 ;
if first.stockcode then price1=.;
if price ne . then price1=price;
price2=lag(price1);
if not first.stockcode and price ne . then change=price1-price2;
run;