## Sorting a row in large data set

Solved
Occasional Contributor
Posts: 11

# 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
Super Contributor
Posts: 281

## 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;

All Replies
Super User
Posts: 8,108

## 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: 6,774

## 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
Super Contributor
Posts: 281

## 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.