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-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
  • 3 replies
  • 814 views
  • 0 likes
  • 4 in conversation