为当前的下一行数据编写脚本 [英] Script the next row data for current

查看:29
本文介绍了为当前的下一行数据编写脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想从下一行复制数据怎么办.例如,客户 A 于 2015 年 1 月 1 日开始了他的当前行程,而下一次行程则是在 2015 年 1 月 15 日.因此,他当前行程的结束日期将是 2015 年 1 月 14 日,也就是他下次行程开始的前一天.我可以为结束行程日期编写什么脚本?

解决方案

由于 SAS 中没有 lead() 函数,您可以将数据按日期降序排序并使用 lag() 然后根据 Vasilij 的回答重新将其重新排序,或者您可以进行前瞻合并".

示例:

<前>proc sort data=have ;按客户 date_start ;跑步 ;数据要;合并有有 (firstobs=2 rename=(date_start=next_date customer=next_customer)) ;如果客户 = next_customer 然后做;date_end = next_date ;结尾 ;格式 date_end date7.;删除下一个_:;跑步 ;

What can I do if I want to copy the data from the next row. For example customer A started his current trip on 01JAN2015 and next trip on 15JAN2015. Therefore, his end trip date for his current trip will be on 14JAN2015, which is a day before his next trip starts. What can I script for the end trip date?

解决方案

As there is no lead() function in SAS, you can either sort your data into descending date order and use lag() then re-sort it back again, as per Vasilij's answer, or you can do a 'look-ahead merge'.

Example:

proc sort data=have ;
  by customer date_start ;
run ;

data want ;
  merge have
        have (firstobs=2 rename=(date_start=next_date customer=next_customer)) ;
  if customer = next_customer then do ;
    date_end = next_date ;
  end ;

  format date_end date7. ;
  drop next_: ;
run ;

这篇关于为当前的下一行数据编写脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆