BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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?
4 REPLIES 4
LinusH
Tourmaline | Level 20
You could use SET with BY nr, then using first. and last. logic in a subsetting IF statement.

/Linus
Data never sleeps
deleted_user
Not applicable
You mean BY X, I suppose?

IF FIRST.X=0 OR LAST.X=0;
LinusH
Tourmaline | Level 20
Yes, I was a bit quick.
It should be

BY nr x;

/Linus
Data never sleeps
CurtisMack
Fluorite | Level 6
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

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1539 views
  • 0 likes
  • 3 in conversation