Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
383 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 Answer

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...