Hi all,
I am pretty new in SAS programming and do need your help. thx
i have a data set like this ( 5 variabes and too many observations)
num a b c d e
1 . 3 4 . .
2 . 5 . 6 .
3 1 2 2 . .
4 2 . . . 4
desired result
num a b c d e
1 . 3 4 . .
2 1 2 2 . .
Indeed, i want to have just those rows that contain 2 consecutive different values.
i do appreciate your help
It's a little complex, since it uses arrays. But not too lengthy:
data want;
set have;
array vars {5} a b c d e;
do _n_=1 to 4;
if (vars{_n_} ne .) and (vars{_n_+1} ne .) and (vars{_n_} ne vars{_n_+1}) then keep_flag='Y';
end;
if keep_flag='Y';
drop keep_flag;
run;
This logic should give you your desired output.
/* Creating the dataset */
Data Input;
Input Num a b c d e ;
Datalines;
1 . 3 4 . .
2 . 5 . 6 .
3 1 2 2 . .
4 2 . . . 4
;
Run;
/* Eliminate rows that do not have consecutive non-missing values */
Data Final;
Set Input;
If Num ^= "" Then do;
If ( (b*a = "") and (c*b = "") and (d*c = "") and (e*d = ""))
Then Delete;
End;
Run;
Thanks for your help. it did not work. still shows undesired observations.
I'm sorry that did not work.
Just out of curiousity, what is an example of an undesired row that was showing up in your output?
I will say that the array method in the post below is a better way of approaching the problem compared to what I did.
It's a little complex, since it uses arrays. But not too lengthy:
data want;
set have;
array vars {5} a b c d e;
do _n_=1 to 4;
if (vars{_n_} ne .) and (vars{_n_+1} ne .) and (vars{_n_} ne vars{_n_+1}) then keep_flag='Y';
end;
if keep_flag='Y';
drop keep_flag;
run;
it worked well. really appreciate it.
thanks 🙂
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.