DATA Step, Macro, Functions and more

Sorting a row in large data set

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 11
Accepted Solution

Sorting a row in large data set

[ Edited ]

I have a data set of 6 numbers:

F1    F2    F3     F4    F5    F6

72    35    108    61    7      201

 

with over 1 million records.

 

I need each row to be ordered as below. 

F1    F2    F3     F4     F5     F6

7      35     61     72     108   201

 

I have worked out how to do it in a macro and a very ugly data set using arrays. Is there an easyier way to do this?

 

Thanks again all.


Accepted Solutions
Solution
‎01-03-2017 02:56 PM
Regular Contributor
Posts: 213

Re: Sorting a row in large data set

Use the call sortn() function.

Here is a working example

data have;
length f1 - f6 8;
input f1 f2 f3 f4 f5 f6;
datalines;
72 35 108 61 7 201
;
run;
data want;
    set have;
    array fs (*) _numeric_;
    call sortn(of fs(*));
run;

View solution in original post


All Replies
Super User
Super User
Posts: 6,502

Re: Sorting a row in large data set

i think you just want to use the CALL SORTN()  function.

data have;
  input f1-f6 ;
cards;
72 35 108 61 7 201
;
data want ;
  set have ;
  call sortn(of f1-f6);
run;

data _null_;
  set want;
  put (_all_) (=);
run;

 

Super User
Posts: 5,088

Re: Sorting a row in large data set

[ Edited ]

A very easy way within a DATA step:

 

call sortn(f1, f2, f3, f4, f5, f6);

 

There might be abbreviations available ... you could test either of these:

 

call sortn(of f1-f6);

call sortn(f1-f6);

 

One of those is pretty sure to work.

 

Looks like Tom is a little quicker than I am today!

 

Solution
‎01-03-2017 02:56 PM
Regular Contributor
Posts: 213

Re: Sorting a row in large data set

Use the call sortn() function.

Here is a working example

data have;
length f1 - f6 8;
input f1 f2 f3 f4 f5 f6;
datalines;
72 35 108 61 7 201
;
run;
data want;
    set have;
    array fs (*) _numeric_;
    call sortn(of fs(*));
run;

Occasional Contributor
Posts: 11

Re: Sorting a row in large data set

This was 3 times faster than my macro and 2 times faster than my ugly data step.

Thank you SO much!

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 176 views
  • 0 likes
  • 4 in conversation