BookmarkSubscribeRSS Feed
zehross
Calcite | Level 5

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!

2 REPLIES 2
Tom
Super User Tom
Super User

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;

Haikuo
Onyx | Level 15

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

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1045 views
  • 3 likes
  • 3 in conversation