finding consecutive different values across variables

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

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.

i do appreciate your help


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

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;

View solution in original post


All Replies
Contributor
Posts: 22

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: 6

Re: finding consecutive different values across variables

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

 

Contributor
Posts: 22

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: 5,352

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: 6

Re: finding consecutive different values across variables

it worked well. really appreciate it.

thanks Smiley Happy

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 152 views
  • 2 likes
  • 3 in conversation