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 🙂
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.