deleting observations...

Reply
N/A
Posts: 1

deleting observations...

Hi everyone,

I have a quick question on how to delete observations.

Say I have a data set as following:

data example;

input y x1 x2 x3 x4 x5;

cards;

5 1 2 3 . 5

2 4 . . 6 8

;

run;

I want to delete rest of the observations after the first encounter of a missing value. For example I want the following result.

5 1 2 3 . .

2 4 . . . .

What would the code look like?

I tried using do loop with delete statement but it didn't quite work.

I tried

array x{5} x1 x2 x3 x4 x5;

do i=1 to 5;

if x=. then delete x to x[5];

output;

end;

Any help would be appreciated. Thanks!

Super User
Super User
Posts: 7,035

Re: deleting observations...

You could use two do loops. One to find the first missing value and the second to clear out the end of the array.

data want;

  set have;

  array x x1-x5;

  do i=1 to dim(x) while (not missing(x(i))); end;

  do i=i to dim(x); x(i)=.; end;

run;

Respected Advisor
Posts: 3,156

Re: deleting observations...

Or one array do loop:

data example;

input y x1 x2 x3 x4 x5;

cards;

5 1 2 3 . 5

2 4 . . 6 8

;

run;

data want;

set example;

array v y x1-x5;

do _n_=1 to dim(v)-1;

if _n_ < dim(v) then if missing(v(_n_)) then call missing (v(_n_+1));

end;

run;

proc print;run;

Kindly Regards,

Haikuo

Ask a Question
Discussion stats
  • 2 replies
  • 258 views
  • 3 likes
  • 3 in conversation