Learning SAS? Welcome to the exclusive online community for all SAS learners.

how to sort values in a macro

Posts: 36

how to sort values in a macro


How to sort the values in a macro? For example, I have the following macro

%let macro1 = 15   77    22   40;

I want to create another macro having the same values in &macro1 but with increasing order like

&macro2 =  15     22     40    77;

Thanks a lot!

Respected Advisor
Posts: 3,777

Re: how to sort values in a macro

Since your input is a string of numbers it seems that using a data step array is most logical and easiest to code.  If you want it to be function style you could make a macro that uses DOSUBL.  This would be the innards of such a function style macro.

23         %let macro1=15 77 44 8;

24         data _null_;

25            array t[%sysfunc(countw(&macro1,%str( )))] _temporary_ (&macro1);

26            call sortn(of t

  • );
  • 27            call symputx('macro2',catx(' ',of t

  • ));
  • 28            run;

    29         %put &=macro2;

    MACRO2=8 15 44 77

    Posts: 36

    Re: how to sort values in a macro

    thank you very much!

    Ask a Question
    Discussion stats
    • 2 replies
    • 2 in conversation