Reading the next observation value in the current observation

I have a dataset called "enter" with the following observations

ID Salary
10 1000
20 2000
30 3000
40 4000

I need an output dataset with the following observations

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

Note. The next salary scenario should be the current observation value for the Next_Row_salary column. If there is no subsequent observation, then the current observation value for the Next_Row_salary column should be "null".

Please help me in creating sas code for this scenario.

+5
source share
3 answers

, .

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;
+17

. , :

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;
+1

This code is from Paul Dorfman on SAS-L and it does what you are looking for, as well as in one pass through the data

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 ;
+1
source

All Articles