## finding consecutive different values across variables

Solved
Occasional Contributor
Posts: 12

# finding consecutive different values across variables

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.

Accepted Solutions
Solution
‎06-15-2017 08:19 PM
Super User
Posts: 6,642

## Re: finding consecutive different values across variables

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;

All Replies
Contributor
Posts: 23

## Re: finding consecutive different values across variables

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;

Occasional Contributor
Posts: 12

## Re: finding consecutive different values across variables

Thanks for your help. it did not work. still shows undesired observations.

Contributor
Posts: 23

## Re: finding consecutive different values across variables

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.

Solution
‎06-15-2017 08:19 PM
Super User
Posts: 6,642

## Re: finding consecutive different values across variables

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;

Occasional Contributor
Posts: 12

## Re: finding consecutive different values across variables

it worked well. really appreciate it.

thanks

☑ This topic is solved.