devon
devon

Reputation: 321

Reading next non-missiong observation's value in current observation

My question is quite similar to reading next observation's value in current observation except that I want to read the next Non-Missing observation (in SAS).

For instance, this is my data set

data have;
   input ID Salary;
   cards;
10 1000
20 .
30 3000
40 .
50 .
60 6000
;
run;

And I am looking for something like this

ID Salary
10 1000
20 3000
30 3000
40 6000
50 6000
60 6000

As you can see, for ID's 40, the salary get the value from the next non-missing observation which is ,from ID's 60, 6000.

Upvotes: 2

Views: 74

Answers (1)

Joe
Joe

Reputation: 63434

The linked question gives several suggestions, most of which can be adapted to this.

For example, the accepted answer can be slightly modified.

data want;
  set have;
  if missing(salary) then
    do _i = _n_+1 to nobs;
      set have(keep=salary) nobs=nobs point=_i;
      if not missing(salary) then leave;
    end;
run;

You could also sort the data in reverse and use retain to accomplish the same. That's probably more efficient if you have a lot of missingness.

Upvotes: 3

Related Questions