Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
378 views
in Technique[技术] by (71.8m points)

sas - reading next observation's value in current observation

I've a dataset called 'input' with the following observations

ID Salary
10 1000
20 2000
30 3000
40 4000

I need an output dataset with following observations

ID Salary Next_row_Salary
10 1000 2000
20 2000 3000
30 3000 4000
40 4000 null

Note: The scenario is next obersavtion's salary should be the current observation's value for the column Next_Row_salary. If there is no next observation then the current observation's value for the column Next_Row_salary should be 'null'.

Kindly help me out in creating a sas code for this scenario.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are a few ways to achieve this, here's how I would do it.

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...