我有一个名为“输入”与以下意见数据集
ID工资
10 1000
20 2000
30 3000
40 4000
我需要以下意见的输出数据集
ID工资Next_row_Salary
10 1000 2000
20 2000 3000
30 3000 4000
40 4000空
注:该方案是未来obersavtion的薪水应该是当前观察的该列Next_Row_salary值。 如果没有下观察,那么当前观察的该列Next_Row_salary值应为“零”。
请帮我在创造这个场景一SAS代码。
有几个方式来实现这一点,这是我会怎么做。
data have;
input ID Salary;
cards;
10 1000
20 2000
30 3000
40 4000
;
run;
data want;
recno=_n_+1;
set have end=last;
if not last
then set have (keep=salary rename=(salary=next_row_salary)) point=recno;
else call missing(next_row_salary);
run;
有没有直接的方法在数据的步骤做到这一点。 还有您可以使用两种方法:
选项1:排序在倒车时,使用滞后函数
proc sort data=your_dataset;
by descending id;
run;
data your_dataset;
set your_dataset;
next_row_salary = lag(salary);
run;
proc sort; by id; run;
选项2:使用proc expand
proc expand data=your_dataset method=none;
by id;
convert salary = next_row_salary / transformout=(lead 1);
run;
此代码是从SAS-L保罗多尔夫曼和它你通过数据与一个通找什么,以及
Data Salary;
input id salary;
Datalines;
10 1000
20 2000
30 3000
40 4000
;
Run;
data need ;
retain id salary;
set salary (rename=(id = origid salary=next_salary)) end=end ;
if _n_ > 1 then output;
salary = next_salary;
id = origid;
if not end then return;
call missing(next_salary);
output;
drop origid;
run ;