加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

滞后期与提前期的做法

(2014-07-23 18:07:41)
标签:

it

分类: 03SAS数据处理
在数据处理中,很多的计算涉及到滞后与提前期数据项的设置,以便在后续的计算中运用。这里总结了SAS和STATA软件中两种 类型的设置。
         

STATA 代码:
相对来说,STATA的设置方法比较的简单,通过varname[_n+1] 或者 varname[_n-1]设置 提前项 和 滞后项在连续加减和连续相乘中能够有作用,参考http://blog.sina.com.cn/s/blog_65b2919d0102uxif.html

bys country_id (obs_date_month):gen  country_vwmktret_country1= country_vwmktret_country[_n+1]
bys country_id (obs_date_month):gen  country_vwmktret_country2= country_vwmktret_country[_n+2]

bys country_id (obs_date_month):gen  country_vwmktret_country3= country_vwmktret_country[_n+3]
bys country_id (obs_date_month):gen  country_vwmktret_country4= country_vwmktret_country[_n+4]

SAS 代码:
SAS可以使用函数 (lag  diff)或者 proc expand 来实现 提前 滞后项,也算比较简单。
data test;
input stockcode $ time price;
cards;
000001        2007 15
000001        2006 16
000001        2005 17
000002        2007 18
000002        2006 19
000002        2005 20
000003        2007 21
000003        2006 22
000003        2005 23
000004        2007 24
000004        2006 25
000004        2005 26
000005        2007 27
000005        2006 28
000005        2005 29
;

proc sort;
by stockcode time;
run;

 
data wanted; set test;
by   stockcode ;
lagprice=lag1(price);
if first.stockcode then call missing(lagprice);
run;

proc expand data = test  out = Example6 method=none; 
 by stockcode;    
 convert price =lead_qty / transformout= (lead); 
 convert price = lag_qty / transformout= (lag); 
 convert price =lead2_qty / transformout= (lead 2); 
 convert price =lag2_qty / transformout= (lag 2); 
 convert price =lag3_qty / transformout= (lag 3); 
 run; 

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有