BookmarkSubscribeRSS Feed
King
Calcite | Level 5

Hai,

please wite a macro to sort in horizontal way.

suppose

var1 var2 var3 var4 var5

1 20 7 2 6 resp

i want 1 2 6 7 20

3 REPLIES 3
art297
Opal | Level 21

If this is a homework assignment, the following probably won't be of much help.  However, if it isn't, the easiest way to sort such variables is with an array.  e.g.,

data have;

  array vars(*) var1-var5;

  input var1-var5;

  call sortn(of vars(*));

  cards;

1 20 7 2 6

;

Domenico
Fluorite | Level 6

Hi Art,

excellente solution. Didn't know about the call routine sortn.
Very nice code and simple to understand.

Thanks for that

Ksharp
Super User

Why do you want to use Macro?

There is no need to use Macro. Macro is only to use to substitute sas code.

data temp;
 input var1-var5;
cards;
1 20 7 2 6
2 5 24 2 65
;
run;
data _null_;
 set temp;
 array _var{*} var1 -var5;
 do i=1 to dim(_var);
  do j=i+1 to dim(_var);
   if _var{i} gt _var{j} then do;
                              temp=_var{i};
                              _var{i}=_var{j};
                              _var{j}=temp;
                              end;
  end;
 end;
 put _var{*}=;
run;

Ksharp

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 3 replies
  • 3616 views
  • 1 like
  • 4 in conversation