DATA Step, Macro, Functions and more

Find records with identical variable values.

Reply
N/A
Posts: 0

Find records with identical variable values.

Table variables NR and X. I want to keep the records for each NR, where the values of X are identical.

From

NR X
10 1
10 2
10 2
10 3
10 4
10 4
10 4
10 5
.

I would like to get

NR X
10 2
10 2
10 4
10 4
10 4
.

How can that be done in a data step?
Super User
Posts: 5,260

Re: Find records with identical variable values.

You could use SET with BY nr, then using first. and last. logic in a subsetting IF statement.

/Linus
Data never sleeps
N/A
Posts: 0

Re: Find records with identical variable values.

You mean BY X, I suppose?

IF FIRST.X=0 OR LAST.X=0;
Super User
Posts: 5,260

Re: Find records with identical variable values.

Yes, I was a bit quick.
It should be

BY nr x;

/Linus
Data never sleeps
Frequent Contributor
Posts: 102

Re: Find records with identical variable values.

Here is how I would handle this (I do it all the time for data validation purposes)
--------------------------------------------
data test;
input NR X;
cards;
10 1
10 2
10 2
10 3
10 4
10 4
10 4
10 5
;
run;

proc sort data=test;
by NR X;
run;

data dups;
set test;
by NR X;
if not(first.X and last.X);
run; Message was edited by: Curtis Mack
Ask a Question
Discussion stats
  • 4 replies
  • 143 views
  • 0 likes
  • 3 in conversation