BookmarkSubscribeRSS Feed
sunilreddy
Fluorite | Level 6

I've below sample data after proc transpose,

Col1Position
V1
V/RRU222
V/RRU/22/173
V4
V/RRU225
V/RRU/22/186
V7
V/RRU228
V/RRU/22/99
V10
V/RRUL11
V/RRUL/11/712
V13
V/RRUL14
V/RRUL/62/2315

I want to extract first unique record on col1 and first unique record row position should not change after sorting. I need below output

Col1Position
V1
V/RRU222
V/RRU/22/173
V/RRU/22/186
V/RRU/22/99
V/RRUL11
V/RRUL/11/712
V/RRUL/62/2315
3 REPLIES 3
PaigeMiller
Diamond | Level 26

1. Sort by COL1 and POSITION

2. In a data step, use the FIRST.COL1 variable to find the first occurrence of each COL1 value; delete others

3. Sort by POSITION

--
Paige Miller
Astounding
PROC Star

Another approach:

proc summary data=have nway;

   class col1;

   var position;

   output out=want (keep=col1 position) min=;

run;

proc sort data=want;

   by position;

run;

I would guess that SQL can do this, but my SQL syntax is suspect:

proc sql noprint;

   create table want as select col1, min(position) as position from have group by col1 order by position;

quit;

Good luck.

ErikT
Obsidian | Level 7

You could also use hash tables to collect all unique elements.

In your sample data the position variable seems nicely sorted. Therefore Paige Millers' approach makes perfectly sense. If in reality that sequence is not guaranteed, you could initially add an additional variable with SequenceNumber = _N_; Then the selection as Paige is suggesting and finally sorting by SequenceNumber and dropping that variable again.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of 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
  • 3 replies
  • 1083 views
  • 0 likes
  • 4 in conversation