I've below sample data after proc transpose,
Col1 | Position |
V | 1 |
V/RRU22 | 2 |
V/RRU/22/17 | 3 |
V | 4 |
V/RRU22 | 5 |
V/RRU/22/18 | 6 |
V | 7 |
V/RRU22 | 8 |
V/RRU/22/9 | 9 |
V | 10 |
V/RRUL | 11 |
V/RRUL/11/7 | 12 |
V | 13 |
V/RRUL | 14 |
V/RRUL/62/23 | 15 |
I want to extract first unique record on col1 and first unique record row position should not change after sorting. I need below output
Col1 | Position |
V | 1 |
V/RRU22 | 2 |
V/RRU/22/17 | 3 |
V/RRU/22/18 | 6 |
V/RRU/22/9 | 9 |
V/RRUL | 11 |
V/RRUL/11/7 | 12 |
V/RRUL/62/23 | 15 |
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
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.
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.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.