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
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.