BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
fama
Fluorite | Level 6

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

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

5 REPLIES 5
jdwaterman91
Obsidian | Level 7

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;

fama
Fluorite | Level 6

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

 

jdwaterman91
Obsidian | Level 7

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. 

Astounding
PROC Star

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;

fama
Fluorite | Level 6

it worked well. really appreciate it.

thanks 🙂

sas-innovate-2024.png

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.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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