DATA Step, Macro, Functions and more

base sas

Reply
Frequent Contributor
Posts: 115

base sas

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
Trusted Advisor
Posts: 1,913

Re: base sas

Posted in reply to sunilreddy

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

Super User
Posts: 5,499

Re: base sas

Posted in reply to sunilreddy

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.

Contributor
Posts: 31

Re: base sas

Posted in reply to sunilreddy

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.

Ask a Question
Discussion stats
  • 3 replies
  • 206 views
  • 0 likes
  • 4 in conversation