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!
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;
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
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.