BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Lochdonan
Fluorite | Level 6

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.

1 ACCEPTED SOLUTION

Accepted Solutions
AhmedAl_Attar
Ammonite | Level 13

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

4 REPLIES 4
Tom
Super User Tom
Super User

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;

 

Astounding
PROC Star

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!

 

AhmedAl_Attar
Ammonite | Level 13

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;

Lochdonan
Fluorite | Level 6

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

Thank you SO much!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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