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.
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!
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.
Ready to level-up your skills? Choose your own adventure.